-
Notifications
You must be signed in to change notification settings - Fork 0
/
nr.go
53 lines (42 loc) · 1019 Bytes
/
nr.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
package nr
import (
"context"
"github.com/emirmuminoglu/lloyd"
"github.com/newrelic/go-agent/v3/newrelic"
)
const userValueName = "nr-txn"
func Middleware(app *newrelic.Application) lloyd.RequestHandler {
return func(c *lloyd.Ctx) {
method := lloyd.B2S(c.Request.Header.Method())
url := c.URL()
txn := app.StartTransaction(method + " " + c.PathName())
var transport newrelic.TransportType
switch url.Scheme {
case "http":
transport = newrelic.TransportHTTP
case "https":
transport = newrelic.TransportHTTPS
default:
transport = newrelic.TransportOther
}
txn.SetWebRequest(newrelic.WebRequest{
URL: url,
Host: url.Host,
Method: method,
Transport: transport,
})
c.SetUserValue(userValueName, txn)
c.Defer(func() {
txn.SetWebResponse(c.ResponseWriter())
txn.End()
})
c.Next()
}
}
func GetTxn(ctx context.Context) *newrelic.Transaction {
v := ctx.Value(userValueName)
if v == nil {
return nil
}
return v.(*newrelic.Transaction)
}