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

package resource // import "go.opentelemetry.io/otel/sdk/resource"

import (
	
	
	
	
	
	

	semconv 
)

type containerIDProvider func() (string, error)

var (
	containerID         containerIDProvider = getContainerIDFromCGroup
	cgroupContainerIDRe                     = regexp.MustCompile(`^.*/(?:.*[-:])?([0-9a-f]+)(?:\.|\s*$)`)
)

type cgroupContainerIDDetector struct{}

const cgroupPath = "/proc/self/cgroup"

// Detect returns a *Resource that describes the id of the container.
// If no container id found, an empty resource will be returned.
func (cgroupContainerIDDetector) (context.Context) (*Resource, error) {
	,  := containerID()
	if  != nil {
		return nil, 
	}

	if  == "" {
		return Empty(), nil
	}
	return NewWithAttributes(semconv.SchemaURL, semconv.ContainerID()), nil
}

var (
	defaultOSStat = os.Stat
	osStat        = defaultOSStat

	defaultOSOpen = func( string) (io.ReadCloser, error) {
		return os.Open()
	}
	osOpen = defaultOSOpen
)

// getContainerIDFromCGroup returns the id of the container from the cgroup file.
// If no container id found, an empty string will be returned.
func getContainerIDFromCGroup() (string, error) {
	if ,  := osStat(cgroupPath); errors.Is(, os.ErrNotExist) {
		// File does not exist, skip
		return "", nil
	}

	,  := osOpen(cgroupPath)
	if  != nil {
		return "", 
	}
	defer .Close()

	return getContainerIDFromReader(), nil
}

// getContainerIDFromReader returns the id of the container from reader.
func getContainerIDFromReader( io.Reader) string {
	 := bufio.NewScanner()
	for .Scan() {
		 := .Text()

		if  := getContainerIDFromLine();  != "" {
			return 
		}
	}
	return ""
}

// getContainerIDFromLine returns the id of the container from one string line.
func getContainerIDFromLine( string) string {
	 := cgroupContainerIDRe.FindStringSubmatch()
	if len() <= 1 {
		return ""
	}
	return [1]
}