diff --git a/staging/src/k8s.io/apiserver/pkg/endpoints/filters/traces.go b/staging/src/k8s.io/apiserver/pkg/endpoints/filters/traces.go index a82edd45690..9b97410b43d 100644 --- a/staging/src/k8s.io/apiserver/pkg/endpoints/filters/traces.go +++ b/staging/src/k8s.io/apiserver/pkg/endpoints/filters/traces.go @@ -20,8 +20,10 @@ import ( "net/http" "go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp" + "go.opentelemetry.io/otel/attribute" semconv "go.opentelemetry.io/otel/semconv/v1.17.0" "go.opentelemetry.io/otel/trace" + "k8s.io/apiserver/pkg/audit" "k8s.io/apiserver/pkg/endpoints/request" "k8s.io/apiserver/pkg/authentication/user" @@ -51,9 +53,12 @@ func WithTracing(handler http.Handler, tp trace.TracerProvider) http.Handler { } // Add the http.target attribute to the otelhttp span // Workaround for https://github.com/open-telemetry/opentelemetry-go-contrib/issues/3743 + span := trace.SpanFromContext(r.Context()) if r.URL != nil { - trace.SpanFromContext(r.Context()).SetAttributes(semconv.HTTPTarget(r.URL.RequestURI())) + span.SetAttributes(semconv.HTTPTarget(r.URL.RequestURI())) } + // Add auditID attribute if available. This helps us connect traces to audit log entries + span.SetAttributes(attribute.String("audit-id", audit.GetAuditIDTruncated(r.Context()))) handler.ServeHTTP(w, r) }) // With Noop TracerProvider, the otelhttp still handles context propagation. diff --git a/test/integration/apiserver/tracing/tracing_test.go b/test/integration/apiserver/tracing/tracing_test.go index 577cf5c3c01..59db06e9986 100644 --- a/test/integration/apiserver/tracing/tracing_test.go +++ b/test/integration/apiserver/tracing/tracing_test.go @@ -397,6 +397,9 @@ endpoint: %s`, listener.Addr().String())), os.FileMode(0755)); err != nil { "http.method": func(v *commonv1.AnyValue) bool { return v.GetStringValue() == "POST" }, + "audit-id": func(v *commonv1.AnyValue) bool { + return v.GetStringValue() != "" + }, }, }, { @@ -513,6 +516,9 @@ endpoint: %s`, listener.Addr().String())), os.FileMode(0755)); err != nil { "http.method": func(v *commonv1.AnyValue) bool { return v.GetStringValue() == "GET" }, + "audit-id": func(v *commonv1.AnyValue) bool { + return v.GetStringValue() != "" + }, }, }, { @@ -617,6 +623,9 @@ endpoint: %s`, listener.Addr().String())), os.FileMode(0755)); err != nil { "http.method": func(v *commonv1.AnyValue) bool { return v.GetStringValue() == "GET" }, + "audit-id": func(v *commonv1.AnyValue) bool { + return v.GetStringValue() != "" + }, }, }, { @@ -709,6 +718,9 @@ endpoint: %s`, listener.Addr().String())), os.FileMode(0755)); err != nil { "http.method": func(v *commonv1.AnyValue) bool { return v.GetStringValue() == "PUT" }, + "audit-id": func(v *commonv1.AnyValue) bool { + return v.GetStringValue() != "" + }, }, }, { @@ -850,6 +862,9 @@ endpoint: %s`, listener.Addr().String())), os.FileMode(0755)); err != nil { "http.method": func(v *commonv1.AnyValue) bool { return v.GetStringValue() == "PATCH" }, + "audit-id": func(v *commonv1.AnyValue) bool { + return v.GetStringValue() != "" + }, }, }, { @@ -968,6 +983,9 @@ endpoint: %s`, listener.Addr().String())), os.FileMode(0755)); err != nil { "http.method": func(v *commonv1.AnyValue) bool { return v.GetStringValue() == "DELETE" }, + "audit-id": func(v *commonv1.AnyValue) bool { + return v.GetStringValue() != "" + }, }, }, {