forked from imgproxy/imgproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
newrelic.go
70 lines (54 loc) · 1.55 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
package main
import (
"context"
"fmt"
"net/http"
"time"
newrelic "github.com/newrelic/go-agent"
)
var (
newRelicEnabled = false
newRelicApp newrelic.Application
newRelicTransactionCtxKey = ctxKey("newRelicTransaction")
)
func initNewrelic() error {
if len(conf.NewRelicKey) == 0 {
return nil
}
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 {
return fmt.Errorf("Can't init New Relic agent: %s", err)
}
newRelicEnabled = true
return nil
}
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(),
},
})
}