container.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // Copyright The OpenTelemetry Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package resource // import "go.opentelemetry.io/otel/sdk/resource"
  15. import (
  16. "bufio"
  17. "context"
  18. "errors"
  19. "io"
  20. "os"
  21. "regexp"
  22. semconv "go.opentelemetry.io/otel/semconv/v1.21.0"
  23. )
  24. type containerIDProvider func() (string, error)
  25. var (
  26. containerID containerIDProvider = getContainerIDFromCGroup
  27. cgroupContainerIDRe = regexp.MustCompile(`^.*/(?:.*-)?([0-9a-f]+)(?:\.|\s*$)`)
  28. )
  29. type cgroupContainerIDDetector struct{}
  30. const cgroupPath = "/proc/self/cgroup"
  31. // Detect returns a *Resource that describes the id of the container.
  32. // If no container id found, an empty resource will be returned.
  33. func (cgroupContainerIDDetector) Detect(ctx context.Context) (*Resource, error) {
  34. containerID, err := containerID()
  35. if err != nil {
  36. return nil, err
  37. }
  38. if containerID == "" {
  39. return Empty(), nil
  40. }
  41. return NewWithAttributes(semconv.SchemaURL, semconv.ContainerID(containerID)), nil
  42. }
  43. var (
  44. defaultOSStat = os.Stat
  45. osStat = defaultOSStat
  46. defaultOSOpen = func(name string) (io.ReadCloser, error) {
  47. return os.Open(name)
  48. }
  49. osOpen = defaultOSOpen
  50. )
  51. // getContainerIDFromCGroup returns the id of the container from the cgroup file.
  52. // If no container id found, an empty string will be returned.
  53. func getContainerIDFromCGroup() (string, error) {
  54. if _, err := osStat(cgroupPath); errors.Is(err, os.ErrNotExist) {
  55. // File does not exist, skip
  56. return "", nil
  57. }
  58. file, err := osOpen(cgroupPath)
  59. if err != nil {
  60. return "", err
  61. }
  62. defer file.Close()
  63. return getContainerIDFromReader(file), nil
  64. }
  65. // getContainerIDFromReader returns the id of the container from reader.
  66. func getContainerIDFromReader(reader io.Reader) string {
  67. scanner := bufio.NewScanner(reader)
  68. for scanner.Scan() {
  69. line := scanner.Text()
  70. if id := getContainerIDFromLine(line); id != "" {
  71. return id
  72. }
  73. }
  74. return ""
  75. }
  76. // getContainerIDFromLine returns the id of the container from one string line.
  77. func getContainerIDFromLine(line string) string {
  78. matches := cgroupContainerIDRe.FindStringSubmatch(line)
  79. if len(matches) <= 1 {
  80. return ""
  81. }
  82. return matches[1]
  83. }