forked from elastic/apm-agent-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
330 lines (299 loc) · 9.55 KB
/
handler.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
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you 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 apmhttp
import (
"context"
"net/http"
"go.elastic.co/apm"
)
// Wrap returns an http.Handler wrapping h, reporting each request as
// a transaction to Elastic APM.
//
// By default, the returned Handler will use apm.DefaultTracer.
// Use WithTracer to specify an alternative tracer.
//
// By default, the returned Handler will recover panics, reporting
// them to the configured tracer. To override this behaviour, use
// WithRecovery.
func Wrap(h http.Handler, o ...ServerOption) http.Handler {
if h == nil {
panic("h == nil")
}
handler := &handler{
handler: h,
tracer: apm.DefaultTracer,
requestName: ServerRequestName,
requestIgnorer: DefaultServerRequestIgnorer(),
}
for _, o := range o {
o(handler)
}
if handler.recovery == nil {
handler.recovery = NewTraceRecovery(handler.tracer)
}
return handler
}
// handler wraps an http.Handler, reporting a new transaction for each request.
//
// The http.Request's context will be updated with the transaction.
type handler struct {
handler http.Handler
tracer *apm.Tracer
recovery RecoveryFunc
panicPropagation bool
requestName RequestNameFunc
requestIgnorer RequestIgnorerFunc
}
// ServeHTTP delegates to h.Handler, tracing the transaction with
// h.Tracer, or apm.DefaultTracer if h.Tracer is nil.
func (h *handler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
if !h.tracer.Recording() || h.requestIgnorer(req) {
h.handler.ServeHTTP(w, req)
return
}
tx, req := StartTransaction(h.tracer, h.requestName(req), req)
defer tx.End()
body := h.tracer.CaptureHTTPRequestBody(req)
w, resp := WrapResponseWriter(w)
defer func() {
if v := recover(); v != nil {
if h.panicPropagation {
defer panic(v)
// 500 status code will be set only for APM transaction
// to allow other middleware to choose a different response code
if resp.StatusCode == 0 {
resp.StatusCode = http.StatusInternalServerError
}
} else if resp.StatusCode == 0 {
w.WriteHeader(http.StatusInternalServerError)
}
h.recovery(w, req, resp, body, tx, v)
}
SetTransactionContext(tx, req, resp, body)
body.Discard()
}()
h.handler.ServeHTTP(w, req)
if resp.StatusCode == 0 {
resp.StatusCode = http.StatusOK
}
}
// StartTransaction returns a new Transaction with name,
// created with tracer, and taking trace context from req.
//
// If the transaction is not ignored, the request will be
// returned with the transaction added to its context.
func StartTransaction(tracer *apm.Tracer, name string, req *http.Request) (*apm.Transaction, *http.Request) {
traceContext, ok := getRequestTraceparent(req, ElasticTraceparentHeader)
if !ok {
traceContext, ok = getRequestTraceparent(req, W3CTraceparentHeader)
}
if ok {
traceContext.State, _ = ParseTracestateHeader(req.Header[TracestateHeader]...)
}
tx := tracer.StartTransactionOptions(name, "request", apm.TransactionOptions{TraceContext: traceContext})
ctx := apm.ContextWithTransaction(req.Context(), tx)
req = RequestWithContext(ctx, req)
return tx, req
}
func getRequestTraceparent(req *http.Request, header string) (apm.TraceContext, bool) {
if values := req.Header[header]; len(values) == 1 && values[0] != "" {
if c, err := ParseTraceparentHeader(values[0]); err == nil {
return c, true
}
}
return apm.TraceContext{}, false
}
// SetTransactionContext sets tx.Result and, if the transaction is being
// sampled, sets tx.Context with information from req, resp, and body.
func SetTransactionContext(tx *apm.Transaction, req *http.Request, resp *Response, body *apm.BodyCapturer) {
tx.Result = StatusCodeResult(resp.StatusCode)
if !tx.Sampled() {
return
}
SetContext(&tx.Context, req, resp, body)
}
// SetContext sets the context for a transaction or error using information
// from req, resp, and body.
func SetContext(ctx *apm.Context, req *http.Request, resp *Response, body *apm.BodyCapturer) {
ctx.SetHTTPRequest(req)
ctx.SetHTTPRequestBody(body)
ctx.SetHTTPStatusCode(resp.StatusCode)
ctx.SetHTTPResponseHeaders(resp.Headers)
}
// WrapResponseWriter wraps an http.ResponseWriter and returns the wrapped
// value along with a *Response which will be filled in when the handler
// is called. The *Response value must not be inspected until after the
// request has been handled, to avoid data races. If neither of the
// ResponseWriter's Write or WriteHeader methods are called, then the
// response's StatusCode field will be zero.
//
// The returned http.ResponseWriter implements http.Pusher and http.Hijacker
// if and only if the provided http.ResponseWriter does.
func WrapResponseWriter(w http.ResponseWriter) (http.ResponseWriter, *Response) {
rw := responseWriter{
ResponseWriter: w,
resp: Response{
Headers: w.Header(),
},
}
h, _ := w.(http.Hijacker)
p, _ := w.(http.Pusher)
switch {
case h != nil && p != nil:
rwhp := &responseWriterHijackerPusher{
responseWriter: rw,
Hijacker: h,
Pusher: p,
}
return rwhp, &rwhp.resp
case h != nil:
rwh := &responseWriterHijacker{
responseWriter: rw,
Hijacker: h,
}
return rwh, &rwh.resp
case p != nil:
rwp := &responseWriterPusher{
responseWriter: rw,
Pusher: p,
}
return rwp, &rwp.resp
}
return &rw, &rw.resp
}
// Response records details of the HTTP response.
type Response struct {
// StatusCode records the HTTP status code set via WriteHeader.
StatusCode int
// Headers holds the headers set in the ResponseWriter.
Headers http.Header
}
type responseWriter struct {
http.ResponseWriter
resp Response
}
// WriteHeader sets w.resp.StatusCode and calls through to the embedded
// ResponseWriter.
func (w *responseWriter) WriteHeader(statusCode int) {
w.ResponseWriter.WriteHeader(statusCode)
w.resp.StatusCode = statusCode
}
// Write calls through to the embedded ResponseWriter, setting
// w.resp.StatusCode to http.StatusOK if WriteHeader has not already
// been called.
func (w *responseWriter) Write(data []byte) (int, error) {
n, err := w.ResponseWriter.Write(data)
if w.resp.StatusCode == 0 {
w.resp.StatusCode = http.StatusOK
}
return n, err
}
// CloseNotify returns w.closeNotify() if w.closeNotify is non-nil,
// otherwise it returns nil.
func (w *responseWriter) CloseNotify() <-chan bool {
if closeNotifier, ok := w.ResponseWriter.(http.CloseNotifier); ok {
return closeNotifier.CloseNotify()
}
return nil
}
// Flush calls w.flush() if w.flush is non-nil, otherwise
// it does nothing.
func (w *responseWriter) Flush() {
if flusher, ok := w.ResponseWriter.(http.Flusher); ok {
flusher.Flush()
}
}
type responseWriterHijacker struct {
responseWriter
http.Hijacker
}
type responseWriterPusher struct {
responseWriter
http.Pusher
}
type responseWriterHijackerPusher struct {
responseWriter
http.Hijacker
http.Pusher
}
// ServerOption sets options for tracing server requests.
type ServerOption func(*handler)
// WithTracer returns a ServerOption which sets t as the tracer
// to use for tracing server requests.
func WithTracer(t *apm.Tracer) ServerOption {
if t == nil {
panic("t == nil")
}
return func(h *handler) {
h.tracer = t
}
}
// WithRecovery returns a ServerOption which sets r as the recovery
// function to use for tracing server requests.
func WithRecovery(r RecoveryFunc) ServerOption {
if r == nil {
panic("r == nil")
}
return func(h *handler) {
h.recovery = r
}
}
// WithPanicPropagation returns a ServerOption which enable panic propagation.
// Any panic will be recovered and recorded as an error in a transaction, then
// panic will be caused again.
func WithPanicPropagation() ServerOption {
return func(h *handler) {
h.panicPropagation = true
}
}
// RequestNameFunc is the type of a function for use in
// WithServerRequestName.
type RequestNameFunc func(*http.Request) string
// WithServerRequestName returns a ServerOption which sets r as the function
// to use to obtain the transaction name for the given server request.
func WithServerRequestName(r RequestNameFunc) ServerOption {
if r == nil {
panic("r == nil")
}
return func(h *handler) {
h.requestName = r
}
}
// RequestIgnorerFunc is the type of a function for use in
// WithServerRequestIgnorer.
type RequestIgnorerFunc func(*http.Request) bool
// WithServerRequestIgnorer returns a ServerOption which sets r as the
// function to use to determine whether or not a server request should
// be ignored. If r is nil, all requests will be reported.
func WithServerRequestIgnorer(r RequestIgnorerFunc) ServerOption {
if r == nil {
r = IgnoreNone
}
return func(h *handler) {
h.requestIgnorer = r
}
}
// RequestWithContext is equivalent to req.WithContext, except that the URL
// pointer is copied, rather than the contents.
func RequestWithContext(ctx context.Context, req *http.Request) *http.Request {
url := req.URL
req.URL = nil
reqCopy := req.WithContext(ctx)
reqCopy.URL = url
req.URL = url
return reqCopy
}