API Server: Include traceID in HTTP request logs (#88179)
This commit is contained in:
@@ -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)
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user