Debian Codegraph @

pkg/endpoints/filters/traces.go

raw
1 /*
2 Copyright 2021 The Kubernetes Authors.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 package filters
18
19 import (
20 "net/http"
21
22 "go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
23 semconv "go.opentelemetry.io/otel/semconv/v1.17.0"
24 "go.opentelemetry.io/otel/trace"
25 "k8s.io/apiserver/pkg/endpoints/request"
26
27 "k8s.io/apiserver/pkg/authentication/user"
28 tracing "k8s.io/component-base/tracing"
29 )
30
31 // WithTracing adds tracing to requests if the incoming request is sampled
32 func WithTracing(handler http.Handler, tp trace.TracerProvider) http.Handler {
33 opts := []otelhttp.Option{
34 otelhttp.WithPropagators(tracing.Propagators()),
35 otelhttp.WithPublicEndpointFn(notSystemPrivilegedGroup),
36 otelhttp.WithTracerProvider(tp),
37 otelhttp.WithSpanNameFormatter(func(operation string, r *http.Request) string {
38 ctx := r.Context()
39 info, exist := request.RequestInfoFrom(ctx)
40 if !exist || !info.IsResourceRequest {
41 return r.Method
42 }
43 return getSpanNameFromRequestInfo(info, r)
44 }),
45 }
46 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 }
52 // Add the http.target attribute to the otelhttp span
53 // Workaround for https://github.com/open-telemetry/opentelemetry-go-contrib/issues/3743
54 if r.URL != nil {
55 trace.SpanFromContext(r.Context()).SetAttributes(semconv.HTTPTarget(r.URL.RequestURI()))
56 }
57 handler.ServeHTTP(w, r)
58 })
59 // With Noop TracerProvider, the otelhttp still handles context propagation.
60 // See https://github.com/open-telemetry/opentelemetry-go/tree/main/example/passthrough
61 return otelhttp.NewHandler(wrappedHandler, "KubernetesAPI", opts...)
62 }
63
64 func getSpanNameFromRequestInfo(info *request.RequestInfo, r *http.Request) string {
65 spanName := "/" + info.APIPrefix
66 if info.APIGroup != "" {
67 spanName += "/" + info.APIGroup
68 }
69 spanName += "/" + info.APIVersion
70 if info.Namespace != "" {
71 spanName += "/namespaces/{:namespace}"
72 }
73 spanName += "/" + info.Resource
74 if info.Name != "" {
75 spanName += "/" + "{:name}"
76 }
77 if info.Subresource != "" {
78 spanName += "/" + info.Subresource
79 }
80 return r.Method + " " + spanName
81 }
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 }