@@ -24,6 +24,7 @@ import (
24
24
"go.opentelemetry.io/collector/component/componenttest"
25
25
"go.opentelemetry.io/collector/pdata/pcommon"
26
26
conventions "go.opentelemetry.io/collector/semconv/v1.6.1"
27
+ "go.uber.org/zap"
27
28
28
29
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/resourcedetectionprocessor/internal"
29
30
)
@@ -40,31 +41,54 @@ func TestDetect(t *testing.T) {
40
41
}{
41
42
{
42
43
desc : "zonal GKE cluster" ,
43
- detector : & detector { detector : & fakeGCPDetector {
44
+ detector : newTestDetector ( & fakeGCPDetector {
44
45
projectID : "my-project" ,
45
46
cloudPlatform : gcp .GKE ,
47
+ gceHostName : "my-gke-node-1234" ,
46
48
gkeHostID : "1472385723456792345" ,
47
49
gkeClusterName : "my-cluster" ,
48
50
gkeAvailabilityZone : "us-central1-c" ,
49
- }} ,
51
+ }) ,
50
52
expectedResource : internal .NewResource (map [string ]interface {}{
51
53
conventions .AttributeCloudProvider : conventions .AttributeCloudProviderGCP ,
52
54
conventions .AttributeCloudAccountID : "my-project" ,
53
55
conventions .AttributeCloudPlatform : conventions .AttributeCloudPlatformGCPKubernetesEngine ,
54
56
conventions .AttributeK8SClusterName : "my-cluster" ,
55
57
conventions .AttributeCloudAvailabilityZone : "us-central1-c" ,
56
58
conventions .AttributeHostID : "1472385723456792345" ,
59
+ conventions .AttributeHostName : "my-gke-node-1234" ,
57
60
}),
58
61
},
59
62
{
60
63
desc : "regional GKE cluster" ,
61
- detector : & detector {detector : & fakeGCPDetector {
64
+ detector : newTestDetector (& fakeGCPDetector {
65
+ projectID : "my-project" ,
66
+ cloudPlatform : gcp .GKE ,
67
+ gceHostName : "my-gke-node-1234" ,
68
+ gkeHostID : "1472385723456792345" ,
69
+ gkeClusterName : "my-cluster" ,
70
+ gkeRegion : "us-central1" ,
71
+ }),
72
+ expectedResource : internal .NewResource (map [string ]interface {}{
73
+ conventions .AttributeCloudProvider : conventions .AttributeCloudProviderGCP ,
74
+ conventions .AttributeCloudAccountID : "my-project" ,
75
+ conventions .AttributeCloudPlatform : conventions .AttributeCloudPlatformGCPKubernetesEngine ,
76
+ conventions .AttributeK8SClusterName : "my-cluster" ,
77
+ conventions .AttributeCloudRegion : "us-central1" ,
78
+ conventions .AttributeHostID : "1472385723456792345" ,
79
+ conventions .AttributeHostName : "my-gke-node-1234" ,
80
+ }),
81
+ },
82
+ {
83
+ desc : "regional GKE cluster with workload identity" ,
84
+ detector : newTestDetector (& fakeGCPDetector {
62
85
projectID : "my-project" ,
63
86
cloudPlatform : gcp .GKE ,
87
+ gceHostNameErr : fmt .Errorf ("metadata endpoint is concealed" ),
64
88
gkeHostID : "1472385723456792345" ,
65
89
gkeClusterName : "my-cluster" ,
66
90
gkeRegion : "us-central1" ,
67
- }} ,
91
+ }) ,
68
92
expectedResource : internal .NewResource (map [string ]interface {}{
69
93
conventions .AttributeCloudProvider : conventions .AttributeCloudProviderGCP ,
70
94
conventions .AttributeCloudAccountID : "my-project" ,
@@ -76,15 +100,15 @@ func TestDetect(t *testing.T) {
76
100
},
77
101
{
78
102
desc : "GCE" ,
79
- detector : & detector { detector : & fakeGCPDetector {
103
+ detector : newTestDetector ( & fakeGCPDetector {
80
104
projectID : "my-project" ,
81
105
cloudPlatform : gcp .GCE ,
82
106
gceHostID : "1472385723456792345" ,
83
107
gceHostName : "my-gke-node-1234" ,
84
108
gceHostType : "n1-standard1" ,
85
109
gceAvailabilityZone : "us-central1-c" ,
86
110
gceRegion : "us-central1" ,
87
- }} ,
111
+ }) ,
88
112
expectedResource : internal .NewResource (map [string ]interface {}{
89
113
conventions .AttributeCloudProvider : conventions .AttributeCloudProviderGCP ,
90
114
conventions .AttributeCloudAccountID : "my-project" ,
@@ -98,14 +122,14 @@ func TestDetect(t *testing.T) {
98
122
},
99
123
{
100
124
desc : "Cloud Run" ,
101
- detector : & detector { detector : & fakeGCPDetector {
125
+ detector : newTestDetector ( & fakeGCPDetector {
102
126
projectID : "my-project" ,
103
127
cloudPlatform : gcp .CloudRun ,
104
128
faaSID : "1472385723456792345" ,
105
129
faaSCloudRegion : "us-central1" ,
106
130
faaSName : "my-service" ,
107
131
faaSVersion : "123456" ,
108
- }} ,
132
+ }) ,
109
133
expectedResource : internal .NewResource (map [string ]interface {}{
110
134
conventions .AttributeCloudProvider : conventions .AttributeCloudProviderGCP ,
111
135
conventions .AttributeCloudAccountID : "my-project" ,
@@ -118,14 +142,14 @@ func TestDetect(t *testing.T) {
118
142
},
119
143
{
120
144
desc : "Cloud Functions" ,
121
- detector : & detector { detector : & fakeGCPDetector {
145
+ detector : newTestDetector ( & fakeGCPDetector {
122
146
projectID : "my-project" ,
123
147
cloudPlatform : gcp .CloudFunctions ,
124
148
faaSID : "1472385723456792345" ,
125
149
faaSCloudRegion : "us-central1" ,
126
150
faaSName : "my-service" ,
127
151
faaSVersion : "123456" ,
128
- }} ,
152
+ }) ,
129
153
expectedResource : internal .NewResource (map [string ]interface {}{
130
154
conventions .AttributeCloudProvider : conventions .AttributeCloudProviderGCP ,
131
155
conventions .AttributeCloudAccountID : "my-project" ,
@@ -138,15 +162,15 @@ func TestDetect(t *testing.T) {
138
162
},
139
163
{
140
164
desc : "App Engine Standard" ,
141
- detector : & detector { detector : & fakeGCPDetector {
165
+ detector : newTestDetector ( & fakeGCPDetector {
142
166
projectID : "my-project" ,
143
167
cloudPlatform : gcp .AppEngineStandard ,
144
168
appEngineServiceInstance : "1472385723456792345" ,
145
169
appEngineAvailabilityZone : "us-central1-c" ,
146
170
appEngineRegion : "us-central1" ,
147
171
appEngineServiceName : "my-service" ,
148
172
appEngineServiceVersion : "123456" ,
149
- }} ,
173
+ }) ,
150
174
expectedResource : internal .NewResource (map [string ]interface {}{
151
175
conventions .AttributeCloudProvider : conventions .AttributeCloudProviderGCP ,
152
176
conventions .AttributeCloudAccountID : "my-project" ,
@@ -160,15 +184,15 @@ func TestDetect(t *testing.T) {
160
184
},
161
185
{
162
186
desc : "App Engine Flex" ,
163
- detector : & detector { detector : & fakeGCPDetector {
187
+ detector : newTestDetector ( & fakeGCPDetector {
164
188
projectID : "my-project" ,
165
189
cloudPlatform : gcp .AppEngineFlex ,
166
190
appEngineServiceInstance : "1472385723456792345" ,
167
191
appEngineAvailabilityZone : "us-central1-c" ,
168
192
appEngineRegion : "us-central1" ,
169
193
appEngineServiceName : "my-service" ,
170
194
appEngineServiceVersion : "123456" ,
171
- }} ,
195
+ }) ,
172
196
expectedResource : internal .NewResource (map [string ]interface {}{
173
197
conventions .AttributeCloudProvider : conventions .AttributeCloudProviderGCP ,
174
198
conventions .AttributeCloudAccountID : "my-project" ,
@@ -182,20 +206,20 @@ func TestDetect(t *testing.T) {
182
206
},
183
207
{
184
208
desc : "Unknown Platform" ,
185
- detector : & detector { detector : & fakeGCPDetector {
209
+ detector : newTestDetector ( & fakeGCPDetector {
186
210
projectID : "my-project" ,
187
211
cloudPlatform : gcp .UnknownPlatform ,
188
- }} ,
212
+ }) ,
189
213
expectedResource : internal .NewResource (map [string ]interface {}{
190
214
conventions .AttributeCloudProvider : conventions .AttributeCloudProviderGCP ,
191
215
conventions .AttributeCloudAccountID : "my-project" ,
192
216
}),
193
217
},
194
218
{
195
219
desc : "error" ,
196
- detector : & detector { detector : & fakeGCPDetector {
220
+ detector : newTestDetector ( & fakeGCPDetector {
197
221
err : fmt .Errorf ("failed to get metadata" ),
198
- }} ,
222
+ }) ,
199
223
expectErr : true ,
200
224
expectedResource : internal .NewResource (map [string ]interface {}{
201
225
conventions .AttributeCloudProvider : conventions .AttributeCloudProviderGCP ,
@@ -217,6 +241,13 @@ func TestDetect(t *testing.T) {
217
241
}
218
242
}
219
243
244
+ func newTestDetector (gcpDetector * fakeGCPDetector ) * detector {
245
+ return & detector {
246
+ logger : zap .NewNop (),
247
+ detector : gcpDetector ,
248
+ }
249
+ }
250
+
220
251
// fakeGCPDetector implements gcpDetector and uses fake values.
221
252
type fakeGCPDetector struct {
222
253
err error
@@ -240,6 +271,7 @@ type fakeGCPDetector struct {
240
271
gceHostType string
241
272
gceHostID string
242
273
gceHostName string
274
+ gceHostNameErr error
243
275
}
244
276
245
277
func (f * fakeGCPDetector ) ProjectID () (string , error ) {
@@ -372,7 +404,7 @@ func (f *fakeGCPDetector) GCEHostName() (string, error) {
372
404
if f .err != nil {
373
405
return "" , f .err
374
406
}
375
- return f .gceHostName , nil
407
+ return f .gceHostName , f . gceHostNameErr
376
408
}
377
409
378
410
func TestDeduplicateDetectors (t * testing.T ) {
0 commit comments