env.go 2.7 KB

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