Skip to content

Commit 6e9dee4

Browse files
committed
in kube-apiserver add otelhttp client and server wrappers
1 parent 1a37266 commit 6e9dee4

File tree

4 files changed

+68
-2
lines changed

4 files changed

+68
-2
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
Copyright 2020 The Kubernetes Authors.
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
14+
package filters
15+
16+
import (
17+
"net/http"
18+
19+
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
20+
"go.opentelemetry.io/otel"
21+
"go.opentelemetry.io/otel/propagators"
22+
)
23+
24+
// WithTracing adds tracing to requests if the incoming request is sampled
25+
func WithTracing(handler http.Handler) http.Handler {
26+
return otelhttp.NewHandler(handler, "KubernetesAPI", otelhttp.WithPropagators(otel.NewCompositeTextMapPropagator(propagators.TraceContext{}, propagators.Baggage{})))
27+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -684,6 +684,7 @@ func DefaultBuildHandlerChain(apiHandler http.Handler, c *Config) http.Handler {
684684
handler = genericfilters.WithTimeoutForNonLongRunningRequests(handler, c.LongRunningFunc, c.RequestTimeout)
685685
handler = genericfilters.WithWaitGroup(handler, c.LongRunningFunc, c.HandlerChainWaitGroup)
686686
handler = genericapifilters.WithRequestInfo(handler, c.RequestInfoResolver)
687+
handler = genericapifilters.WithTracing(handler)
687688
if c.SecureServing != nil && !c.SecureServing.DisableHTTP2 && c.GoawayChance > 0 {
688689
handler = genericfilters.WithProbabilisticGoaway(handler, c.GoawayChance)
689690
}

staging/src/k8s.io/client-go/transport/round_trippers.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ import (
2222
"strings"
2323
"time"
2424

25+
"go.opentelemetry.io/otel"
26+
"go.opentelemetry.io/otel/propagators"
27+
28+
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
2529
"golang.org/x/oauth2"
2630
"k8s.io/klog/v2"
2731

@@ -61,9 +65,27 @@ func HTTPWrappersForConfig(config *Config, rt http.RoundTripper) (http.RoundTrip
6165
len(config.Impersonate.Extra) > 0 {
6266
rt = NewImpersonatingRoundTripper(config.Impersonate, rt)
6367
}
68+
rt = NewOtelRoundTripper(rt)
6469
return rt, nil
6570
}
6671

72+
type otelWrapper struct {
73+
otelRT http.RoundTripper
74+
baseRT http.RoundTripper
75+
}
76+
77+
// NewOtelRoundTripper returns an otelhttp-wrapped round tripper that implements
78+
// WrappedRoundTripper
79+
func NewOtelRoundTripper(rt http.RoundTripper) http.RoundTripper {
80+
return &otelWrapper{otelRT: otelhttp.NewTransport(rt, otelhttp.WithPropagators(otel.NewCompositeTextMapPropagator(propagators.TraceContext{}, propagators.Baggage{}))), baseRT: rt}
81+
}
82+
83+
func (o *otelWrapper) WrappedRoundTripper() http.RoundTripper { return o.baseRT }
84+
85+
func (o *otelWrapper) RoundTrip(req *http.Request) (*http.Response, error) {
86+
return o.otelRT.RoundTrip(req)
87+
}
88+
6789
// DebugWrappers wraps a round tripper and logs based on the current log level.
6890
func DebugWrappers(rt http.RoundTripper) http.RoundTripper {
6991
switch {

staging/src/k8s.io/client-go/transport/transport_test.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ import (
2323
"fmt"
2424
"net/http"
2525
"testing"
26+
27+
"k8s.io/apimachinery/pkg/util/net"
2628
)
2729

2830
const (
@@ -260,6 +262,8 @@ func TestNew(t *testing.T) {
260262
if testCase.Err {
261263
return
262264
}
265+
transport := transportFor(t, rt)
266+
rt = transport
263267

264268
switch {
265269
case testCase.Default && rt != http.DefaultTransport:
@@ -268,8 +272,6 @@ func TestNew(t *testing.T) {
268272
t.Fatalf("got %#v, expected non-default transport", rt)
269273
}
270274

271-
// We only know how to check TLSConfig on http.Transports
272-
transport := rt.(*http.Transport)
273275
switch {
274276
case testCase.TLS && transport.TLSClientConfig == nil:
275277
t.Fatalf("got %#v, expected TLSClientConfig", transport)
@@ -450,3 +452,17 @@ func Test_contextCanceller_RoundTrip(t *testing.T) {
450452
})
451453
}
452454
}
455+
456+
// transportFor finds the underlying *http.Transport for the round tripper.
457+
// we only know how to check TLSConfig on http.Transports
458+
func transportFor(t *testing.T, rt http.RoundTripper) *http.Transport {
459+
switch transport := rt.(type) {
460+
case *http.Transport:
461+
return transport
462+
case net.RoundTripperWrapper:
463+
return transportFor(t, transport.WrappedRoundTripper())
464+
default:
465+
t.Fatalf("unexpected transport type: %v", transport)
466+
return nil
467+
}
468+
}

0 commit comments

Comments
 (0)