Skip to content

Commit c85b9d4

Browse files
fix(logging): Use instance/attributes/cluster-location for location on GKE (#9094)
* fix(logging): Use instance/attributes/cluster-location for location on GKE. * Fixed `go vet` errors --------- Co-authored-by: Cindy Peng <148148319+cindy-peng@users.noreply.github.com>
1 parent 0538be4 commit c85b9d4

File tree

3 files changed

+47
-30
lines changed

3 files changed

+47
-30
lines changed

logging/logging_test.go

+25-25
Original file line numberDiff line numberDiff line change
@@ -282,25 +282,25 @@ func TestToLogEntry(t *testing.T) {
282282
tests := []struct {
283283
name string
284284
in logging.Entry
285-
want logpb.LogEntry
285+
want *logpb.LogEntry
286286
wantError error
287287
}{
288288
{
289289
name: "BlankLogEntry",
290290
in: logging.Entry{},
291-
want: logpb.LogEntry{},
291+
want: &logpb.LogEntry{},
292292
}, {
293293
name: "Already set Trace",
294294
in: logging.Entry{Trace: "t1"},
295-
want: logpb.LogEntry{Trace: "t1"},
295+
want: &logpb.LogEntry{Trace: "t1"},
296296
}, {
297297
name: "No X-Trace-Context header",
298298
in: logging.Entry{
299299
HTTPRequest: &logging.HTTPRequest{
300300
Request: &http.Request{URL: u, Header: http.Header{"foo": {"bar"}}},
301301
},
302302
},
303-
want: logpb.LogEntry{},
303+
want: &logpb.LogEntry{},
304304
}, {
305305
name: "X-Trace-Context header with all fields",
306306
in: logging.Entry{
@@ -312,7 +312,7 @@ func TestToLogEntry(t *testing.T) {
312312
},
313313
},
314314
},
315-
want: logpb.LogEntry{
315+
want: &logpb.LogEntry{
316316
Trace: "projects/P/traces/105445aa7843bc8bf206b120001000",
317317
SpanId: "000000000000004a",
318318
TraceSampled: true,
@@ -328,7 +328,7 @@ func TestToLogEntry(t *testing.T) {
328328
},
329329
},
330330
},
331-
want: logpb.LogEntry{
331+
want: &logpb.LogEntry{
332332
Trace: "projects/P/traces/105445aa7843bc8bf206b120001000",
333333
SpanId: "000000000000004a",
334334
TraceSampled: true,
@@ -343,7 +343,7 @@ func TestToLogEntry(t *testing.T) {
343343
},
344344
},
345345
},
346-
want: logpb.LogEntry{
346+
want: &logpb.LogEntry{
347347
Trace: "projects/P/traces/105445aa7843bc8bf206b120001000",
348348
SpanId: "000000000000004a",
349349
TraceSampled: true,
@@ -358,7 +358,7 @@ func TestToLogEntry(t *testing.T) {
358358
},
359359
},
360360
},
361-
want: logpb.LogEntry{},
361+
want: &logpb.LogEntry{},
362362
}, {
363363
name: "X-Trace-Context header with blank span",
364364
in: logging.Entry{
@@ -369,7 +369,7 @@ func TestToLogEntry(t *testing.T) {
369369
},
370370
},
371371
},
372-
want: logpb.LogEntry{
372+
want: &logpb.LogEntry{
373373
Trace: "projects/P/traces/105445aa7843bc8bf206b120001000",
374374
},
375375
}, {
@@ -382,7 +382,7 @@ func TestToLogEntry(t *testing.T) {
382382
},
383383
},
384384
},
385-
want: logpb.LogEntry{
385+
want: &logpb.LogEntry{
386386
Trace: "projects/P/traces/105445aa7843bc8bf206b120001000",
387387
},
388388
}, {
@@ -395,7 +395,7 @@ func TestToLogEntry(t *testing.T) {
395395
},
396396
},
397397
},
398-
want: logpb.LogEntry{},
398+
want: &logpb.LogEntry{},
399399
}, {
400400
name: "Invalid X-Trace-Context header but already set TraceID",
401401
in: logging.Entry{
@@ -407,13 +407,13 @@ func TestToLogEntry(t *testing.T) {
407407
},
408408
Trace: "t4",
409409
},
410-
want: logpb.LogEntry{
410+
want: &logpb.LogEntry{
411411
Trace: "t4",
412412
},
413413
}, {
414414
name: "Already set TraceID and SpanID",
415415
in: logging.Entry{Trace: "t1", SpanID: "007"},
416-
want: logpb.LogEntry{
416+
want: &logpb.LogEntry{
417417
Trace: "t1",
418418
SpanId: "007",
419419
},
@@ -436,7 +436,7 @@ func TestToLogEntry(t *testing.T) {
436436
},
437437
},
438438
},
439-
want: logpb.LogEntry{
439+
want: &logpb.LogEntry{
440440
Trace: "projects/P/traces/105445aa7843bc8bf206b12000100012",
441441
SpanId: "000000000000004a",
442442
},
@@ -452,7 +452,7 @@ func TestToLogEntry(t *testing.T) {
452452
},
453453
},
454454
},
455-
want: logpb.LogEntry{
455+
want: &logpb.LogEntry{
456456
Trace: "projects/P/traces/105445aa7843bc8bf206b12000100012",
457457
SpanId: "000000000000004a",
458458
TraceSampled: true,
@@ -470,7 +470,7 @@ func TestToLogEntry(t *testing.T) {
470470
},
471471
},
472472
},
473-
want: logpb.LogEntry{
473+
want: &logpb.LogEntry{
474474
Trace: "projects/P/traces/105445aa7843bc8bf206b1200010aaaa",
475475
SpanId: "0000000000000aaa",
476476
},
@@ -485,7 +485,7 @@ func TestToLogEntry(t *testing.T) {
485485
},
486486
},
487487
},
488-
want: logpb.LogEntry{},
488+
want: &logpb.LogEntry{},
489489
},
490490
{
491491
name: "Traceparent header short trace field",
@@ -497,7 +497,7 @@ func TestToLogEntry(t *testing.T) {
497497
},
498498
},
499499
},
500-
want: logpb.LogEntry{},
500+
want: &logpb.LogEntry{},
501501
},
502502
{
503503
name: "Traceparent header long trace field",
@@ -509,7 +509,7 @@ func TestToLogEntry(t *testing.T) {
509509
},
510510
},
511511
},
512-
want: logpb.LogEntry{},
512+
want: &logpb.LogEntry{},
513513
},
514514
{
515515
name: "Traceparent header invalid trace field",
@@ -521,7 +521,7 @@ func TestToLogEntry(t *testing.T) {
521521
},
522522
},
523523
},
524-
want: logpb.LogEntry{},
524+
want: &logpb.LogEntry{},
525525
},
526526
{
527527
name: "Traceparent header trace field all 0s",
@@ -533,7 +533,7 @@ func TestToLogEntry(t *testing.T) {
533533
},
534534
},
535535
},
536-
want: logpb.LogEntry{},
536+
want: &logpb.LogEntry{},
537537
},
538538
{
539539
name: "Traceparent header short span field",
@@ -545,7 +545,7 @@ func TestToLogEntry(t *testing.T) {
545545
},
546546
},
547547
},
548-
want: logpb.LogEntry{},
548+
want: &logpb.LogEntry{},
549549
},
550550
{
551551
name: "Traceparent header long span field",
@@ -557,7 +557,7 @@ func TestToLogEntry(t *testing.T) {
557557
},
558558
},
559559
},
560-
want: logpb.LogEntry{},
560+
want: &logpb.LogEntry{},
561561
},
562562
{
563563
name: "Traceparent header invalid span field",
@@ -569,7 +569,7 @@ func TestToLogEntry(t *testing.T) {
569569
},
570570
},
571571
},
572-
want: logpb.LogEntry{},
572+
want: &logpb.LogEntry{},
573573
},
574574
{
575575
name: "Traceparent header span field all 0s",
@@ -581,7 +581,7 @@ func TestToLogEntry(t *testing.T) {
581581
},
582582
},
583583
},
584-
want: logpb.LogEntry{},
584+
want: &logpb.LogEntry{},
585585
},
586586
}
587587
for _, test := range tests {

logging/resource.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,8 @@ func detectKubernetesResource() *mrpb.MonitoredResource {
201201
if projectID == "" {
202202
return nil
203203
}
204-
zone := detectedResource.metadataZone()
205204
clusterName := detectedResource.attrs.Metadata("instance/attributes/cluster-name")
205+
clusterLocation := detectedResource.attrs.Metadata("instance/attributes/cluster-location")
206206
namespaceName := detectedResource.attrs.ReadAll("/var/run/secrets/kubernetes.io/serviceaccount/namespace")
207207
if namespaceName == "" {
208208
// if automountServiceAccountToken is disabled allow to customize
@@ -217,7 +217,7 @@ func detectKubernetesResource() *mrpb.MonitoredResource {
217217
Type: "k8s_container",
218218
Labels: map[string]string{
219219
"cluster_name": clusterName,
220-
"location": zone,
220+
"location": clusterLocation,
221221
"project_id": projectID,
222222
"pod_name": podName,
223223
"namespace_name": namespaceName,

logging/resource_test.go

+20-3
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,9 @@ func TestResourceDetection(t *testing.T) {
152152
},
153153
},
154154
{
155-
name: "detect GKE resource",
155+
name: "detect GKE resource for a zonal cluster",
156156
envVars: map[string]string{"HOSTNAME": podName},
157-
metaVars: map[string]string{"": there, "project/project-id": projectID, "instance/zone": qualifiedZoneName, "instance/attributes/cluster-name": clusterName},
157+
metaVars: map[string]string{"": there, "project/project-id": projectID, "instance/attributes/cluster-location": zoneID, "instance/attributes/cluster-name": clusterName},
158158
fsPaths: map[string]string{"/var/run/secrets/kubernetes.io/serviceaccount/namespace": namespaceName},
159159
want: &mrpb.MonitoredResource{
160160
Type: "k8s_container",
@@ -168,10 +168,27 @@ func TestResourceDetection(t *testing.T) {
168168
},
169169
},
170170
},
171+
{
172+
name: "detect GKE resource for a regional cluster",
173+
envVars: map[string]string{"HOSTNAME": podName},
174+
metaVars: map[string]string{"": there, "project/project-id": projectID, "instance/attributes/cluster-location": regionID, "instance/attributes/cluster-name": clusterName},
175+
fsPaths: map[string]string{"/var/run/secrets/kubernetes.io/serviceaccount/namespace": namespaceName},
176+
want: &mrpb.MonitoredResource{
177+
Type: "k8s_container",
178+
Labels: map[string]string{
179+
"cluster_name": clusterName,
180+
"location": regionID,
181+
"project_id": projectID,
182+
"pod_name": podName,
183+
"namespace_name": namespaceName,
184+
"container_name": "",
185+
},
186+
},
187+
},
171188
{
172189
name: "detect GKE resource with custom container and namespace config",
173190
envVars: map[string]string{"HOSTNAME": podName, "CONTAINER_NAME": containerName, "NAMESPACE_NAME": namespaceName},
174-
metaVars: map[string]string{"": there, "project/project-id": projectID, "instance/zone": qualifiedZoneName, "instance/attributes/cluster-name": clusterName},
191+
metaVars: map[string]string{"": there, "project/project-id": projectID, "instance/attributes/cluster-location": zoneID, "instance/attributes/cluster-name": clusterName},
175192
want: &mrpb.MonitoredResource{
176193
Type: "k8s_container",
177194
Labels: map[string]string{

0 commit comments

Comments
 (0)