-
Notifications
You must be signed in to change notification settings - Fork 6
/
http.go
258 lines (220 loc) · 5.8 KB
/
http.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
package ctrace
import (
"io"
"net/http"
"sync"
"github.com/Nordstrom/ctrace-go/core"
"github.com/Nordstrom/ctrace-go/ext"
"github.com/Nordstrom/ctrace-go/log"
opentracing "github.com/opentracing/opentracing-go"
)
// ******************* Http Client *******************
// TracedHTTPInterceptor is a defined function called during HTTP tracing
// to return custom OperationName and/or Tags for the given request
type TracedHTTPInterceptor func(r *http.Request) SpanConfig
type tracedHTTPClientTransport struct {
component string
transport http.RoundTripper
interceptor []TracedHTTPInterceptor // There is 0 or 1 interceptor
}
// TracedHTTPClientTransport creates a new Transporter (http.RoundTripper) that intercepts
// and traces egress requests.
func TracedHTTPClientTransport(
t http.RoundTripper,
interceptor ...TracedHTTPInterceptor,
) http.RoundTripper {
return &tracedHTTPClientTransport{
component: "ctrace.TracedHttpClientTransport",
transport: t,
interceptor: interceptor,
}
}
type closeTracker struct {
io.ReadCloser
sp opentracing.Span
}
func (c closeTracker) Close() error {
// debug("Closing Response Writer...")
err := c.ReadCloser.Close()
if err != nil {
c.sp.SetTag(ext.ErrorKey, true)
c.sp.LogFields(
log.Event("error"),
log.ErrorKind("http-client"),
log.ErrorObject(err),
)
}
c.sp.Finish()
return err
}
func optioniallyInterceptHTTP(
i []TracedHTTPInterceptor,
r *http.Request,
) SpanConfig {
for _, f := range i {
return f(r)
}
return SpanConfig{}
}
func (t *tracedHTTPClientTransport) RoundTrip(
r *http.Request,
) (*http.Response, error) {
op := r.Method + ":" + r.URL.Path
// debug("Starting client RoundTrip: op=%s", op)
config := optioniallyInterceptHTTP(t.interceptor, r)
opts := []opentracing.StartSpanOption{
ext.SpanKindClient(),
ext.Component(t.component),
ext.HTTPMethod(r.Method),
ext.HTTPUrl(r.URL.String()),
}
if config.OperationName != "" {
op = config.OperationName
}
if len(config.Tags) > 0 {
opts = append(opts, config.Tags...)
}
tracer := Global()
span, _ := StartSpanFromContext(r.Context(), op, opts...)
tracer.Inject(
span.Context(),
opentracing.HTTPHeaders,
opentracing.HTTPHeadersCarrier(r.Header))
res, err := t.transport.RoundTrip(r)
if err != nil {
span.SetTag(ext.ErrorKey, true)
span.LogFields(
log.Event("error"),
log.ErrorKind("http-client"),
log.ErrorObject(err),
)
span.Finish()
return res, err
}
span.SetTag(ext.HTTPStatusCodeKey, res.StatusCode)
if res.StatusCode >= 400 {
span.SetTag(ext.ErrorKey, true)
}
if r.Method == "HEAD" {
span.Finish()
} else {
res.Body = closeTracker{res.Body, span}
}
return res, nil
}
// ******************* Http Server *******************
func httpOperationName(
mux *http.ServeMux,
muxFound bool,
r *http.Request,
) string {
if muxFound {
_, pattern := mux.Handler(r)
return r.Method + ":" + pattern
}
return r.Method + ":" + r.URL.Path
}
type responseInterceptor struct {
tracer opentracing.Tracer
span opentracing.Span
parentCtx core.SpanContext
ctx core.SpanContext
writer http.ResponseWriter
headerWritten bool
traced bool
lock sync.Mutex
}
func (i *responseInterceptor) trace(code int) {
i.lock.Lock()
defer i.lock.Unlock()
if !i.headerWritten {
i.span.SetTag(ext.HTTPStatusCodeKey, code)
if code >= 400 {
i.span.SetTag(ext.ErrorKey, true)
}
i.span.Finish()
i.tracer.Inject(
i.ctx,
core.HTTPHeaders,
core.HTTPHeadersCarrier(i.writer.Header()),
)
i.headerWritten = true
}
}
func (i *responseInterceptor) WriteHeader(code int) {
i.writer.WriteHeader(code)
i.trace(code)
}
func (i *responseInterceptor) Write(b []byte) (int, error) {
// debug("Writing response body %s", string(b))
r, e := i.writer.Write(b)
if !i.headerWritten {
i.trace(http.StatusOK)
}
return r, e
}
func (i *responseInterceptor) Header() http.Header {
return i.writer.Header()
}
// TracedHTTPHandler returns a http.Handler that is traced as an opentracing.Span
func TracedHTTPHandler(
h http.Handler,
interceptor ...TracedHTTPInterceptor,
) http.Handler {
mux, muxFound := h.(*http.ServeMux)
fn := func(w http.ResponseWriter, r *http.Request) {
for _, v := range interceptor {
config := v(r)
if config.Ignored != nil && config.Ignored.MatchString(r.URL.String()) {
h.ServeHTTP(w, r)
return
}
}
tracer := opentracing.GlobalTracer()
parentCtx, _ := tracer.Extract(core.HTTPHeaders, core.HTTPHeadersCarrier(r.Header))
config := optioniallyInterceptHTTP(interceptor, r)
var op string
if config.OperationName != "" {
op = config.OperationName
} else {
op = httpOperationName(mux, muxFound, r)
}
opts := []opentracing.StartSpanOption{
ChildOf(parentCtx),
ext.SpanKindServer(),
ext.Component("ctrace.TracedHttpHandler"),
ext.HTTPRemoteAddr(r.RemoteAddr),
ext.HTTPMethod(r.Method),
ext.HTTPUrl(r.URL.String()),
ext.HTTPUserAgent(r.UserAgent()),
}
if config.OperationName != "" {
op = config.OperationName
}
if len(config.Tags) > 0 {
opts = append(opts, config.Tags...)
}
// debug("TracedHttpHandler: StartSpan(%s)", op)
span := tracer.StartSpan(op, opts...)
ctx := span.Context()
ri := responseInterceptor{
tracer: tracer,
span: span,
ctx: ctx.(core.SpanContext),
writer: w,
}
if parentCtx != nil {
ri.parentCtx = parentCtx.(core.SpanContext)
}
// debug("TracedHttpHandler: ServeHTTP(...)")
h.ServeHTTP(&ri, r.WithContext(ContextWithSpan(r.Context(), span)))
}
return http.HandlerFunc(fn)
}
// TracedHTTPHandlerFunc returns a http.HandlerFunc that is traced as an opentracing.Span
func TracedHTTPHandlerFunc(
fn func(http.ResponseWriter, *http.Request),
interceptor ...TracedHTTPInterceptor,
) http.HandlerFunc {
return TracedHTTPHandler(http.HandlerFunc(fn), interceptor...).ServeHTTP
}