blob: cefe4ab914af35312c8aa474d6a57ac60a201b52 [file] [log] [blame]
Abhay Kumar40252eb2025-10-13 13:25:53 +00001// Copyright The OpenTelemetry Authors
2// SPDX-License-Identifier: Apache-2.0
3
4package resource // import "go.opentelemetry.io/otel/sdk/resource"
5
6import (
7 "context"
8 "fmt"
9 "os"
10 "path/filepath"
11
12 "github.com/google/uuid"
13
14 "go.opentelemetry.io/otel/attribute"
15 "go.opentelemetry.io/otel/sdk"
16 semconv "go.opentelemetry.io/otel/semconv/v1.34.0"
17)
18
19type (
20 // telemetrySDK is a Detector that provides information about
21 // the OpenTelemetry SDK used. This Detector is included as a
22 // builtin. If these resource attributes are not wanted, use
23 // resource.New() to explicitly disable them.
24 telemetrySDK struct{}
25
26 // host is a Detector that provides information about the host
27 // being run on. This Detector is included as a builtin. If
28 // these resource attributes are not wanted, use the
29 // resource.New() to explicitly disable them.
30 host struct{}
31
32 stringDetector struct {
33 schemaURL string
34 K attribute.Key
35 F func() (string, error)
36 }
37
38 defaultServiceNameDetector struct{}
39
40 defaultServiceInstanceIDDetector struct{}
41)
42
43var (
44 _ Detector = telemetrySDK{}
45 _ Detector = host{}
46 _ Detector = stringDetector{}
47 _ Detector = defaultServiceNameDetector{}
48 _ Detector = defaultServiceInstanceIDDetector{}
49)
50
51// Detect returns a *Resource that describes the OpenTelemetry SDK used.
52func (telemetrySDK) Detect(context.Context) (*Resource, error) {
53 return NewWithAttributes(
54 semconv.SchemaURL,
55 semconv.TelemetrySDKName("opentelemetry"),
56 semconv.TelemetrySDKLanguageGo,
57 semconv.TelemetrySDKVersion(sdk.Version()),
58 ), nil
59}
60
61// Detect returns a *Resource that describes the host being run on.
62func (host) Detect(ctx context.Context) (*Resource, error) {
63 return StringDetector(semconv.SchemaURL, semconv.HostNameKey, os.Hostname).Detect(ctx)
64}
65
66// StringDetector returns a Detector that will produce a *Resource
67// containing the string as a value corresponding to k. The resulting Resource
68// will have the specified schemaURL.
69func StringDetector(schemaURL string, k attribute.Key, f func() (string, error)) Detector {
70 return stringDetector{schemaURL: schemaURL, K: k, F: f}
71}
72
73// Detect returns a *Resource that describes the string as a value
74// corresponding to attribute.Key as well as the specific schemaURL.
75func (sd stringDetector) Detect(ctx context.Context) (*Resource, error) {
76 value, err := sd.F()
77 if err != nil {
78 return nil, fmt.Errorf("%s: %w", string(sd.K), err)
79 }
80 a := sd.K.String(value)
81 if !a.Valid() {
82 return nil, fmt.Errorf("invalid attribute: %q -> %q", a.Key, a.Value.Emit())
83 }
84 return NewWithAttributes(sd.schemaURL, sd.K.String(value)), nil
85}
86
87// Detect implements Detector.
88func (defaultServiceNameDetector) Detect(ctx context.Context) (*Resource, error) {
89 return StringDetector(
90 semconv.SchemaURL,
91 semconv.ServiceNameKey,
92 func() (string, error) {
93 executable, err := os.Executable()
94 if err != nil {
95 return "unknown_service:go", nil
96 }
97 return "unknown_service:" + filepath.Base(executable), nil
98 },
99 ).Detect(ctx)
100}
101
102// Detect implements Detector.
103func (defaultServiceInstanceIDDetector) Detect(ctx context.Context) (*Resource, error) {
104 return StringDetector(
105 semconv.SchemaURL,
106 semconv.ServiceInstanceIDKey,
107 func() (string, error) {
108 version4Uuid, err := uuid.NewRandom()
109 if err != nil {
110 return "", err
111 }
112
113 return version4Uuid.String(), nil
114 },
115 ).Detect(ctx)
116}