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