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