-
Notifications
You must be signed in to change notification settings - Fork 0
/
newrelic.go
96 lines (77 loc) · 2.84 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
90
91
92
93
94
95
96
package main
import (
"fmt"
"strconv"
"github.com/Sirupsen/logrus"
"github.com/gocraft/health"
"github.com/gorilla/mux"
"github.com/newrelic/go-agent"
"github.com/newrelic/go-agent/_integrations/nrgorilla/v1"
"github.com/TykTechnologies/tyk/config"
)
// SetupNewRelic creates new newrelic.Application instance
func SetupNewRelic() (app newrelic.Application) {
var err error
logger := log.WithFields(logrus.Fields{"prefix": "newrelic"})
logger.Info("Initializing NewRelic...")
cfg := newrelic.NewConfig(config.Global().NewRelic.AppName, config.Global().NewRelic.LicenseKey)
if config.Global().NewRelic.AppName != "" {
cfg.Enabled = true
}
cfg.Logger = &newRelicLogger{logger}
if app, err = newrelic.NewApplication(cfg); err != nil {
logger.Warn("Error initializing NewRelic, skipping... ", err)
return
}
instrument.AddSink(&newRelicSink{relic: app})
logger.Info("NewRelic initialized")
return
}
// AddNewRelicInstrumentation adds NewRelic instrumentation to the router
func AddNewRelicInstrumentation(app newrelic.Application, r *mux.Router) {
if app != nil {
nrgorilla.InstrumentRoutes(r, app)
}
}
type newRelicLogger struct{ *logrus.Entry }
func (l *newRelicLogger) Error(msg string, c map[string]interface{}) {
l.WithFields(c).Error(msg)
}
func (l *newRelicLogger) Warn(msg string, c map[string]interface{}) {
l.WithFields(c).Warn(msg)
}
func (l *newRelicLogger) Info(msg string, c map[string]interface{}) {
l.WithFields(c).Info(msg)
}
func (l *newRelicLogger) Debug(msg string, c map[string]interface{}) {
l.WithFields(c).Debug(msg)
}
func (l *newRelicLogger) DebugEnabled() bool {
return l.Level >= logrus.DebugLevel
}
type newRelicSink struct {
relic newrelic.Application
health.Sink
}
func (s *newRelicSink) EmitEvent(job string, event string, kvs map[string]string) {
s.relic.RecordCustomEvent(job+":"+event, makeParams(kvs))
}
func (s *newRelicSink) EmitEventErr(job string, event string, err error, kvs map[string]string) {
s.relic.RecordCustomEvent(job+":"+event+":msg:"+err.Error(), makeParams(kvs))
}
func (s *newRelicSink) EmitTiming(job string, event string, nanoseconds int64, kvs map[string]string) {
s.relic.RecordCustomEvent(job+":"+event+":dur(ns):"+strconv.FormatInt(nanoseconds, 10), makeParams(kvs))
}
func (s *newRelicSink) EmitComplete(job string, status health.CompletionStatus, nanoseconds int64, kvs map[string]string) {
s.relic.RecordCustomEvent(job+":health:"+status.String()+":dur(ns):"+strconv.FormatInt(nanoseconds, 10), makeParams(kvs))
}
func (s *newRelicSink) EmitGauge(job string, event string, value float64, kvs map[string]string) {
s.relic.RecordCustomEvent(job+":"+event+":value:"+fmt.Sprintf("%.2f", value), makeParams(kvs))
}
func makeParams(kvs map[string]string) (params map[string]interface{}) {
params = make(map[string]interface{}, len(kvs))
for k, v := range kvs {
params[k] = v
}
return
}