// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package 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  == "" &&  == "" {
		return Empty(), nil
	}

	var  *Resource

	if  != "" {
		 = 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 {
		 = 
	} else if  != nil {
		 = fmt.Errorf("detecting resources: %s", []string{.Error(), .Error()})
	}

	return , 
}

func constructOTResources( string) (*Resource, error) {
	if  == "" {
		return Empty(), nil
	}
	 := strings.Split(, ",")
	var  []attribute.KeyValue
	var  []string
	for ,  := 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(, ))
	}
	var  error
	if len() > 0 {
		 = fmt.Errorf("%w: %v", errMissingValue, )
	}
	return NewSchemaless(...), 
}