-
Notifications
You must be signed in to change notification settings - Fork 20
/
jaeger_round_tripper.go
executable file
·56 lines (44 loc) · 1.5 KB
/
jaeger_round_tripper.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
// Copyright © 2019 by PACE Telematics GmbH. All rights reserved.
// Created at 2019/03/14 by Florian Hübsch
package transport
import (
"fmt"
"github.com/pace/bricks/maintenance/log"
"github.com/pace/bricks/maintenance/tracing/wire"
"net/http"
"github.com/opentracing/opentracing-go"
olog "github.com/opentracing/opentracing-go/log"
)
// JaegerRoundTripper implements a chainable round tripper for tracing
type JaegerRoundTripper struct {
transport http.RoundTripper
}
// Transport returns the RoundTripper to make HTTP requests
func (l *JaegerRoundTripper) Transport() http.RoundTripper {
return l.transport
}
// SetTransport sets the RoundTripper to make HTTP requests
func (l *JaegerRoundTripper) SetTransport(rt http.RoundTripper) {
l.transport = rt
}
// RoundTrip executes a HTTP request with distributed tracing
func (l *JaegerRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
operationName := fmt.Sprintf("%s %s", req.Method, req.URL.Path)
span, ctx := opentracing.StartSpanFromContext(req.Context(), operationName)
defer span.Finish()
err := wire.ToWire(span.Context(), req)
if err != nil {
log.Ctx(ctx).Info().Err(err).Msg("unable to serialize tracing context")
}
resp, err := l.Transport().RoundTrip(req.WithContext(ctx))
attempt := attemptFromCtx(ctx)
if attempt > 0 {
span.LogFields(olog.Int("attempt", int(attempt)))
}
if err != nil {
span.LogFields(olog.Error(err))
return nil, err
}
span.LogFields(olog.Int("code", resp.StatusCode))
return resp, nil
}