| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package resource // import "go.opentelemetry.io/otel/sdk/resource" |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
|
| 9 |
"go.opentelemetry.io/otel/attribute" |
| 10 |
) |
| 11 |
|
| 12 |
// config contains configuration for Resource creation. |
| 13 |
type 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. |
| 21 |
type 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. |
| 27 |
func WithAttributes(attributes ...attribute.KeyValue) Option { |
| 28 |
return WithDetectors(detectAttributes{attributes}) |
| 29 |
} |
| 30 |
|
| 31 |
type detectAttributes struct { |
| 32 |
attributes []attribute.KeyValue |
| 33 |
} |
| 34 |
|
| 35 |
func (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. |
| 40 |
func WithDetectors(detectors ...Detector) Option { |
| 41 |
return detectorsOption{detectors: detectors} |
| 42 |
} |
| 43 |
|
| 44 |
type detectorsOption struct { |
| 45 |
detectors []Detector |
| 46 |
} |
| 47 |
|
| 48 |
func (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. |
| 54 |
func WithFromEnv() Option { |
| 55 |
return WithDetectors(fromEnv{}) |
| 56 |
} |
| 57 |
|
| 58 |
// WithHost adds attributes from the host to the configured resource. |
| 59 |
func WithHost() Option { |
| 60 |
return WithDetectors(host{}) |
| 61 |
} |
| 62 |
|
| 63 |
// WithHostID adds host ID information to the configured resource. |
| 64 |
func WithHostID() Option { |
| 65 |
return WithDetectors(hostIDDetector{}) |
| 66 |
} |
| 67 |
|
| 68 |
// WithTelemetrySDK adds TelemetrySDK version info to the configured resource. |
| 69 |
func WithTelemetrySDK() Option { |
| 70 |
return WithDetectors(telemetrySDK{}) |
| 71 |
} |
| 72 |
|
| 73 |
// WithSchemaURL sets the schema URL for the configured resource. |
| 74 |
func WithSchemaURL(schemaURL string) Option { |
| 75 |
return schemaURLOption(schemaURL) |
| 76 |
} |
| 77 |
|
| 78 |
type schemaURLOption string |
| 79 |
|
| 80 |
func (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. |
| 87 |
func 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. |
| 95 |
func 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. |
| 102 |
func 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. |
| 117 |
func 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. |
| 132 |
func 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. |
| 138 |
func 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. |
| 144 |
func 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. |
| 154 |
func 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. |
| 160 |
func 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. |
| 166 |
func 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. |
| 172 |
func 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. |
| 178 |
func 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. |
| 184 |
func 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). |
| 193 |
func WithContainerID() Option { |
| 194 |
return WithDetectors(cgroupContainerIDDetector{}) |
| 195 |
} |
| 196 |
|
| 197 |
// WithService adds all the Service attributes to the configured Resource. |
| 198 |
func WithService() Option { |
| 199 |
return WithDetectors( |
| 200 |
defaultServiceInstanceIDDetector{}, |
| 201 |
defaultServiceNameDetector{}, |
| 202 |
) |
| 203 |
} |