-
Notifications
You must be signed in to change notification settings - Fork 29
/
interceptor.go
400 lines (385 loc) · 13.4 KB
/
interceptor.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
// Copyright 2022-2023 The Connect Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// 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 otelconnect
import (
"context"
"errors"
"fmt"
"strings"
"sync"
"time"
connect "connectrpc.com/connect"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/propagation"
semconv "go.opentelemetry.io/otel/semconv/v1.21.0"
"go.opentelemetry.io/otel/trace"
"google.golang.org/protobuf/proto"
)
// Interceptor implements [connect.Interceptor] that adds
// OpenTelemetry metrics and tracing to connect handlers and clients.
type Interceptor struct {
config config
clientInstruments instruments
serverInstruments instruments
}
var _ connect.Interceptor = &Interceptor{}
// NewInterceptor returns an interceptor that implements [connect.Interceptor].
// It adds OpenTelemetry metrics and tracing to connect handlers and clients.
// Use options to configure the interceptor. Any invalid options will cause an
// error to be returned. The interceptor will use the default tracer and meter
// providers. To use a custom tracer or meter provider pass in the
// [WithTracerProvider] or [WithMeterProvider] options. To disable metrics or
// tracing pass in the [WithoutMetrics] or [WithoutTracing] options.
func NewInterceptor(options ...Option) (*Interceptor, error) {
cfg := config{
now: time.Now,
tracer: otel.GetTracerProvider().Tracer(
instrumentationName,
trace.WithInstrumentationVersion(semanticVersion),
),
propagator: otel.GetTextMapPropagator(),
meter: otel.GetMeterProvider().Meter(
instrumentationName,
metric.WithInstrumentationVersion(semanticVersion)),
}
for _, opt := range options {
opt.apply(&cfg)
}
clientInstruments, err := createInstruments(cfg.meter, clientKey)
if err != nil {
return nil, fmt.Errorf("failed to create client instruments: %w", err)
}
serverInstruments, err := createInstruments(cfg.meter, serverKey)
if err != nil {
return nil, fmt.Errorf("failed to create server instruments: %w", err)
}
return &Interceptor{
config: cfg,
clientInstruments: clientInstruments,
serverInstruments: serverInstruments,
}, nil
}
// getInstruments returns the correct instrumentation for the interceptor.
func (i *Interceptor) getInstruments(isClient bool) *instruments {
if isClient {
return &i.clientInstruments
}
return &i.serverInstruments
}
// WrapUnary implements otel tracing and metrics for unary handlers.
func (i *Interceptor) WrapUnary(next connect.UnaryFunc) connect.UnaryFunc {
return func(ctx context.Context, request connect.AnyRequest) (connect.AnyResponse, error) {
requestStartTime := i.config.now()
if i.config.filter != nil {
if !i.config.filter(ctx, request.Spec()) {
return next(ctx, request)
}
}
attributeFilter := i.config.filterAttribute.filter
isClient := request.Spec().IsClient
name := strings.TrimLeft(request.Spec().Procedure, "/")
protocol := protocolToSemConv(request.Peer().Protocol)
attributes := attributeFilter(request.Spec(), requestAttributes(request.Spec(), request.Peer())...)
instrumentation := i.getInstruments(isClient)
carrier := propagation.HeaderCarrier(request.Header())
spanKind := trace.SpanKindClient
requestSpan, responseSpan := semconv.MessageTypeSent, semconv.MessageTypeReceived
traceOpts := []trace.SpanStartOption{
trace.WithAttributes(attributes...),
trace.WithAttributes(headerAttributes(protocol, requestKey, request.Header(), i.config.requestHeaderKeys)...),
}
if !isClient {
spanKind = trace.SpanKindServer
requestSpan, responseSpan = semconv.MessageTypeReceived, semconv.MessageTypeSent
// if a span already exists in ctx then there must have already been another interceptor
// that set it, so don't extract from carrier.
if !trace.SpanContextFromContext(ctx).IsValid() {
ctx = i.config.propagator.Extract(ctx, carrier)
if !i.config.trustRemote {
traceOpts = append(traceOpts,
trace.WithNewRoot(),
trace.WithLinks(trace.LinkFromContext(ctx)),
)
}
}
}
traceOpts = append(traceOpts, trace.WithSpanKind(spanKind))
ctx, span := i.config.tracer.Start(
ctx,
name,
traceOpts...,
)
defer span.End()
if isClient {
i.config.propagator.Inject(ctx, carrier)
}
var requestSize int
if request != nil {
if msg, ok := request.Any().(proto.Message); ok {
requestSize = proto.Size(msg)
}
}
if !i.config.omitTraceEvents {
span.AddEvent(messageKey,
trace.WithAttributes(
requestSpan,
semconv.MessageIDKey.Int(1),
semconv.MessageUncompressedSizeKey.Int(requestSize),
),
)
}
response, err := next(ctx, request)
if statusCode, ok := statusCodeAttribute(protocol, err); ok {
attributes = append(attributes, statusCode)
}
var responseSize int
if err == nil {
if msg, ok := response.Any().(proto.Message); ok {
responseSize = proto.Size(msg)
}
span.SetAttributes(headerAttributes(protocol, responseKey, response.Header(), i.config.responseHeaderKeys)...)
}
if !i.config.omitTraceEvents {
span.AddEvent(messageKey,
trace.WithAttributes(
responseSpan,
semconv.MessageIDKey.Int(1),
semconv.MessageUncompressedSizeKey.Int(responseSize),
),
)
}
attributes = attributeFilter(request.Spec(), attributes...)
if isClient {
span.SetStatus(clientSpanStatus(protocol, err))
} else {
span.SetStatus(serverSpanStatus(protocol, err))
}
span.SetAttributes(attributes...)
instrumentation.duration.Record(ctx, i.config.now().Sub(requestStartTime).Milliseconds(), metric.WithAttributes(attributes...))
instrumentation.requestSize.Record(ctx, int64(requestSize), metric.WithAttributes(attributes...))
instrumentation.requestsPerRPC.Record(ctx, 1, metric.WithAttributes(attributes...))
instrumentation.responseSize.Record(ctx, int64(responseSize), metric.WithAttributes(attributes...))
instrumentation.responsesPerRPC.Record(ctx, 1, metric.WithAttributes(attributes...))
return response, err
}
}
// WrapStreamingClient implements otel tracing and metrics for streaming connect clients.
func (i *Interceptor) WrapStreamingClient(next connect.StreamingClientFunc) connect.StreamingClientFunc {
return func(ctx context.Context, spec connect.Spec) connect.StreamingClientConn {
if i.config.filter != nil {
if !i.config.filter(ctx, spec) {
return next(ctx, spec)
}
}
requestStartTime := i.config.now()
name := strings.TrimLeft(spec.Procedure, "/")
ctx, span := i.config.tracer.Start(
ctx,
name,
trace.WithSpanKind(trace.SpanKindClient),
)
conn := next(ctx, spec)
instrumentation := i.getInstruments(spec.IsClient)
// inject the newly created span into the carrier
carrier := propagation.HeaderCarrier(conn.RequestHeader())
i.config.propagator.Inject(ctx, carrier)
state := newStreamingState(
spec,
conn.Peer(),
i.config.filterAttribute,
i.config.omitTraceEvents,
instrumentation.responseSize,
instrumentation.requestSize,
)
protocol := protocolToSemConv(conn.Peer().Protocol)
var requestOnce sync.Once
setRequestAttributes := func() {
span.SetAttributes(
headerAttributes(
protocol,
requestKey,
conn.RequestHeader(),
i.config.requestHeaderKeys,
)...,
)
}
closeSpan := func() {
requestOnce.Do(setRequestAttributes)
// state.attributes is updated with the final error that was recorded.
// If error is nil a "success" is recorded on the span and on the final duration
// metric. The "rpc.<protocol>.status_code" is not defined for any other metrics for
// streams because the error only exists when finishing the stream.
if statusCode, ok := statusCodeAttribute(protocol, state.error); ok {
state.addAttributes(statusCode)
}
span.SetAttributes(state.attributes...)
span.SetAttributes(headerAttributes(protocol, responseKey, conn.ResponseHeader(), i.config.responseHeaderKeys)...)
span.SetStatus(clientSpanStatus(protocol, state.error))
span.End()
instrumentation.requestsPerRPC.Record(ctx, state.sentCounter,
metric.WithAttributes(state.attributes...))
instrumentation.responsesPerRPC.Record(ctx, state.receivedCounter,
metric.WithAttributes(state.attributes...))
duration := i.config.now().Sub(requestStartTime).Milliseconds()
instrumentation.duration.Record(ctx, duration,
metric.WithAttributes(state.attributes...))
}
stopCtxClose := afterFunc(ctx, closeSpan)
return &streamingClientInterceptor{
StreamingClientConn: conn,
onClose: func() {
if stopCtxClose() {
closeSpan()
}
},
receive: func(msg any, conn connect.StreamingClientConn) error {
return state.receive(ctx, msg, conn)
},
send: func(msg any, conn connect.StreamingClientConn) error {
requestOnce.Do(setRequestAttributes)
return state.send(ctx, msg, conn)
},
}
}
}
// WrapStreamingHandler implements otel tracing and metrics for streaming connect handlers.
func (i *Interceptor) WrapStreamingHandler(next connect.StreamingHandlerFunc) connect.StreamingHandlerFunc {
return func(ctx context.Context, conn connect.StreamingHandlerConn) error {
requestStartTime := i.config.now()
isClient := conn.Spec().IsClient
instrumentation := i.getInstruments(isClient)
if i.config.filter != nil {
if !i.config.filter(ctx, conn.Spec()) {
return next(ctx, conn)
}
}
name := strings.TrimLeft(conn.Spec().Procedure, "/")
protocol := protocolToSemConv(conn.Peer().Protocol)
state := newStreamingState(
conn.Spec(),
conn.Peer(),
i.config.filterAttribute,
i.config.omitTraceEvents,
instrumentation.requestSize,
instrumentation.responseSize,
)
// extract any request headers into the context
carrier := propagation.HeaderCarrier(conn.RequestHeader())
traceOpts := []trace.SpanStartOption{
trace.WithSpanKind(trace.SpanKindServer),
trace.WithAttributes(state.attributes...),
trace.WithAttributes(headerAttributes(protocol, requestKey, conn.RequestHeader(), i.config.requestHeaderKeys)...),
}
if !trace.SpanContextFromContext(ctx).IsValid() {
ctx = i.config.propagator.Extract(ctx, carrier)
if !i.config.trustRemote {
traceOpts = append(traceOpts,
trace.WithNewRoot(),
trace.WithLinks(trace.LinkFromContext(ctx)),
)
}
}
// start a new span with any trace that is in the context
ctx, span := i.config.tracer.Start(
ctx,
name,
traceOpts...,
)
defer span.End()
streamingHandler := &streamingHandlerInterceptor{
StreamingHandlerConn: conn,
receive: func(msg any, conn connect.StreamingHandlerConn) error {
return state.receive(ctx, msg, conn)
},
send: func(msg any, conn connect.StreamingHandlerConn) error {
return state.send(ctx, msg, conn)
},
}
err := next(ctx, streamingHandler)
if statusCode, ok := statusCodeAttribute(protocol, err); ok {
state.addAttributes(statusCode)
}
span.SetAttributes(state.attributes...)
span.SetAttributes(headerAttributes(protocol, responseKey, conn.ResponseHeader(), i.config.responseHeaderKeys)...)
span.SetStatus(serverSpanStatus(protocol, err))
instrumentation.requestsPerRPC.Record(ctx, state.receivedCounter,
metric.WithAttributes(state.attributes...))
instrumentation.responsesPerRPC.Record(ctx, state.sentCounter,
metric.WithAttributes(state.attributes...))
duration := i.config.now().Sub(requestStartTime).Milliseconds()
instrumentation.duration.Record(ctx, duration,
metric.WithAttributes(state.attributes...))
return err
}
}
// protocolToSemConv converts the protocol string to the OpenTelemetry format.
func protocolToSemConv(protocol string) string {
switch protocol {
case grpcwebString:
return grpcwebProtocol
case grpcProtocol:
return grpcProtocol
case connectString:
return connectProtocol
default:
return protocol
}
}
func clientSpanStatus(protocol string, err error) (codes.Code, string) {
if err == nil {
return codes.Unset, ""
}
if protocol == connectProtocol && connect.IsNotModifiedError(err) {
return codes.Unset, ""
}
if connectErr := new(connect.Error); errors.As(err, &connectErr) {
return codes.Error, connectErr.Message()
}
return codes.Error, err.Error()
}
func serverSpanStatus(protocol string, err error) (codes.Code, string) {
if err == nil {
return codes.Unset, ""
}
if protocol == connectProtocol && connect.IsNotModifiedError(err) {
return codes.Unset, ""
}
if connectErr := new(connect.Error); errors.As(err, &connectErr) {
switch connectErr.Code() {
case connect.CodeUnknown,
connect.CodeDeadlineExceeded,
connect.CodeUnimplemented,
connect.CodeInternal,
connect.CodeUnavailable,
connect.CodeDataLoss:
return codes.Error, connectErr.Message()
case connect.CodeCanceled,
connect.CodeInvalidArgument,
connect.CodeNotFound,
connect.CodeAlreadyExists,
connect.CodePermissionDenied,
connect.CodeResourceExhausted,
connect.CodeFailedPrecondition,
connect.CodeAborted,
connect.CodeOutOfRange,
connect.CodeUnauthenticated:
return codes.Unset, ""
default:
return codes.Unset, ""
}
}
return codes.Error, err.Error()
}