// Copyright The OpenTelemetry Authors// SPDX-License-Identifier: Apache-2.0package resource // import "go.opentelemetry.io/otel/sdk/resource"import (semconv)const (// resourceAttrKey is the environment variable name OpenTelemetry Resource information will be read from. resourceAttrKey = "OTEL_RESOURCE_ATTRIBUTES"//nolint:gosec // False positive G101: Potential hardcoded credentials// svcNameKey is the environment variable name that Service Name information will be read from. svcNameKey = "OTEL_SERVICE_NAME")// errMissingValue is returned when a resource value is missing.var errMissingValue = fmt.Errorf("%w: missing value", ErrPartialResource)// fromEnv is a Detector that implements the Detector and collects// resources from environment. This Detector is included as a// builtin.type fromEnv struct{}// compile time assertion that FromEnv implements Detector interface.var _ Detector = fromEnv{}// Detect collects resources from environment.func (fromEnv) (context.Context) (*Resource, error) { := strings.TrimSpace(os.Getenv(resourceAttrKey)) := strings.TrimSpace(os.Getenv(svcNameKey))if == "" && == "" {returnEmpty(), nil }var *Resourceif != "" { = NewSchemaless(semconv.ServiceName()) } , := constructOTResources()// Ensure that the resource with the service name from OTEL_SERVICE_NAME // takes precedence, if it was defined. , := Merge(, )if == nil { = } elseif != nil { = fmt.Errorf("detecting resources: %s", []string{.Error(), .Error()}) }return , }func constructOTResources( string) (*Resource, error) {if == "" {returnEmpty(), nil } := strings.Split(, ",")var []attribute.KeyValuevar []stringfor , := range { , , := strings.Cut(, "=")if ! { = append(, )continue } := strings.TrimSpace() , := url.PathUnescape(strings.TrimSpace())if != nil {// Retain original value if decoding fails, otherwise it will be // an empty string. = otel.Handle() } = append(, attribute.String(, )) }varerroriflen() > 0 { = fmt.Errorf("%w: %v", errMissingValue, ) }returnNewSchemaless(...), }
The pages are generated with Goldsv0.8.2. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.