Skip to content

Commit 3842d74

Browse files
committed
restrict trace context propagation to system:master and system:monitoring
1 parent c5b83f7 commit 3842d74

File tree

4 files changed

+168
-99
lines changed

4 files changed

+168
-99
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ require (
6464
go.etcd.io/etcd/client/pkg/v3 v3.5.16
6565
go.etcd.io/etcd/client/v3 v3.5.16
6666
go.opentelemetry.io/contrib/instrumentation/github.com/emicklei/go-restful/otelrestful v0.42.0
67+
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.58.0
6768
go.opentelemetry.io/otel v1.33.0
6869
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.33.0
6970
go.opentelemetry.io/otel/metric v1.33.0
@@ -201,7 +202,6 @@ require (
201202
go.etcd.io/etcd/server/v3 v3.5.16 // indirect
202203
go.opentelemetry.io/auto/sdk v1.1.0 // indirect
203204
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.58.0 // indirect
204-
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.58.0 // indirect
205205
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.33.0 // indirect
206206
go.uber.org/multierr v1.11.0 // indirect
207207
golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56 // indirect

staging/src/k8s.io/apiserver/pkg/endpoints/filters/traces.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,15 @@ import (
2424
"go.opentelemetry.io/otel/trace"
2525
"k8s.io/apiserver/pkg/endpoints/request"
2626

27+
"k8s.io/apiserver/pkg/authentication/user"
2728
tracing "k8s.io/component-base/tracing"
2829
)
2930

3031
// WithTracing adds tracing to requests if the incoming request is sampled
3132
func WithTracing(handler http.Handler, tp trace.TracerProvider) http.Handler {
3233
opts := []otelhttp.Option{
3334
otelhttp.WithPropagators(tracing.Propagators()),
34-
otelhttp.WithPublicEndpoint(),
35+
otelhttp.WithPublicEndpointFn(notSystemPrivilegedGroup),
3536
otelhttp.WithTracerProvider(tp),
3637
otelhttp.WithSpanNameFormatter(func(operation string, r *http.Request) string {
3738
ctx := r.Context()
@@ -43,6 +44,11 @@ func WithTracing(handler http.Handler, tp trace.TracerProvider) http.Handler {
4344
}),
4445
}
4546
wrappedHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
47+
// Adjust otelhttp tracing start time to match the start time used
48+
// for Prometheus metrics.
49+
if startTime, ok := request.ReceivedTimestampFrom(r.Context()); ok {
50+
r = r.WithContext(otelhttp.ContextWithStartTime(r.Context(), startTime))
51+
}
4652
// Add the http.target attribute to the otelhttp span
4753
// Workaround for https://github.com/open-telemetry/opentelemetry-go-contrib/issues/3743
4854
if r.URL != nil {
@@ -73,3 +79,14 @@ func getSpanNameFromRequestInfo(info *request.RequestInfo, r *http.Request) stri
7379
}
7480
return r.Method + " " + spanName
7581
}
82+
83+
func notSystemPrivilegedGroup(req *http.Request) bool {
84+
if u, ok := request.UserFrom(req.Context()); ok {
85+
for _, group := range u.GetGroups() {
86+
if group == user.SystemPrivilegedGroup || group == user.MonitoringGroup {
87+
return false
88+
}
89+
}
90+
}
91+
return true
92+
}

staging/src/k8s.io/apiserver/pkg/server/config.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1030,6 +1030,11 @@ func DefaultBuildHandlerChain(apiHandler http.Handler, c *Config) http.Handler {
10301030
failedHandler := genericapifilters.Unauthorized(c.Serializer)
10311031
failedHandler = genericapifilters.WithFailedAuthenticationAudit(failedHandler, c.AuditBackend, c.AuditPolicyRuleEvaluator)
10321032

1033+
// WithTracing comes after authentication so we can allow authenticated
1034+
// clients to influence sampling.
1035+
if c.FeatureGate.Enabled(genericfeatures.APIServerTracing) {
1036+
handler = genericapifilters.WithTracing(handler, c.TracerProvider)
1037+
}
10331038
failedHandler = filterlatency.TrackCompleted(failedHandler)
10341039
handler = filterlatency.TrackCompleted(handler)
10351040
handler = genericapifilters.WithAuthentication(handler, c.Authentication.Authenticator, failedHandler, c.Authentication.APIAudiences, c.Authentication.RequestHeaderConfig)
@@ -1060,9 +1065,6 @@ func DefaultBuildHandlerChain(apiHandler http.Handler, c *Config) http.Handler {
10601065
handler = genericfilters.WithRetryAfter(handler, c.lifecycleSignals.NotAcceptingNewRequest.Signaled())
10611066
}
10621067
handler = genericfilters.WithHTTPLogging(handler)
1063-
if c.FeatureGate.Enabled(genericfeatures.APIServerTracing) {
1064-
handler = genericapifilters.WithTracing(handler, c.TracerProvider)
1065-
}
10661068
handler = genericapifilters.WithLatencyTrackers(handler)
10671069
// WithRoutine will execute future handlers in a separate goroutine and serving
10681070
// handler in current goroutine to minimize the stack memory usage. It must be

0 commit comments

Comments
 (0)