forked from open-telemetry/opentelemetry-go-contrib
/
transport.go
193 lines (159 loc) · 4.91 KB
/
transport.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
* Tencent is pleased to support the open source community by making Blueking Container Service available.
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
* Licensed under the MIT License (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
* http://opensource.org/licenses/MIT
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package transport
import (
"context"
"fmt"
"io"
"net/http"
"strings"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/trace"
"k8s.io/client-go/transport"
"github.com/adevjoe/opentelemetry-go-contrib/instrumentation/k8s.io/client-go/internal"
"github.com/adevjoe/opentelemetry-go-contrib/instrumentation/k8s.io/client-go/option"
"github.com/adevjoe/opentelemetry-go-contrib/instrumentation/k8s.io/client-go/semconv"
)
// instrumentationName is the instrumentation library identifier for a Tracer.
const instrumentationName = "client-go"
// NewWrapperFunc returns a Kubernetes WrapperFunc that can be used with a
// client configuration to trace all communication the client makes.
func NewWrapperFunc(opts ...option.Option) transport.WrapperFunc {
return func(rt http.RoundTripper) http.RoundTripper {
if rt == nil {
rt = http.DefaultTransport
}
wrapped := roundTripper{
RoundTripper: rt,
cfg: internal.NewConfig(
instrumentationName,
localToInternal(opts)...,
),
}
return &wrapped
}
}
func localToInternal(opts []option.Option) []internal.Option {
out := make([]internal.Option, len(opts))
for i, o := range opts {
out[i] = internal.Option(o)
}
return out
}
// roundTripper wraps an http.RoundTripper's requests with a span.
type roundTripper struct {
http.RoundTripper
cfg *internal.Config
}
var _ http.RoundTripper = (*roundTripper)(nil)
func (rt *roundTripper) RoundTrip(r *http.Request) (resp *http.Response, err error) {
const nLocalOpts = 2
opts := make([]trace.SpanStartOption, len(rt.cfg.DefaultStartOpts), len(rt.cfg.DefaultStartOpts)+nLocalOpts)
copy(opts, rt.cfg.DefaultStartOpts)
opts = append(
opts,
trace.WithSpanKind(trace.SpanKindClient),
trace.WithAttributes(semconv.ClientRequest(r)...),
)
tracer := rt.cfg.ResolveTracer(r.Context())
ctx, span := tracer.Start(r.Context(), name(r), opts...)
// Ensure anything downstream knows about the started span.
r = r.WithContext(ctx)
rt.cfg.Propagator.Inject(ctx, propagation.HeaderCarrier(r.Header))
resp, err = rt.RoundTripper.RoundTrip(r)
if err != nil {
span.RecordError(err)
span.SetStatus(codes.Error, err.Error())
span.End()
return
}
if resp.StatusCode < http.StatusMultipleChoices {
span.SetAttributes(semconv.ClientResponse(resp)...)
span.SetStatus(codes.Ok, codes.Ok.String())
} else {
span.SetAttributes(semconv.ClientResponse(resp)...)
span.SetStatus(codes.Error, fmt.Sprintf("Invalid HTTP status code %d", resp.StatusCode))
}
resp.Body = &wrappedBody{ctx: ctx, span: span, body: resp.Body}
return
}
const (
prefixAPI = "/api/v1/"
prefixWatch = "watch/"
)
// name returns an appropriate span name based on the client request.
// OpenTelemetry semantic conventions require this name to be low cardinality,
// but since the Kubernetes API is somewhat predictable we can usually return
// more than just "HTTP {METHOD}".
func name(r *http.Request) string {
path := r.URL.Path
method := r.Method
if !strings.HasPrefix(path, prefixAPI) {
return "HTTP " + method
}
var out strings.Builder
out.WriteString("HTTP " + method + " ")
path = strings.TrimPrefix(path, prefixAPI)
if strings.HasPrefix(path, prefixWatch) {
path = strings.TrimPrefix(path, prefixWatch)
out.WriteString(prefixWatch)
}
// For each {type}/{name}, tokenize the {name} portion.
var previous string
for i, part := range strings.Split(path, "/") {
if i > 0 {
out.WriteRune('/')
}
if i%2 == 0 {
out.WriteString(part)
previous = part
} else {
out.WriteString(tokenize(previous))
}
}
return out.String()
}
func tokenize(k8Type string) string {
switch k8Type {
case "namespaces":
return "{namespace}"
case "proxy":
return "{path}"
default:
return "{name}"
}
}
type wrappedBody struct {
ctx context.Context
span trace.Span
body io.ReadCloser
}
var _ io.ReadCloser = (*wrappedBody)(nil)
func (wb *wrappedBody) Read(b []byte) (int, error) {
n, err := wb.body.Read(b)
switch err {
case nil:
// nothing to do here but fall through to the return
case io.EOF:
wb.span.End()
default:
wb.span.RecordError(err)
wb.span.SetStatus(codes.Error, err.Error())
}
return n, err
}
func (wb *wrappedBody) Close() error {
wb.span.End()
return wb.body.Close()
}