API Server: Include traceID in HTTP request logs (#88179)

This commit is contained in:
Marcus Efraimsson
2024-05-22 17:56:34 +02:00
committed by GitHub
parent 10f600a6e9
commit 8ea0b336d5
4 changed files with 40 additions and 37 deletions
@@ -0,0 +1,26 @@
package filters
import (
"net/http"
"go.opentelemetry.io/otel/trace"
"k8s.io/apiserver/pkg/server/httplog"
)
// WithTracingHTTPLoggingAttributes adds tracing attributes to HTTP request logs.
func WithTracingHTTPLoggingAttributes(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
spanCtx := trace.SpanContextFromContext(r.Context())
if spanCtx.IsValid() {
if spanCtx.HasTraceID() {
httplog.AddKeyValue(r.Context(), "traceID", spanCtx.TraceID().String())
}
if spanCtx.HasSpanID() {
httplog.AddKeyValue(r.Context(), "spanID", spanCtx.SpanID().String())
}
}
handler.ServeHTTP(w, r)
})
}