forked from elastic/apm-agent-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.go
62 lines (54 loc) · 1.4 KB
/
middleware.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
package apmgorilla
import (
"net/http"
"github.com/gorilla/mux"
"github.com/elastic/apm-agent-go"
"github.com/elastic/apm-agent-go/module/apmhttp"
)
// Middleware returns a new gorilla/mux middleware handler
// for tracing requests and reporting errors.
//
// This middleware will recover and report panics, so it can
// be used instead of the gorilla/middleware.RecoveryHandler
// middleware.
//
// By default, the middleware will use elasticapm.DefaultTracer.
// Use WithTracer to specify an alternative tracer.
func Middleware(o ...Option) mux.MiddlewareFunc {
opts := options{tracer: elasticapm.DefaultTracer}
for _, o := range o {
o(&opts)
}
return func(h http.Handler) http.Handler {
return apmhttp.Wrap(
h,
apmhttp.WithTracer(opts.tracer),
apmhttp.WithServerRequestName(routeRequestName),
)
}
}
func routeRequestName(req *http.Request) string {
route := mux.CurrentRoute(req)
if route != nil {
tpl, err := route.GetPathTemplate()
if err == nil {
return req.Method + " " + massageTemplate(tpl)
}
}
return apmhttp.ServerRequestName(req)
}
type options struct {
tracer *elasticapm.Tracer
}
// Option sets options for tracing.
type Option func(*options)
// WithTracer returns an Option which sets t as the tracer
// to use for tracing server requests.
func WithTracer(t *elasticapm.Tracer) Option {
if t == nil {
panic("t == nil")
}
return func(o *options) {
o.tracer = t
}
}