-
Notifications
You must be signed in to change notification settings - Fork 2
/
opentracing.go
158 lines (126 loc) · 4.31 KB
/
opentracing.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
package opentracing
import (
"fmt"
"net/http"
"strings"
"time"
"github.com/alexfalkowski/go-service/meta"
sstrings "github.com/alexfalkowski/go-service/strings"
stime "github.com/alexfalkowski/go-service/time"
sopentracing "github.com/alexfalkowski/go-service/trace/opentracing"
"github.com/alexfalkowski/go-service/version"
"github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/ext"
"github.com/opentracing/opentracing-go/log"
"go.uber.org/fx"
)
const (
httpURL = "http.url"
httpMethod = "http.method"
httpDuration = "http.duration_ms"
httpStartTime = "http.start_time"
httpRequestDeadline = "http.request.deadline"
httpStatusCode = "http.status_code"
component = "component"
httpComponent = "http"
)
// TracerParams for opentracing.
type TracerParams struct {
fx.In
Lifecycle fx.Lifecycle
Config *sopentracing.Config
Version version.Version
}
// NewTracer for opentracing.
func NewTracer(params TracerParams) (Tracer, error) {
return sopentracing.NewTracer(sopentracing.TracerParams{Lifecycle: params.Lifecycle, Name: "http", Config: params.Config, Version: params.Version})
}
// Tracer for opentracing.
type Tracer opentracing.Tracer
// Handler for opentracing.
type Handler struct {
tracer Tracer
http.Handler
}
// NewHandler for opentracing.
func NewHandler(tracer Tracer, handler http.Handler) *Handler {
return &Handler{tracer: tracer, Handler: handler}
}
// ServeHTTP for opentracing.
func (h *Handler) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
service, method := req.URL.Path, strings.ToLower(req.Method)
if sstrings.IsHealth(service) {
h.Handler.ServeHTTP(resp, req)
return
}
start := time.Now().UTC()
ctx := req.Context()
operationName := fmt.Sprintf("%s %s", method, service)
carrier := opentracing.HTTPHeadersCarrier(req.Header)
traceCtx, _ := h.tracer.Extract(opentracing.HTTPHeaders, carrier)
opts := []opentracing.StartSpanOption{
ext.RPCServerOption(traceCtx),
opentracing.Tag{Key: httpStartTime, Value: start.Format(time.RFC3339)},
opentracing.Tag{Key: httpURL, Value: service},
opentracing.Tag{Key: httpMethod, Value: method},
opentracing.Tag{Key: component, Value: httpComponent},
ext.SpanKindRPCServer,
}
span := h.tracer.StartSpan(operationName, opts...)
defer span.Finish()
if d, ok := ctx.Deadline(); ok {
span.SetTag(httpRequestDeadline, d.UTC().Format(time.RFC3339))
}
ctx = opentracing.ContextWithSpan(ctx, span)
h.Handler.ServeHTTP(resp, req.WithContext(ctx))
for k, v := range meta.Attributes(ctx) {
span.SetTag(k, v)
}
span.SetTag(httpDuration, stime.ToMilliseconds(time.Since(start)))
}
// NewRoundTripper for opentracing.
func NewRoundTripper(tracer Tracer, hrt http.RoundTripper) *RoundTripper {
return &RoundTripper{tracer: tracer, RoundTripper: hrt}
}
// RoundTripper for opentracing.
type RoundTripper struct {
tracer Tracer
http.RoundTripper
}
func (r *RoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
if sstrings.IsHealth(req.URL.String()) {
return r.RoundTripper.RoundTrip(req)
}
service, method := req.URL.Hostname(), strings.ToLower(req.Method)
start := time.Now().UTC()
ctx := req.Context()
operationName := fmt.Sprintf("%s %s", method, service)
opts := []opentracing.StartSpanOption{
opentracing.Tag{Key: httpStartTime, Value: start.Format(time.RFC3339)},
opentracing.Tag{Key: httpURL, Value: service},
opentracing.Tag{Key: httpMethod, Value: method},
opentracing.Tag{Key: component, Value: httpComponent},
ext.SpanKindRPCClient,
}
span, ctx := opentracing.StartSpanFromContextWithTracer(ctx, r.tracer, operationName, opts...)
defer span.Finish()
if d, ok := ctx.Deadline(); ok {
span.SetTag(httpRequestDeadline, d.UTC().Format(time.RFC3339))
}
carrier := opentracing.HTTPHeadersCarrier(req.Header)
if err := r.tracer.Inject(span.Context(), opentracing.HTTPHeaders, carrier); err != nil {
return nil, err
}
resp, err := r.RoundTripper.RoundTrip(req.WithContext(ctx))
for k, v := range meta.Attributes(ctx) {
span.SetTag(k, v)
}
span.SetTag(httpDuration, stime.ToMilliseconds(time.Since(start)))
if err != nil {
ext.Error.Set(span, true)
span.LogFields(log.String("event", "error"), log.String("message", err.Error()))
return nil, err
}
span.SetTag(httpStatusCode, resp.StatusCode)
return resp, nil
}