-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
gcp.go
148 lines (137 loc) · 6.4 KB
/
gcp.go
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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package gcp // import "github.com/open-telemetry/opentelemetry-collector-contrib/processor/resourcedetectionprocessor/internal/gcp"
import (
"context"
"cloud.google.com/go/compute/metadata"
"github.com/GoogleCloudPlatform/opentelemetry-operations-go/detectors/gcp"
"go.opentelemetry.io/collector/featuregate"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/processor"
conventions "go.opentelemetry.io/collector/semconv/v1.6.1"
"go.uber.org/multierr"
"go.uber.org/zap"
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/resourcedetectionprocessor/internal"
localMetadata "github.com/open-telemetry/opentelemetry-collector-contrib/processor/resourcedetectionprocessor/internal/gcp/internal/metadata"
)
const (
// TypeStr is type of detector.
TypeStr = "gcp"
)
var removeGCPFaasID = featuregate.GlobalRegistry().MustRegister(
"processor.resourcedetection.removeGCPFaasID",
featuregate.StageAlpha,
featuregate.WithRegisterDescription("Remove faas.id from the GCP detector. Use faas.instance instead."),
featuregate.WithRegisterFromVersion("v0.87.0"))
// NewDetector returns a detector which can detect resource attributes on:
// * Google Compute Engine (GCE).
// * Google Kubernetes Engine (GKE).
// * Google App Engine (GAE).
// * Cloud Run.
// * Cloud Functions.
func NewDetector(set processor.CreateSettings, dcfg internal.DetectorConfig) (internal.Detector, error) {
cfg := dcfg.(Config)
return &detector{
logger: set.Logger,
detector: gcp.NewDetector(),
rb: localMetadata.NewResourceBuilder(cfg.ResourceAttributes),
}, nil
}
type detector struct {
logger *zap.Logger
detector gcpDetector
rb *localMetadata.ResourceBuilder
}
func (d *detector) Detect(context.Context) (resource pcommon.Resource, schemaURL string, err error) {
if !metadata.OnGCE() {
return pcommon.NewResource(), "", nil
}
d.rb.SetCloudProvider(conventions.AttributeCloudProviderGCP)
errs := d.rb.SetFromCallable(d.rb.SetCloudAccountID, d.detector.ProjectID)
switch d.detector.CloudPlatform() {
case gcp.GKE:
d.rb.SetCloudPlatform(conventions.AttributeCloudPlatformGCPKubernetesEngine)
errs = multierr.Combine(errs,
d.rb.SetZoneOrRegion(d.detector.GKEAvailabilityZoneOrRegion),
d.rb.SetFromCallable(d.rb.SetK8sClusterName, d.detector.GKEClusterName),
d.rb.SetFromCallable(d.rb.SetHostID, d.detector.GKEHostID),
)
// GCEHostname is fallible on GKE, since it's not available when using workload identity.
if v, err := d.detector.GCEHostName(); err == nil {
d.rb.SetHostName(v)
} else {
d.logger.Info("Fallible detector failed. This attribute will not be available.",
zap.String("key", conventions.AttributeHostName), zap.Error(err))
}
case gcp.CloudRun:
d.rb.SetCloudPlatform(conventions.AttributeCloudPlatformGCPCloudRun)
errs = multierr.Combine(errs,
d.rb.SetFromCallable(d.rb.SetFaasName, d.detector.FaaSName),
d.rb.SetFromCallable(d.rb.SetFaasVersion, d.detector.FaaSVersion),
d.rb.SetFromCallable(d.rb.SetFaasInstance, d.detector.FaaSID),
d.rb.SetFromCallable(d.rb.SetCloudRegion, d.detector.FaaSCloudRegion),
)
if !removeGCPFaasID.IsEnabled() {
errs = multierr.Combine(errs, d.rb.SetFromCallable(d.rb.SetFaasID, d.detector.FaaSID))
}
case gcp.CloudRunJob:
d.rb.SetCloudPlatform(conventions.AttributeCloudPlatformGCPCloudRun)
errs = multierr.Combine(errs,
d.rb.SetFromCallable(d.rb.SetFaasName, d.detector.FaaSName),
d.rb.SetFromCallable(d.rb.SetCloudRegion, d.detector.FaaSCloudRegion),
d.rb.SetFromCallable(d.rb.SetFaasInstance, d.detector.FaaSID),
d.rb.SetFromCallable(d.rb.SetGcpCloudRunJobExecution, d.detector.CloudRunJobExecution),
d.rb.SetFromCallable(d.rb.SetGcpCloudRunJobTaskIndex, d.detector.CloudRunJobTaskIndex),
)
if !removeGCPFaasID.IsEnabled() {
errs = multierr.Combine(errs, d.rb.SetFromCallable(d.rb.SetFaasID, d.detector.FaaSID))
}
case gcp.CloudFunctions:
d.rb.SetCloudPlatform(conventions.AttributeCloudPlatformGCPCloudFunctions)
errs = multierr.Combine(errs,
d.rb.SetFromCallable(d.rb.SetFaasName, d.detector.FaaSName),
d.rb.SetFromCallable(d.rb.SetFaasVersion, d.detector.FaaSVersion),
d.rb.SetFromCallable(d.rb.SetFaasInstance, d.detector.FaaSID),
d.rb.SetFromCallable(d.rb.SetCloudRegion, d.detector.FaaSCloudRegion),
)
if !removeGCPFaasID.IsEnabled() {
errs = multierr.Combine(errs, d.rb.SetFromCallable(d.rb.SetFaasID, d.detector.FaaSID))
}
case gcp.AppEngineFlex:
d.rb.SetCloudPlatform(conventions.AttributeCloudPlatformGCPAppEngine)
errs = multierr.Combine(errs,
d.rb.SetZoneAndRegion(d.detector.AppEngineFlexAvailabilityZoneAndRegion),
d.rb.SetFromCallable(d.rb.SetFaasName, d.detector.AppEngineServiceName),
d.rb.SetFromCallable(d.rb.SetFaasVersion, d.detector.AppEngineServiceVersion),
d.rb.SetFromCallable(d.rb.SetFaasInstance, d.detector.AppEngineServiceInstance),
)
if !removeGCPFaasID.IsEnabled() {
errs = multierr.Combine(errs, d.rb.SetFromCallable(d.rb.SetFaasID, d.detector.AppEngineServiceInstance))
}
case gcp.AppEngineStandard:
d.rb.SetCloudPlatform(conventions.AttributeCloudPlatformGCPAppEngine)
errs = multierr.Combine(errs,
d.rb.SetFromCallable(d.rb.SetFaasName, d.detector.AppEngineServiceName),
d.rb.SetFromCallable(d.rb.SetFaasVersion, d.detector.AppEngineServiceVersion),
d.rb.SetFromCallable(d.rb.SetFaasInstance, d.detector.AppEngineServiceInstance),
d.rb.SetFromCallable(d.rb.SetCloudAvailabilityZone, d.detector.AppEngineStandardAvailabilityZone),
d.rb.SetFromCallable(d.rb.SetCloudRegion, d.detector.AppEngineStandardCloudRegion),
)
if !removeGCPFaasID.IsEnabled() {
errs = multierr.Combine(errs, d.rb.SetFromCallable(d.rb.SetFaasID, d.detector.AppEngineServiceInstance))
}
case gcp.GCE:
d.rb.SetCloudPlatform(conventions.AttributeCloudPlatformGCPComputeEngine)
errs = multierr.Combine(errs,
d.rb.SetZoneAndRegion(d.detector.GCEAvailabilityZoneAndRegion),
d.rb.SetFromCallable(d.rb.SetHostType, d.detector.GCEHostType),
d.rb.SetFromCallable(d.rb.SetHostID, d.detector.GCEHostID),
d.rb.SetFromCallable(d.rb.SetHostName, d.detector.GCEHostName),
d.rb.SetFromCallable(d.rb.SetGcpGceInstanceHostname, d.detector.GCEInstanceHostname),
d.rb.SetFromCallable(d.rb.SetGcpGceInstanceName, d.detector.GCEInstanceName),
)
default:
// We don't support this platform yet, so just return with what we have
}
return d.rb.Emit(), conventions.SchemaURL, errs
}