Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(logging): Use instance/attributes/cluster-location for location on GKE #9094

Merged
merged 6 commits into from Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
50 changes: 25 additions & 25 deletions logging/logging_test.go
Expand Up @@ -282,25 +282,25 @@ func TestToLogEntry(t *testing.T) {
tests := []struct {
name string
in logging.Entry
want logpb.LogEntry
want *logpb.LogEntry
wantError error
}{
{
name: "BlankLogEntry",
in: logging.Entry{},
want: logpb.LogEntry{},
want: &logpb.LogEntry{},
}, {
name: "Already set Trace",
in: logging.Entry{Trace: "t1"},
want: logpb.LogEntry{Trace: "t1"},
want: &logpb.LogEntry{Trace: "t1"},
}, {
name: "No X-Trace-Context header",
in: logging.Entry{
HTTPRequest: &logging.HTTPRequest{
Request: &http.Request{URL: u, Header: http.Header{"foo": {"bar"}}},
},
},
want: logpb.LogEntry{},
want: &logpb.LogEntry{},
}, {
name: "X-Trace-Context header with all fields",
in: logging.Entry{
Expand All @@ -312,7 +312,7 @@ func TestToLogEntry(t *testing.T) {
},
},
},
want: logpb.LogEntry{
want: &logpb.LogEntry{
Trace: "projects/P/traces/105445aa7843bc8bf206b120001000",
SpanId: "000000000000004a",
TraceSampled: true,
Expand All @@ -328,7 +328,7 @@ func TestToLogEntry(t *testing.T) {
},
},
},
want: logpb.LogEntry{
want: &logpb.LogEntry{
Trace: "projects/P/traces/105445aa7843bc8bf206b120001000",
SpanId: "000000000000004a",
TraceSampled: true,
Expand All @@ -343,7 +343,7 @@ func TestToLogEntry(t *testing.T) {
},
},
},
want: logpb.LogEntry{
want: &logpb.LogEntry{
Trace: "projects/P/traces/105445aa7843bc8bf206b120001000",
SpanId: "000000000000004a",
TraceSampled: true,
Expand All @@ -358,7 +358,7 @@ func TestToLogEntry(t *testing.T) {
},
},
},
want: logpb.LogEntry{},
want: &logpb.LogEntry{},
}, {
name: "X-Trace-Context header with blank span",
in: logging.Entry{
Expand All @@ -369,7 +369,7 @@ func TestToLogEntry(t *testing.T) {
},
},
},
want: logpb.LogEntry{
want: &logpb.LogEntry{
Trace: "projects/P/traces/105445aa7843bc8bf206b120001000",
},
}, {
Expand All @@ -382,7 +382,7 @@ func TestToLogEntry(t *testing.T) {
},
},
},
want: logpb.LogEntry{
want: &logpb.LogEntry{
Trace: "projects/P/traces/105445aa7843bc8bf206b120001000",
},
}, {
Expand All @@ -395,7 +395,7 @@ func TestToLogEntry(t *testing.T) {
},
},
},
want: logpb.LogEntry{},
want: &logpb.LogEntry{},
}, {
name: "Invalid X-Trace-Context header but already set TraceID",
in: logging.Entry{
Expand All @@ -407,13 +407,13 @@ func TestToLogEntry(t *testing.T) {
},
Trace: "t4",
},
want: logpb.LogEntry{
want: &logpb.LogEntry{
Trace: "t4",
},
}, {
name: "Already set TraceID and SpanID",
in: logging.Entry{Trace: "t1", SpanID: "007"},
want: logpb.LogEntry{
want: &logpb.LogEntry{
Trace: "t1",
SpanId: "007",
},
Expand All @@ -436,7 +436,7 @@ func TestToLogEntry(t *testing.T) {
},
},
},
want: logpb.LogEntry{
want: &logpb.LogEntry{
Trace: "projects/P/traces/105445aa7843bc8bf206b12000100012",
SpanId: "000000000000004a",
},
Expand All @@ -452,7 +452,7 @@ func TestToLogEntry(t *testing.T) {
},
},
},
want: logpb.LogEntry{
want: &logpb.LogEntry{
Trace: "projects/P/traces/105445aa7843bc8bf206b12000100012",
SpanId: "000000000000004a",
TraceSampled: true,
Expand All @@ -470,7 +470,7 @@ func TestToLogEntry(t *testing.T) {
},
},
},
want: logpb.LogEntry{
want: &logpb.LogEntry{
Trace: "projects/P/traces/105445aa7843bc8bf206b1200010aaaa",
SpanId: "0000000000000aaa",
},
Expand All @@ -485,7 +485,7 @@ func TestToLogEntry(t *testing.T) {
},
},
},
want: logpb.LogEntry{},
want: &logpb.LogEntry{},
},
{
name: "Traceparent header short trace field",
Expand All @@ -497,7 +497,7 @@ func TestToLogEntry(t *testing.T) {
},
},
},
want: logpb.LogEntry{},
want: &logpb.LogEntry{},
},
{
name: "Traceparent header long trace field",
Expand All @@ -509,7 +509,7 @@ func TestToLogEntry(t *testing.T) {
},
},
},
want: logpb.LogEntry{},
want: &logpb.LogEntry{},
},
{
name: "Traceparent header invalid trace field",
Expand All @@ -521,7 +521,7 @@ func TestToLogEntry(t *testing.T) {
},
},
},
want: logpb.LogEntry{},
want: &logpb.LogEntry{},
},
{
name: "Traceparent header trace field all 0s",
Expand All @@ -533,7 +533,7 @@ func TestToLogEntry(t *testing.T) {
},
},
},
want: logpb.LogEntry{},
want: &logpb.LogEntry{},
},
{
name: "Traceparent header short span field",
Expand All @@ -545,7 +545,7 @@ func TestToLogEntry(t *testing.T) {
},
},
},
want: logpb.LogEntry{},
want: &logpb.LogEntry{},
},
{
name: "Traceparent header long span field",
Expand All @@ -557,7 +557,7 @@ func TestToLogEntry(t *testing.T) {
},
},
},
want: logpb.LogEntry{},
want: &logpb.LogEntry{},
},
{
name: "Traceparent header invalid span field",
Expand All @@ -569,7 +569,7 @@ func TestToLogEntry(t *testing.T) {
},
},
},
want: logpb.LogEntry{},
want: &logpb.LogEntry{},
},
{
name: "Traceparent header span field all 0s",
Expand All @@ -581,7 +581,7 @@ func TestToLogEntry(t *testing.T) {
},
},
},
want: logpb.LogEntry{},
want: &logpb.LogEntry{},
},
}
for _, test := range tests {
Expand Down
4 changes: 2 additions & 2 deletions logging/resource.go
Expand Up @@ -201,8 +201,8 @@ func detectKubernetesResource() *mrpb.MonitoredResource {
if projectID == "" {
return nil
}
zone := detectedResource.metadataZone()
clusterName := detectedResource.attrs.Metadata("instance/attributes/cluster-name")
clusterLocation := detectedResource.attrs.Metadata("instance/attributes/cluster-location")
namespaceName := detectedResource.attrs.ReadAll("/var/run/secrets/kubernetes.io/serviceaccount/namespace")
if namespaceName == "" {
// if automountServiceAccountToken is disabled allow to customize
Expand All @@ -217,7 +217,7 @@ func detectKubernetesResource() *mrpb.MonitoredResource {
Type: "k8s_container",
Labels: map[string]string{
"cluster_name": clusterName,
"location": zone,
"location": clusterLocation,
"project_id": projectID,
"pod_name": podName,
"namespace_name": namespaceName,
Expand Down
23 changes: 20 additions & 3 deletions logging/resource_test.go
Expand Up @@ -152,9 +152,9 @@ func TestResourceDetection(t *testing.T) {
},
},
{
name: "detect GKE resource",
name: "detect GKE resource for a zonal cluster",
envVars: map[string]string{"HOSTNAME": podName},
metaVars: map[string]string{"": there, "project/project-id": projectID, "instance/zone": qualifiedZoneName, "instance/attributes/cluster-name": clusterName},
metaVars: map[string]string{"": there, "project/project-id": projectID, "instance/attributes/cluster-location": zoneID, "instance/attributes/cluster-name": clusterName},
fsPaths: map[string]string{"/var/run/secrets/kubernetes.io/serviceaccount/namespace": namespaceName},
want: &mrpb.MonitoredResource{
Type: "k8s_container",
Expand All @@ -168,10 +168,27 @@ func TestResourceDetection(t *testing.T) {
},
},
},
{
name: "detect GKE resource for a regional cluster",
envVars: map[string]string{"HOSTNAME": podName},
metaVars: map[string]string{"": there, "project/project-id": projectID, "instance/attributes/cluster-location": regionID, "instance/attributes/cluster-name": clusterName},
fsPaths: map[string]string{"/var/run/secrets/kubernetes.io/serviceaccount/namespace": namespaceName},
want: &mrpb.MonitoredResource{
Type: "k8s_container",
Labels: map[string]string{
"cluster_name": clusterName,
"location": regionID,
"project_id": projectID,
"pod_name": podName,
"namespace_name": namespaceName,
"container_name": "",
},
},
},
{
name: "detect GKE resource with custom container and namespace config",
envVars: map[string]string{"HOSTNAME": podName, "CONTAINER_NAME": containerName, "NAMESPACE_NAME": namespaceName},
metaVars: map[string]string{"": there, "project/project-id": projectID, "instance/zone": qualifiedZoneName, "instance/attributes/cluster-name": clusterName},
metaVars: map[string]string{"": there, "project/project-id": projectID, "instance/attributes/cluster-location": zoneID, "instance/attributes/cluster-name": clusterName},
want: &mrpb.MonitoredResource{
Type: "k8s_container",
Labels: map[string]string{
Expand Down