mirror of
https://github.com/k3s-io/kubernetes.git
synced 2026-07-18 04:54:54 +00:00
Merge pull request #127322 from vrutkovs/tracing-set-audit-id
tracing: set audit-id on incoming requests
This commit is contained in:
@@ -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.
|
||||
|
||||
@@ -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() != ""
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user