forked from imgproxy/imgproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
newrelic.go
68 lines (53 loc) · 1.52 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
package main
import (
"context"
"log"
"net/http"
"time"
newrelic "github.com/newrelic/go-agent"
)
var (
newRelicEnabled = false
newRelicApp newrelic.Application
newRelicTransactionCtxKey = ctxKey("newRelicTransaction")
)
func initNewrelic() {
if len(conf.NewRelicKey) == 0 {
return
}
name := conf.NewRelicAppName
if len(name) == 0 {
name = "imgproxy"
}
var err error
config := newrelic.NewConfig(name, conf.NewRelicKey)
newRelicApp, err = newrelic.NewApplication(config)
if err != nil {
log.Fatalf("Can't init New Relic agent: %s", err)
}
newRelicEnabled = true
}
func startNewRelicTransaction(ctx context.Context, rw http.ResponseWriter, r *http.Request) (context.Context, context.CancelFunc) {
txn := newRelicApp.StartTransaction("request", rw, r)
cancel := func() { txn.End() }
return context.WithValue(ctx, newRelicTransactionCtxKey, txn), cancel
}
func startNewRelicSegment(ctx context.Context, name string) context.CancelFunc {
txn := ctx.Value(newRelicTransactionCtxKey).(newrelic.Transaction)
segment := newrelic.StartSegment(txn, name)
return func() { segment.End() }
}
func sendErrorToNewRelic(ctx context.Context, err error) {
txn := ctx.Value(newRelicTransactionCtxKey).(newrelic.Transaction)
txn.NoticeError(err)
}
func sendTimeoutToNewRelic(ctx context.Context, d time.Duration) {
txn := ctx.Value(newRelicTransactionCtxKey).(newrelic.Transaction)
txn.NoticeError(newrelic.Error{
Message: "Timeout",
Class: "Timeout",
Attributes: map[string]interface{}{
"time": d.Seconds(),
},
})
}