-
Notifications
You must be signed in to change notification settings - Fork 1
/
newrelic.go
49 lines (41 loc) · 1.59 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
package newrelic
import (
"context"
"log"
"net/http"
"strings"
newrelic "github.com/newrelic/go-agent"
)
var (
newRelicApp newrelic.Application
)
// InitNewRelic init the client application of New Relic, for metrics and monitoring
func InitNewRelic(appName string, key string) {
var err error
config := newrelic.NewConfig(strings.ToLower(appName), key)
newRelicApp, err = newrelic.NewApplication(config)
if err != nil {
log.Printf("Cant init New Relic agent: %v", err)
}
}
// WrapHandleFunc instruments handler functions using transactions. To record route/handler transactions in New Relic
func WrapHandleFunc(pattern string, handler func(http.ResponseWriter, *http.Request)) (string, func(http.ResponseWriter, *http.Request)) {
return newrelic.WrapHandleFunc(newRelicApp, pattern, handler)
}
// StartNewRelicCustomSegment creates a custom segment for metrics/monitoring, by passing the transaction context and a custom name
func StartNewRelicCustomSegment(ctx context.Context, name string) context.CancelFunc {
txn := newrelic.FromContext(ctx)
s := newrelic.StartSegment(txn, name)
return func() { s.End() }
}
// StartNewRelicDBSegment creates a SQL database segment for metrics/monitoring, by passing the transaction context and operation (select, insert)
func StartNewRelicDBSegment(ctx context.Context, operation string, collection string) context.CancelFunc {
txn := newrelic.FromContext(ctx)
s := newrelic.DatastoreSegment{
StartTime: newrelic.StartSegmentNow(txn),
Product: newrelic.DatastoreMySQL,
Collection: collection,
Operation: operation,
}
return func() { s.End() }
}