env.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. "context"
  17. "fmt"
  18. "net/url"
  19. "os"
  20. "strings"
  21. "go.opentelemetry.io/otel"
  22. "go.opentelemetry.io/otel/attribute"
  23. semconv "go.opentelemetry.io/otel/semconv/v1.21.0"
  24. )
  25. const (
  26. // resourceAttrKey is the environment variable name OpenTelemetry Resource information will be read from.
  27. resourceAttrKey = "OTEL_RESOURCE_ATTRIBUTES"
  28. // svcNameKey is the environment variable name that Service Name information will be read from.
  29. svcNameKey = "OTEL_SERVICE_NAME"
  30. )
  31. var (
  32. // errMissingValue is returned when a resource value is missing.
  33. errMissingValue = fmt.Errorf("%w: missing value", ErrPartialResource)
  34. )
  35. // fromEnv is a Detector that implements the Detector and collects
  36. // resources from environment. This Detector is included as a
  37. // builtin.
  38. type fromEnv struct{}
  39. // compile time assertion that FromEnv implements Detector interface.
  40. var _ Detector = fromEnv{}
  41. // Detect collects resources from environment.
  42. func (fromEnv) Detect(context.Context) (*Resource, error) {
  43. attrs := strings.TrimSpace(os.Getenv(resourceAttrKey))
  44. svcName := strings.TrimSpace(os.Getenv(svcNameKey))
  45. if attrs == "" && svcName == "" {
  46. return Empty(), nil
  47. }
  48. var res *Resource
  49. if svcName != "" {
  50. res = NewSchemaless(semconv.ServiceName(svcName))
  51. }
  52. r2, err := constructOTResources(attrs)
  53. // Ensure that the resource with the service name from OTEL_SERVICE_NAME
  54. // takes precedence, if it was defined.
  55. res, err2 := Merge(r2, res)
  56. if err == nil {
  57. err = err2
  58. } else if err2 != nil {
  59. err = fmt.Errorf("detecting resources: %s", []string{err.Error(), err2.Error()})
  60. }
  61. return res, err
  62. }
  63. func constructOTResources(s string) (*Resource, error) {
  64. if s == "" {
  65. return Empty(), nil
  66. }
  67. pairs := strings.Split(s, ",")
  68. var attrs []attribute.KeyValue
  69. var invalid []string
  70. for _, p := range pairs {
  71. k, v, found := strings.Cut(p, "=")
  72. if !found {
  73. invalid = append(invalid, p)
  74. continue
  75. }
  76. key := strings.TrimSpace(k)
  77. val, err := url.QueryUnescape(strings.TrimSpace(v))
  78. if err != nil {
  79. // Retain original value if decoding fails, otherwise it will be
  80. // an empty string.
  81. val = v
  82. otel.Handle(err)
  83. }
  84. attrs = append(attrs, attribute.String(key, val))
  85. }
  86. var err error
  87. if len(invalid) > 0 {
  88. err = fmt.Errorf("%w: %v", errMissingValue, invalid)
  89. }
  90. return NewSchemaless(attrs...), err
  91. }