k8s.io/apiserver@v0.31.1/pkg/endpoints/filters/traces.go (about)

     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  	tracing "k8s.io/component-base/tracing"
    28  )
    29  
    30  // WithTracing adds tracing to requests if the incoming request is sampled
    31  func WithTracing(handler http.Handler, tp trace.TracerProvider) http.Handler {
    32  	opts := []otelhttp.Option{
    33  		otelhttp.WithPropagators(tracing.Propagators()),
    34  		otelhttp.WithPublicEndpoint(),
    35  		otelhttp.WithTracerProvider(tp),
    36  		otelhttp.WithSpanNameFormatter(func(operation string, r *http.Request) string {
    37  			ctx := r.Context()
    38  			info, exist := request.RequestInfoFrom(ctx)
    39  			if !exist || !info.IsResourceRequest {
    40  				return r.Method
    41  			}
    42  			return getSpanNameFromRequestInfo(info, r)
    43  		}),
    44  	}
    45  	wrappedHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    46  		// Add the http.target attribute to the otelhttp span
    47  		// Workaround for https://github.com/open-telemetry/opentelemetry-go-contrib/issues/3743
    48  		if r.URL != nil {
    49  			trace.SpanFromContext(r.Context()).SetAttributes(semconv.HTTPTarget(r.URL.RequestURI()))
    50  		}
    51  		handler.ServeHTTP(w, r)
    52  	})
    53  	// With Noop TracerProvider, the otelhttp still handles context propagation.
    54  	// See https://github.com/open-telemetry/opentelemetry-go/tree/main/example/passthrough
    55  	return otelhttp.NewHandler(wrappedHandler, "KubernetesAPI", opts...)
    56  }
    57  
    58  func getSpanNameFromRequestInfo(info *request.RequestInfo, r *http.Request) string {
    59  	spanName := "/" + info.APIPrefix
    60  	if info.APIGroup != "" {
    61  		spanName += "/" + info.APIGroup
    62  	}
    63  	spanName += "/" + info.APIVersion
    64  	if info.Namespace != "" {
    65  		spanName += "/namespaces/{:namespace}"
    66  	}
    67  	spanName += "/" + info.Resource
    68  	if info.Name != "" {
    69  		spanName += "/" + "{:name}"
    70  	}
    71  	if info.Subresource != "" {
    72  		spanName += "/" + info.Subresource
    73  	}
    74  	return r.Method + " " + spanName
    75  }