Merge pull request #127322 from vrutkovs/tracing-set-audit-id

tracing: set audit-id on incoming requests
This commit is contained in:
Kubernetes Prow Robot
2025-07-13 17:58:23 -07:00
committed by GitHub
2 changed files with 24 additions and 1 deletions

View File

@@ -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.

View File

@@ -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() != ""
},
},
},
{