builtin.go 3.4 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. "os"
  19. "path/filepath"
  20. "go.opentelemetry.io/otel/attribute"
  21. "go.opentelemetry.io/otel/sdk"
  22. semconv "go.opentelemetry.io/otel/semconv/v1.21.0"
  23. )
  24. type (
  25. // telemetrySDK is a Detector that provides information about
  26. // the OpenTelemetry SDK used. This Detector is included as a
  27. // builtin. If these resource attributes are not wanted, use
  28. // the WithTelemetrySDK(nil) or WithoutBuiltin() options to
  29. // explicitly disable them.
  30. telemetrySDK struct{}
  31. // host is a Detector that provides information about the host
  32. // being run on. This Detector is included as a builtin. If
  33. // these resource attributes are not wanted, use the
  34. // WithHost(nil) or WithoutBuiltin() options to explicitly
  35. // disable them.
  36. host struct{}
  37. stringDetector struct {
  38. schemaURL string
  39. K attribute.Key
  40. F func() (string, error)
  41. }
  42. defaultServiceNameDetector struct{}
  43. )
  44. var (
  45. _ Detector = telemetrySDK{}
  46. _ Detector = host{}
  47. _ Detector = stringDetector{}
  48. _ Detector = defaultServiceNameDetector{}
  49. )
  50. // Detect returns a *Resource that describes the OpenTelemetry SDK used.
  51. func (telemetrySDK) Detect(context.Context) (*Resource, error) {
  52. return NewWithAttributes(
  53. semconv.SchemaURL,
  54. semconv.TelemetrySDKName("opentelemetry"),
  55. semconv.TelemetrySDKLanguageGo,
  56. semconv.TelemetrySDKVersion(sdk.Version()),
  57. ), nil
  58. }
  59. // Detect returns a *Resource that describes the host being run on.
  60. func (host) Detect(ctx context.Context) (*Resource, error) {
  61. return StringDetector(semconv.SchemaURL, semconv.HostNameKey, os.Hostname).Detect(ctx)
  62. }
  63. // StringDetector returns a Detector that will produce a *Resource
  64. // containing the string as a value corresponding to k. The resulting Resource
  65. // will have the specified schemaURL.
  66. func StringDetector(schemaURL string, k attribute.Key, f func() (string, error)) Detector {
  67. return stringDetector{schemaURL: schemaURL, K: k, F: f}
  68. }
  69. // Detect returns a *Resource that describes the string as a value
  70. // corresponding to attribute.Key as well as the specific schemaURL.
  71. func (sd stringDetector) Detect(ctx context.Context) (*Resource, error) {
  72. value, err := sd.F()
  73. if err != nil {
  74. return nil, fmt.Errorf("%s: %w", string(sd.K), err)
  75. }
  76. a := sd.K.String(value)
  77. if !a.Valid() {
  78. return nil, fmt.Errorf("invalid attribute: %q -> %q", a.Key, a.Value.Emit())
  79. }
  80. return NewWithAttributes(sd.schemaURL, sd.K.String(value)), nil
  81. }
  82. // Detect implements Detector.
  83. func (defaultServiceNameDetector) Detect(ctx context.Context) (*Resource, error) {
  84. return StringDetector(
  85. semconv.SchemaURL,
  86. semconv.ServiceNameKey,
  87. func() (string, error) {
  88. executable, err := os.Executable()
  89. if err != nil {
  90. return "unknown_service:go", nil
  91. }
  92. return "unknown_service:" + filepath.Base(executable), nil
  93. },
  94. ).Detect(ctx)
  95. }