-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
newrelic.go
89 lines (70 loc) · 1.72 KB
/
newrelic.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
package newrelic
import (
"net/http"
"net/url"
"github.com/gogearbox/gearbox"
"github.com/newrelic/go-agent/v3/newrelic"
"github.com/valyala/fasthttp"
)
type handler struct {
repanic bool
app *newrelic.Application
}
// Options struct holds newrelic middleware settings
type Options struct {
// Repanic configures whether newrelic should repanic after recovery
Repanic bool
}
// New returns middleware handler
func New(app *newrelic.Application, options ...Options) func(ctx gearbox.Context) {
var op Options
if len(options) > 0 {
op = options[0]
}
handler := &handler{
repanic: op.Repanic,
app: app,
}
return handler.handle
}
func (h *handler) handle(ctx gearbox.Context) {
name := string(ctx.Context().Method()) + " " + string(ctx.Context().Path())
txn := h.app.StartTransaction(name)
txn.SetWebRequestHTTP(convert(ctx.Context()))
defer h.recoverWithNewrelic(txn)
ctx.Next()
}
func (h *handler) recoverWithNewrelic(txn *newrelic.Transaction) {
err := recover()
if err != nil {
switch err := err.(type) {
case error:
txn.NoticeError(err)
default:
txn.NoticeError(errWrapper{err})
}
}
txn.End()
if h.repanic && err != nil {
panic(err)
}
}
// convert converts fasthttp request to net/http request
func convert(ctx *fasthttp.RequestCtx) *http.Request {
r := new(http.Request)
r.Method = string(ctx.Method())
uri := ctx.URI()
r.URL = &url.URL{
Scheme: string(uri.Scheme()),
Path: string(uri.Path()),
Host: string(uri.Host()),
RawQuery: string(uri.QueryString()),
}
// Headers
r.Header = make(http.Header)
r.Header.Add("Host", string(ctx.Host()))
ctx.Request.Header.VisitAll(func(key, value []byte) {
r.Header.Add(string(key), string(value))
})
return r
}