| 1 | // Copyright The OpenTelemetry Authors |
| 2 | // SPDX-License-Identifier: Apache-2.0 |
| 3 | |
| 4 | // Package autodetect provides functionality to configures and use a set of |
| 5 | // resource detectors at runtime. |
| 6 | package autodetect // import "go.opentelemetry.io/contrib/detectors/autodetect" |
| 7 | |
| 8 | import ( |
| 9 | "context" |
| 10 | "errors" |
| 11 | "fmt" |
| 12 | "sort" |
| 13 | "sync" |
| 14 | |
| 15 | "go.opentelemetry.io/otel/sdk/resource" |
| 16 | |
| 17 | "go.opentelemetry.io/contrib/detectors/aws/ec2/v2" |
| 18 | "go.opentelemetry.io/contrib/detectors/aws/ecs" |
| 19 | "go.opentelemetry.io/contrib/detectors/aws/eks" |
| 20 | "go.opentelemetry.io/contrib/detectors/aws/lambda" |
| 21 | "go.opentelemetry.io/contrib/detectors/azure/azurevm" |
| 22 | "go.opentelemetry.io/contrib/detectors/gcp" |
| 23 | ) |
| 24 | |
| 25 | var ( |
| 26 | // IDAWSEC2 is the ID for the AWS EC2 detector that detects resource |
| 27 | // attributes on Amazon Web Services (AWS) EC2 instances (see |
| 28 | // ec2.NewResourceDetector for details). |
| 29 | IDAWSEC2 = ID("aws.ec2") |
| 30 | // IDAWSECS is the ID for the AWS ECS detector that detects resource |
| 31 | // attributes on Amazon Web Services (AWS) ECS clusters (see |
| 32 | // ecs.NewResourceDetector for details). |
| 33 | IDAWSECS = ID("aws.ecs") |
| 34 | // IDAWSEKS is the ID for the AWS EKS detector that detects resource |
| 35 | // attributes on Amazon Web Services (AWS) EKS clusters (see |
| 36 | // eks.NewResourceDetector for details). |
| 37 | IDAWSEKS = ID("aws.eks") |
| 38 | // IDAWSLambda is the ID for the AWS Lambda detector that detects resource |
| 39 | // attributes on Amazon Web Services (AWS) Lambda functions (see |
| 40 | // lambda.NewResourceDetector for details). |
| 41 | IDAWSLambda = ID("aws.lambda") |
| 42 | // IDAzureVM is the ID for the Azure VM detector that detects resource |
| 43 | // attributes on Microsoft Azure virtual machines (see azurevm.New for |
| 44 | // details). |
| 45 | IDAzureVM = ID("azure.vm") |
| 46 | // IDGCP is the ID for the GCP detector that detects resource attributes on |
| 47 | // Google Cloud Platform (GCP) environments (see gcp.NewDetector for |
| 48 | // details). |
| 49 | IDGCP = ID("gcp") |
| 50 | // IDHost is the ID for the host detector. This detector detects the |
| 51 | // "host.name" attribute from the os.Hostname function. |
| 52 | IDHost = ID("host") |
| 53 | // IDHostID is the ID for the host ID detector. This detector detects the |
| 54 | // "host.id" attribute, which is a unique identifier for the host (e.g., |
| 55 | // machine-id, UUID). |
| 56 | IDHostID = ID("host.id") |
| 57 | // IDTelemetrySDK is the ID for the telemetry SDK detector. This detector |
| 58 | // detects the "telemetry.sdk.name", "telemetry.sdk.language", and |
| 59 | // "telemetry.sdk.version" attributes, which provide information about the |
| 60 | // SDK being used. |
| 61 | IDTelemetrySDK = ID("telemetry.sdk") |
| 62 | // IDOSType is the ID for the OS type detector. This detector detects the |
| 63 | // "os.type" attribute, which indicates the type of operating system (e.g., |
| 64 | // "linux", "windows", "darwin"). |
| 65 | IDOSType = ID("os.type") |
| 66 | // IDOSDescription is the ID for the OS description detector. This detector |
| 67 | // detects the "os.description" attribute, which provides a human-readable |
| 68 | // description of the operating system. |
| 69 | IDOSDescription = ID("os.description") |
| 70 | // IDProcessPID is the ID for the process PID detector. This detector |
| 71 | // detects the "process.pid" attribute, which is the process ID of the |
| 72 | // current process. |
| 73 | IDProcessPID = ID("process.pid") |
| 74 | // IDProcessExecutableName is the ID for the process executable name |
| 75 | // detector. This detector detects the "process.executable.name" attribute, |
| 76 | // which is the name of the executable file for the current process. |
| 77 | IDProcessExecutableName = ID("process.executable.name") |
| 78 | // IDProcessExecutablePath is the ID for the process executable path |
| 79 | // detector. This detector detects the "process.executable.path" attribute, |
| 80 | // which is the full path to the executable file for the current process. |
| 81 | IDProcessExecutablePath = ID("process.executable.path") |
| 82 | // IDProcessCommandArgs is the ID for the process command arguments |
| 83 | // detector. This detector detects the "process.command.args" attribute, |
| 84 | // which is the command line arguments used to start the current process. |
| 85 | // |
| 86 | // Warning! This detector will include process command line arguments. If |
| 87 | // these contain sensitive information it will be included in the exported |
| 88 | // resource. |
| 89 | IDProcessCommandArgs = ID("process.command.args") |
| 90 | // IDProcessOwner is the ID for the process owner detector. This detector |
| 91 | // detects the "process.owner" attribute, which is the user who owns the |
| 92 | // current process. |
| 93 | IDProcessOwner = ID("process.owner") |
| 94 | // IDProcessRuntimeName is the ID for the process runtime name detector. |
| 95 | // This detector detects the "process.runtime.name" attribute, which is the |
| 96 | // name of the runtime environment for the current process (e.g., "go", |
| 97 | // "python", "java"). |
| 98 | IDProcessRuntimeName = ID("process.runtime.name") |
| 99 | // IDProcessRuntimeVersion is the ID for the process runtime version |
| 100 | // detector. This detector detects the "process.runtime.version" attribute, |
| 101 | // which is the version of the runtime environment for the current process |
| 102 | // (e.g., "1.16.3", "3.8.5"). |
| 103 | IDProcessRuntimeVersion = ID("process.runtime.version") |
| 104 | // IDProcessRuntimeDescription is the ID for the process runtime |
| 105 | // description detector. This detector detects the |
| 106 | // "process.runtime.description" attribute, which provides an additional |
| 107 | // description of the runtime environment for the current process (e.g., |
| 108 | // "Go runtime version 1.16.3", "Python 3.8.5"). |
| 109 | IDProcessRuntimeDescription = ID("process.runtime.description") |
| 110 | // IDContainer is the ID for the container detector. This detector detects |
| 111 | // the "container.id" attribute, which is a unique identifier for the |
| 112 | // container in which the process is running. This is useful for |
| 113 | // identifying the container in which the process is running, especially in |
| 114 | // containerized environments like Kubernetes or Docker. |
| 115 | IDContainer = ID("container") |
| 116 | ) |
| 117 | |
| 118 | var ( |
| 119 | registryMu sync.Mutex |
| 120 | registry = map[ID]func() resource.Detector{ |
| 121 | IDAWSEC2: ec2.NewResourceDetector, |
| 122 | IDAWSECS: ecs.NewResourceDetector, |
| 123 | IDAWSEKS: eks.NewResourceDetector, |
| 124 | IDAWSLambda: lambda.NewResourceDetector, |
| 125 | |
| 126 | IDAzureVM: func() resource.Detector { |
| 127 | return azurevm.New() |
| 128 | }, |
| 129 | |
| 130 | IDGCP: gcp.NewDetector, |
| 131 | |
| 132 | IDHost: optFactory(resource.WithHost()), |
| 133 | IDHostID: optFactory(resource.WithHostID()), |
| 134 | |
| 135 | IDTelemetrySDK: optFactory(resource.WithTelemetrySDK()), |
| 136 | |
| 137 | IDOSType: optFactory(resource.WithOSType()), |
| 138 | IDOSDescription: optFactory(resource.WithOSDescription()), |
| 139 | |
| 140 | IDProcessPID: optFactory(resource.WithProcessPID()), |
| 141 | IDProcessExecutableName: optFactory(resource.WithProcessExecutableName()), |
| 142 | IDProcessExecutablePath: optFactory(resource.WithProcessExecutablePath()), |
| 143 | IDProcessCommandArgs: optFactory(resource.WithProcessCommandArgs()), |
| 144 | IDProcessOwner: optFactory(resource.WithProcessOwner()), |
| 145 | IDProcessRuntimeName: optFactory(resource.WithProcessRuntimeName()), |
| 146 | IDProcessRuntimeVersion: optFactory(resource.WithProcessRuntimeVersion()), |
| 147 | IDProcessRuntimeDescription: optFactory(resource.WithProcessRuntimeDescription()), |
| 148 | |
| 149 | IDContainer: optFactory(resource.WithContainer()), |
| 150 | } |
| 151 | ) |
| 152 | |
| 153 | // ID represents the unique identifier of a resource detector. |
| 154 | type ID string |
| 155 | |
| 156 | // Register registers a new resource detector function with the given ID. |
| 157 | func Register(id ID, fn func() resource.Detector) { |
| 158 | registryMu.Lock() |
| 159 | defer registryMu.Unlock() |
| 160 | if _, exists := registry[id]; exists { |
| 161 | panic("detector already registered: " + id) |
| 162 | } |
| 163 | registry[id] = fn |
| 164 | } |
| 165 | |
| 166 | // Registered returns a sorted slice of all registered resource detector IDs. |
| 167 | func Registered() []ID { |
| 168 | registryMu.Lock() |
| 169 | defer registryMu.Unlock() |
| 170 | out := make([]ID, 0, len(registry)) |
| 171 | for id := range registry { |
| 172 | out = append(out, id) |
| 173 | } |
| 174 | |
| 175 | sort.SliceStable(out, func(i, j int) bool { |
| 176 | return out[i] < out[j] |
| 177 | }) |
| 178 | return out |
| 179 | } |
| 180 | |
| 181 | // optDetector is a resource.Detector that uses a resource.Option to |
| 182 | // create a resource.Resource. This is useful for detectors that |
| 183 | // do not require any additional logic beyond creating a resource |
| 184 | // from a resource.Option but do not export a concrete resource.Detector type |
| 185 | // directly. |
| 186 | type optDetector struct { |
| 187 | opt resource.Option |
| 188 | } |
| 189 | |
| 190 | var _ resource.Detector = optDetector{} |
| 191 | |
| 192 | // optFactory returns a function that creates an resource.Detector factory |
| 193 | // function with the given resource.Option. |
| 194 | func optFactory(opt resource.Option) func() resource.Detector { |
| 195 | return func() resource.Detector { return optDetector{opt: opt} } |
| 196 | } |
| 197 | |
| 198 | // Detect returns the resource.Resource created by the resource.Option passed |
| 199 | // to the optDetector. |
| 200 | func (d optDetector) Detect(ctx context.Context) (*resource.Resource, error) { |
| 201 | return resource.New(ctx, d.opt) |
| 202 | } |
| 203 | |
| 204 | // composite is a [resource.Detector] that composes multiple |
| 205 | // [resource.Detector] into a single instance. |
| 206 | type composite struct { |
| 207 | detectors []resource.Detector |
| 208 | } |
| 209 | |
| 210 | var _ resource.Detector = &composite{} |
| 211 | |
| 212 | // newComposite returns a new composite detector that runs the provided |
| 213 | // detectors in parallel and merges their results. |
| 214 | func newComposite(detectors []resource.Detector) *composite { |
| 215 | return &composite{detectors: detectors} |
| 216 | } |
| 217 | |
| 218 | // Detect runs all the detectors in parallel and merges the results into a |
| 219 | // single resource.Resource. If any detector returns an error, it is |
| 220 | // collected and returned as a single error. The resulting |
| 221 | // resource.Resource is the merge of all the resources returned by the |
| 222 | // detectors. If there is a merge conflict (e.g., different schema URLs), |
| 223 | // the resulting resource.Resource will be a partial resource with an |
| 224 | // error indicating the conflict (see |
| 225 | // [resource.ErrSchemaURLConflict] for more information). |
| 226 | func (c *composite) Detect(ctx context.Context) (*resource.Resource, error) { |
| 227 | out := <-mergeDetections(doDetect(ctx, c.detectors)) |
| 228 | return out.res, out.err |
| 229 | } |
| 230 | |
| 231 | // detection is the result of a [resource.Detector] detection. |
| 232 | type detection struct { |
| 233 | res *resource.Resource |
| 234 | err error |
| 235 | } |
| 236 | |
| 237 | // doDetect runs all the detectors concurrently in their own goroutines. All |
| 238 | // detections are sent on the returned channel, and the channel is closed once |
| 239 | // all detections are complete. |
| 240 | func doDetect(ctx context.Context, detectors []resource.Detector) <-chan detection { |
| 241 | detected := make(chan detection, len(detectors)) |
| 242 | go func() { |
| 243 | var wg sync.WaitGroup |
| 244 | for _, detector := range detectors { |
| 245 | wg.Add(1) |
| 246 | go func(d resource.Detector) { |
| 247 | defer wg.Done() |
| 248 | r, e := d.Detect(ctx) |
| 249 | detected <- detection{res: r, err: e} |
| 250 | }(detector) |
| 251 | } |
| 252 | |
| 253 | wg.Wait() |
| 254 | close(detected) |
| 255 | }() |
| 256 | return detected |
| 257 | } |
| 258 | |
| 259 | // mergeDetections merges the results of multiple detections received on the in |
| 260 | // chan into a single detection result. The resulting detection is sent on |
| 261 | // the returned channel. If any of the detections have an error, it is |
| 262 | // collected and returned as a single error. The resulting resource.Resource |
| 263 | // is the merge of all the resources returned by the detectors. If there is a |
| 264 | // merge conflict (e.g., different schema URLs), the resulting |
| 265 | // resource.Resource will be a partial resource with an |
| 266 | // error indicating the conflict (see |
| 267 | // [resource.ErrSchemaURLConflict] for more information). |
| 268 | func mergeDetections(in <-chan detection) <-chan detection { |
| 269 | merged := make(chan detection, 1) |
| 270 | go func() { |
| 271 | m := detection{res: resource.Empty()} |
| 272 | for d := range in { |
| 273 | m.err = errors.Join(m.err, d.err) |
| 274 | |
| 275 | var err error |
| 276 | m.res, err = resource.Merge(m.res, d.res) |
| 277 | if err != nil { |
| 278 | // Merge errors are not recoverable. |
| 279 | m.res, m.err = nil, err |
| 280 | break |
| 281 | } |
| 282 | } |
| 283 | merged <- m |
| 284 | close(merged) |
| 285 | }() |
| 286 | return merged |
| 287 | } |
| 288 | |
| 289 | // ErrUnknownDetector is returned when an unknown resource detector ID is |
| 290 | // requested. |
| 291 | var ErrUnknownDetector = errors.New("unknown resource detector") |
| 292 | |
| 293 | // Detector returns a [resource.Detector] composed of the detectors |
| 294 | // identified by the provided IDs. If an ID is not recognized, |
| 295 | // ErrUnknownDetector is returned. The returned detector merges all the |
| 296 | // resource from each detector when Detect is called. The order of the merge is |
| 297 | // not guaranteed. |
| 298 | func Detector(ids ...ID) (resource.Detector, error) { |
| 299 | registryMu.Lock() |
| 300 | defer registryMu.Unlock() |
| 301 | |
| 302 | var ( |
| 303 | detectors []resource.Detector |
| 304 | err error |
| 305 | ) |
| 306 | |
| 307 | for _, id := range ids { |
| 308 | fn, exists := registry[id] |
| 309 | if !exists { |
| 310 | e := fmt.Errorf("%w: %s", ErrUnknownDetector, id) |
| 311 | err = errors.Join(err, e) |
| 312 | continue |
| 313 | } |
| 314 | detectors = append(detectors, fn()) |
| 315 | } |
| 316 | return newComposite(detectors), err |
| 317 | } |