Debian Codegraph @

detectors/gcp/gce.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package gcp // import "go.opentelemetry.io/contrib/detectors/gcp"
5
6 import (
7 "context"
8 "errors"
9 "fmt"
10 "os"
11 "strings"
12
13 "cloud.google.com/go/compute/metadata"
14 "go.opentelemetry.io/otel/attribute"
15 "go.opentelemetry.io/otel/sdk/resource"
16 semconv "go.opentelemetry.io/otel/semconv/v1.40.0"
17 )
18
19 // GCE collects resource information of GCE computing instances.
20 //
21 // Deprecated: Use gcp.NewDetector() instead, which sets the same resource attributes on GCE.
22 type GCE struct{}
23
24 // compile time assertion that GCE implements the resource.Detector interface.
25 var _ resource.Detector = (*GCE)(nil)
26
27 // Detect detects associated resources when running on GCE hosts.
28 func (*GCE) Detect(ctx context.Context) (*resource.Resource, error) {
29 if !metadata.OnGCE() {
30 return nil, nil
31 }
32
33 attributes := []attribute.KeyValue{
34 semconv.CloudProviderGCP,
35 }
36
37 var errInfo []string
38
39 if projectID, err := metadata.ProjectIDWithContext(ctx); hasProblem(err) {
40 errInfo = append(errInfo, err.Error())
41 } else if projectID != "" {
42 attributes = append(attributes, semconv.CloudAccountID(projectID))
43 }
44
45 if zone, err := metadata.ZoneWithContext(ctx); hasProblem(err) {
46 errInfo = append(errInfo, err.Error())
47 } else if zone != "" {
48 attributes = append(attributes, semconv.CloudAvailabilityZone(zone))
49
50 splitArr := strings.SplitN(zone, "-", 3)
51 if len(splitArr) == 3 {
52 attributes = append(attributes, semconv.CloudRegion(strings.Join(splitArr[0:2], "-")))
53 }
54 }
55
56 if instanceID, err := metadata.InstanceIDWithContext(ctx); hasProblem(err) {
57 errInfo = append(errInfo, err.Error())
58 } else if instanceID != "" {
59 attributes = append(attributes, semconv.HostID(instanceID))
60 }
61
62 if name, err := metadata.InstanceNameWithContext(ctx); hasProblem(err) {
63 errInfo = append(errInfo, err.Error())
64 } else if name != "" {
65 attributes = append(attributes, semconv.HostName(name))
66 }
67
68 if hostname, err := os.Hostname(); hasProblem(err) {
69 errInfo = append(errInfo, err.Error())
70 } else if hostname != "" {
71 attributes = append(attributes, semconv.HostName(hostname))
72 }
73
74 if hostType, err := metadata.GetWithContext(ctx, "instance/machine-type"); hasProblem(err) {
75 errInfo = append(errInfo, err.Error())
76 } else if hostType != "" {
77 attributes = append(attributes, semconv.HostType(hostType))
78 }
79
80 var aggregatedErr error
81 if len(errInfo) > 0 {
82 aggregatedErr = fmt.Errorf("detecting GCE resources: %s", errInfo)
83 }
84
85 return resource.NewWithAttributes(semconv.SchemaURL, attributes...), aggregatedErr
86 }
87
88 // hasProblem checks if the err is not nil or for missing resources.
89 func hasProblem(err error) bool {
90 if err == nil {
91 return false
92 }
93
94 var nde metadata.NotDefinedError
95 if undefined := errors.As(err, &nde); undefined {
96 return false
97 }
98 return true
99 }