blob: 0d6e213d92455970a775ec083b3f5d31e564c562 [file] [log] [blame]
Abhay Kumara2ae5992025-11-10 14:02:24 +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
9 "go.opentelemetry.io/otel/attribute"
10)
11
12// config contains configuration for Resource creation.
13type config struct {
14 // detectors that will be evaluated.
15 detectors []Detector
16 // SchemaURL to associate with the Resource.
17 schemaURL string
18}
19
20// Option is the interface that applies a configuration option.
21type Option interface {
22 // apply sets the Option value of a config.
23 apply(config) config
24}
25
26// WithAttributes adds attributes to the configured Resource.
27func WithAttributes(attributes ...attribute.KeyValue) Option {
28 return WithDetectors(detectAttributes{attributes})
29}
30
31type detectAttributes struct {
32 attributes []attribute.KeyValue
33}
34
35func (d detectAttributes) Detect(context.Context) (*Resource, error) {
36 return NewSchemaless(d.attributes...), nil
37}
38
39// WithDetectors adds detectors to be evaluated for the configured resource.
40func WithDetectors(detectors ...Detector) Option {
41 return detectorsOption{detectors: detectors}
42}
43
44type detectorsOption struct {
45 detectors []Detector
46}
47
48func (o detectorsOption) apply(cfg config) config {
49 cfg.detectors = append(cfg.detectors, o.detectors...)
50 return cfg
51}
52
53// WithFromEnv adds attributes from environment variables to the configured resource.
54func WithFromEnv() Option {
55 return WithDetectors(fromEnv{})
56}
57
58// WithHost adds attributes from the host to the configured resource.
59func WithHost() Option {
60 return WithDetectors(host{})
61}
62
63// WithHostID adds host ID information to the configured resource.
64func WithHostID() Option {
65 return WithDetectors(hostIDDetector{})
66}
67
68// WithTelemetrySDK adds TelemetrySDK version info to the configured resource.
69func WithTelemetrySDK() Option {
70 return WithDetectors(telemetrySDK{})
71}
72
73// WithSchemaURL sets the schema URL for the configured resource.
74func WithSchemaURL(schemaURL string) Option {
75 return schemaURLOption(schemaURL)
76}
77
78type schemaURLOption string
79
80func (o schemaURLOption) apply(cfg config) config {
81 cfg.schemaURL = string(o)
82 return cfg
83}
84
85// WithOS adds all the OS attributes to the configured Resource.
86// See individual WithOS* functions to configure specific attributes.
87func WithOS() Option {
88 return WithDetectors(
89 osTypeDetector{},
90 osDescriptionDetector{},
91 )
92}
93
94// WithOSType adds an attribute with the operating system type to the configured Resource.
95func WithOSType() Option {
96 return WithDetectors(osTypeDetector{})
97}
98
99// WithOSDescription adds an attribute with the operating system description to the
100// configured Resource. The formatted string is equivalent to the output of the
101// `uname -snrvm` command.
102func WithOSDescription() Option {
103 return WithDetectors(osDescriptionDetector{})
104}
105
106// WithProcess adds all the Process attributes to the configured Resource.
107//
108// Warning! This option will include process command line arguments. If these
109// contain sensitive information it will be included in the exported resource.
110//
111// This option is equivalent to calling WithProcessPID,
112// WithProcessExecutableName, WithProcessExecutablePath,
113// WithProcessCommandArgs, WithProcessOwner, WithProcessRuntimeName,
114// WithProcessRuntimeVersion, and WithProcessRuntimeDescription. See each
115// option function for information about what resource attributes each
116// includes.
117func WithProcess() Option {
118 return WithDetectors(
119 processPIDDetector{},
120 processExecutableNameDetector{},
121 processExecutablePathDetector{},
122 processCommandArgsDetector{},
123 processOwnerDetector{},
124 processRuntimeNameDetector{},
125 processRuntimeVersionDetector{},
126 processRuntimeDescriptionDetector{},
127 )
128}
129
130// WithProcessPID adds an attribute with the process identifier (PID) to the
131// configured Resource.
132func WithProcessPID() Option {
133 return WithDetectors(processPIDDetector{})
134}
135
136// WithProcessExecutableName adds an attribute with the name of the process
137// executable to the configured Resource.
138func WithProcessExecutableName() Option {
139 return WithDetectors(processExecutableNameDetector{})
140}
141
142// WithProcessExecutablePath adds an attribute with the full path to the process
143// executable to the configured Resource.
144func WithProcessExecutablePath() Option {
145 return WithDetectors(processExecutablePathDetector{})
146}
147
148// WithProcessCommandArgs adds an attribute with all the command arguments (including
149// the command/executable itself) as received by the process to the configured
150// Resource.
151//
152// Warning! This option will include process command line arguments. If these
153// contain sensitive information it will be included in the exported resource.
154func WithProcessCommandArgs() Option {
155 return WithDetectors(processCommandArgsDetector{})
156}
157
158// WithProcessOwner adds an attribute with the username of the user that owns the process
159// to the configured Resource.
160func WithProcessOwner() Option {
161 return WithDetectors(processOwnerDetector{})
162}
163
164// WithProcessRuntimeName adds an attribute with the name of the runtime of this
165// process to the configured Resource.
166func WithProcessRuntimeName() Option {
167 return WithDetectors(processRuntimeNameDetector{})
168}
169
170// WithProcessRuntimeVersion adds an attribute with the version of the runtime of
171// this process to the configured Resource.
172func WithProcessRuntimeVersion() Option {
173 return WithDetectors(processRuntimeVersionDetector{})
174}
175
176// WithProcessRuntimeDescription adds an attribute with an additional description
177// about the runtime of the process to the configured Resource.
178func WithProcessRuntimeDescription() Option {
179 return WithDetectors(processRuntimeDescriptionDetector{})
180}
181
182// WithContainer adds all the Container attributes to the configured Resource.
183// See individual WithContainer* functions to configure specific attributes.
184func WithContainer() Option {
185 return WithDetectors(
186 cgroupContainerIDDetector{},
187 )
188}
189
190// WithContainerID adds an attribute with the id of the container to the configured Resource.
191// Note: WithContainerID will not extract the correct container ID in an ECS environment.
192// Please use the ECS resource detector instead (https://pkg.go.dev/go.opentelemetry.io/contrib/detectors/aws/ecs).
193func WithContainerID() Option {
194 return WithDetectors(cgroupContainerIDDetector{})
195}