-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
gcp.go
162 lines (147 loc) · 6.26 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
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
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"
)
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, _ internal.DetectorConfig) (internal.Detector, error) {
return &detector{
logger: set.Logger,
detector: gcp.NewDetector(),
}, nil
}
type detector struct {
logger *zap.Logger
detector gcpDetector
}
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()}
b.attrs.PutStr(conventions.AttributeCloudProvider, conventions.AttributeCloudProviderGCP)
b.add(conventions.AttributeCloudAccountID, d.detector.ProjectID)
switch d.detector.CloudPlatform() {
case gcp.GKE:
b.attrs.PutStr(conventions.AttributeCloudPlatform, conventions.AttributeCloudPlatformGCPKubernetesEngine)
b.addZoneOrRegion(d.detector.GKEAvailabilityZoneOrRegion)
b.add(conventions.AttributeK8SClusterName, d.detector.GKEClusterName)
b.add(conventions.AttributeHostID, d.detector.GKEHostID)
// GCEHostname is fallible on GKE, since it's not available when using workload identity.
b.addFallible(conventions.AttributeHostName, d.detector.GCEHostName)
case gcp.CloudRun:
b.attrs.PutStr(conventions.AttributeCloudPlatform, conventions.AttributeCloudPlatformGCPCloudRun)
b.add(conventions.AttributeFaaSName, d.detector.FaaSName)
b.add(conventions.AttributeFaaSVersion, d.detector.FaaSVersion)
b.add(conventions.AttributeFaaSID, d.detector.FaaSID)
b.add(conventions.AttributeCloudRegion, d.detector.FaaSCloudRegion)
case gcp.CloudFunctions:
b.attrs.PutStr(conventions.AttributeCloudPlatform, conventions.AttributeCloudPlatformGCPCloudFunctions)
b.add(conventions.AttributeFaaSName, d.detector.FaaSName)
b.add(conventions.AttributeFaaSVersion, d.detector.FaaSVersion)
b.add(conventions.AttributeFaaSID, d.detector.FaaSID)
b.add(conventions.AttributeCloudRegion, d.detector.FaaSCloudRegion)
case gcp.AppEngineFlex:
b.attrs.PutStr(conventions.AttributeCloudPlatform, conventions.AttributeCloudPlatformGCPAppEngine)
b.addZoneAndRegion(d.detector.AppEngineFlexAvailabilityZoneAndRegion)
b.add(conventions.AttributeFaaSName, d.detector.AppEngineServiceName)
b.add(conventions.AttributeFaaSVersion, d.detector.AppEngineServiceVersion)
b.add(conventions.AttributeFaaSID, d.detector.AppEngineServiceInstance)
case gcp.AppEngineStandard:
b.attrs.PutStr(conventions.AttributeCloudPlatform, conventions.AttributeCloudPlatformGCPAppEngine)
b.add(conventions.AttributeFaaSName, d.detector.AppEngineServiceName)
b.add(conventions.AttributeFaaSVersion, d.detector.AppEngineServiceVersion)
b.add(conventions.AttributeFaaSID, d.detector.AppEngineServiceInstance)
b.add(conventions.AttributeCloudAvailabilityZone, d.detector.AppEngineStandardAvailabilityZone)
b.add(conventions.AttributeCloudRegion, d.detector.AppEngineStandardCloudRegion)
case gcp.GCE:
b.attrs.PutStr(conventions.AttributeCloudPlatform, conventions.AttributeCloudPlatformGCPComputeEngine)
b.addZoneAndRegion(d.detector.GCEAvailabilityZoneAndRegion)
b.add(conventions.AttributeHostType, d.detector.GCEHostType)
b.add(conventions.AttributeHostID, d.detector.GCEHostID)
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)) {
zone, region, err := detect()
if err != nil {
r.errs = append(r.errs, err)
return
}
r.attrs.PutStr(conventions.AttributeCloudAvailabilityZone, zone)
r.attrs.PutStr(conventions.AttributeCloudRegion, region)
}
func (r *resourceBuilder) addZoneOrRegion(detect func() (string, gcp.LocationType, error)) {
v, locType, err := detect()
if err != nil {
r.errs = append(r.errs, err)
return
}
switch locType {
case gcp.Zone:
r.attrs.PutStr(conventions.AttributeCloudAvailabilityZone, v)
case gcp.Region:
r.attrs.PutStr(conventions.AttributeCloudRegion, v)
default:
r.errs = append(r.errs, fmt.Errorf("location must be zone or region. Got %v", locType))
}
}