-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathgcp.go
225 lines (210 loc) · 8.05 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
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
// 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"
"fmt"
"cloud.google.com/go/compute/metadata"
"github.com/GoogleCloudPlatform/opentelemetry-operations-go/detectors/gcp"
"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"
)
// 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(),
resourceAttributes: cfg.ResourceAttributes,
}, nil
}
type detector struct {
logger *zap.Logger
detector gcpDetector
resourceAttributes localMetadata.ResourceAttributesConfig
}
func (d *detector) Detect(context.Context) (resource pcommon.Resource, schemaURL string, err error) {
res := pcommon.NewResource()
if !metadata.OnGCE() {
return res, "", nil
}
b := &resourceBuilder{logger: d.logger, attrs: res.Attributes()}
if d.resourceAttributes.CloudProvider.Enabled {
b.attrs.PutStr(conventions.AttributeCloudProvider, conventions.AttributeCloudProviderGCP)
}
if d.resourceAttributes.CloudAccountID.Enabled {
b.add(conventions.AttributeCloudAccountID, d.detector.ProjectID)
}
switch d.detector.CloudPlatform() {
case gcp.GKE:
if d.resourceAttributes.CloudPlatform.Enabled {
b.attrs.PutStr(conventions.AttributeCloudPlatform, conventions.AttributeCloudPlatformGCPKubernetesEngine)
}
if d.resourceAttributes.CloudAvailabilityZone.Enabled {
b.addZoneOrRegion(d.detector.GKEAvailabilityZoneOrRegion, d.resourceAttributes)
}
if d.resourceAttributes.K8sClusterName.Enabled {
b.add(conventions.AttributeK8SClusterName, d.detector.GKEClusterName)
}
if d.resourceAttributes.HostID.Enabled {
b.add(conventions.AttributeHostID, d.detector.GKEHostID)
}
// GCEHostname is fallible on GKE, since it's not available when using workload identity.
if d.resourceAttributes.HostName.Enabled {
b.addFallible(conventions.AttributeHostName, d.detector.GCEHostName)
}
case gcp.CloudRun:
if d.resourceAttributes.CloudPlatform.Enabled {
b.attrs.PutStr(conventions.AttributeCloudPlatform, conventions.AttributeCloudPlatformGCPCloudRun)
}
if d.resourceAttributes.FaasName.Enabled {
b.add(conventions.AttributeFaaSName, d.detector.FaaSName)
}
if d.resourceAttributes.FaasVersion.Enabled {
b.add(conventions.AttributeFaaSVersion, d.detector.FaaSVersion)
}
if d.resourceAttributes.FaasID.Enabled {
b.add(conventions.AttributeFaaSID, d.detector.FaaSID)
}
if d.resourceAttributes.CloudRegion.Enabled {
b.add(conventions.AttributeCloudRegion, d.detector.FaaSCloudRegion)
}
case gcp.CloudFunctions:
if d.resourceAttributes.CloudPlatform.Enabled {
b.attrs.PutStr(conventions.AttributeCloudPlatform, conventions.AttributeCloudPlatformGCPCloudFunctions)
}
if d.resourceAttributes.FaasName.Enabled {
b.add(conventions.AttributeFaaSName, d.detector.FaaSName)
}
if d.resourceAttributes.FaasVersion.Enabled {
b.add(conventions.AttributeFaaSVersion, d.detector.FaaSVersion)
}
if d.resourceAttributes.FaasID.Enabled {
b.add(conventions.AttributeFaaSID, d.detector.FaaSID)
}
if d.resourceAttributes.CloudRegion.Enabled {
b.add(conventions.AttributeCloudRegion, d.detector.FaaSCloudRegion)
}
case gcp.AppEngineFlex:
if d.resourceAttributes.CloudPlatform.Enabled {
b.attrs.PutStr(conventions.AttributeCloudPlatform, conventions.AttributeCloudPlatformGCPAppEngine)
}
b.addZoneAndRegion(d.detector.AppEngineFlexAvailabilityZoneAndRegion, d.resourceAttributes)
if d.resourceAttributes.FaasName.Enabled {
b.add(conventions.AttributeFaaSName, d.detector.AppEngineServiceName)
}
if d.resourceAttributes.FaasVersion.Enabled {
b.add(conventions.AttributeFaaSVersion, d.detector.AppEngineServiceVersion)
}
if d.resourceAttributes.FaasID.Enabled {
b.add(conventions.AttributeFaaSID, d.detector.AppEngineServiceInstance)
}
case gcp.AppEngineStandard:
if d.resourceAttributes.CloudPlatform.Enabled {
b.attrs.PutStr(conventions.AttributeCloudPlatform, conventions.AttributeCloudPlatformGCPAppEngine)
}
if d.resourceAttributes.FaasName.Enabled {
b.add(conventions.AttributeFaaSName, d.detector.AppEngineServiceName)
}
if d.resourceAttributes.FaasVersion.Enabled {
b.add(conventions.AttributeFaaSVersion, d.detector.AppEngineServiceVersion)
}
if d.resourceAttributes.FaasID.Enabled {
b.add(conventions.AttributeFaaSID, d.detector.AppEngineServiceInstance)
}
if d.resourceAttributes.CloudAvailabilityZone.Enabled {
b.add(conventions.AttributeCloudAvailabilityZone, d.detector.AppEngineStandardAvailabilityZone)
}
if d.resourceAttributes.CloudRegion.Enabled {
b.add(conventions.AttributeCloudRegion, d.detector.AppEngineStandardCloudRegion)
}
case gcp.GCE:
if d.resourceAttributes.CloudPlatform.Enabled {
b.attrs.PutStr(conventions.AttributeCloudPlatform, conventions.AttributeCloudPlatformGCPComputeEngine)
}
b.addZoneAndRegion(d.detector.GCEAvailabilityZoneAndRegion, d.resourceAttributes)
if d.resourceAttributes.HostType.Enabled {
b.add(conventions.AttributeHostType, d.detector.GCEHostType)
}
if d.resourceAttributes.HostID.Enabled {
b.add(conventions.AttributeHostID, d.detector.GCEHostID)
}
if d.resourceAttributes.HostName.Enabled {
b.add(conventions.AttributeHostName, d.detector.GCEHostName)
}
default:
// We don't support this platform yet, so just return with what we have
}
return res, conventions.SchemaURL, multierr.Combine(b.errs...)
}
// resourceBuilder simplifies constructing resources using GCP detection
// library functions.
type resourceBuilder struct {
logger *zap.Logger
errs []error
attrs pcommon.Map
}
func (r *resourceBuilder) add(key string, detect func() (string, error)) {
v, err := detect()
if err != nil {
r.errs = append(r.errs, err)
return
}
r.attrs.PutStr(key, v)
}
// addFallible adds a detect function whose failures should be ignored
func (r *resourceBuilder) addFallible(key string, detect func() (string, error)) {
v, err := detect()
if err != nil {
r.logger.Info("Fallible detector failed. This attribute will not be available.", zap.String("key", key), zap.Error(err))
return
}
r.attrs.PutStr(key, v)
}
// zoneAndRegion functions are expected to return zone, region, err.
func (r *resourceBuilder) addZoneAndRegion(detect func() (string, string, error), resourceAttributes localMetadata.ResourceAttributesConfig) {
zone, region, err := detect()
if err != nil {
r.errs = append(r.errs, err)
return
}
if resourceAttributes.CloudAvailabilityZone.Enabled {
r.attrs.PutStr(conventions.AttributeCloudAvailabilityZone, zone)
}
if resourceAttributes.CloudRegion.Enabled {
r.attrs.PutStr(conventions.AttributeCloudRegion, region)
}
}
func (r *resourceBuilder) addZoneOrRegion(detect func() (string, gcp.LocationType, error), resourceAttributes localMetadata.ResourceAttributesConfig) {
v, locType, err := detect()
if err != nil {
r.errs = append(r.errs, err)
return
}
switch locType {
case gcp.Zone:
if resourceAttributes.CloudAvailabilityZone.Enabled {
r.attrs.PutStr(conventions.AttributeCloudAvailabilityZone, v)
}
case gcp.Region:
if resourceAttributes.CloudRegion.Enabled {
r.attrs.PutStr(conventions.AttributeCloudRegion, v)
}
default:
r.errs = append(r.errs, fmt.Errorf("location must be zone or region. Got %v", locType))
}
}