-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathgorelic.go
72 lines (59 loc) · 1.88 KB
/
gorelic.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
package beego_gorelic
import (
"strings"
"time"
"github.com/astaxie/beego"
"github.com/astaxie/beego/context"
metrics "github.com/yvasiyarov/go-metrics"
"github.com/yvasiyarov/gorelic"
)
const (
SEPARATOR = "-"
)
var agent *gorelic.Agent
func InitNewRelicTimer(ctx *context.Context) {
startTime := time.Now()
ctx.Input.SetData("newrelic_timer", startTime)
}
func ReportMetricsToNewrelic(ctx *context.Context) {
startTimeInterface := ctx.Input.GetData("newrelic_timer")
if startTime, ok := startTimeInterface.(time.Time); ok {
agent.HTTPTimer.UpdateSince(startTime)
}
}
func InitNewrelicAgent() {
var appname string
license := beego.AppConfig.String("newrelicLicense")
if license == "" {
beego.Warn("Please specify NewRelic license in the application config: NewrelicLicense=7bceac019c7dcafae1ef95be3e3a3ff8866de245")
return
}
agent = gorelic.NewAgent()
agent.NewrelicLicense = license
agent.HTTPTimer = metrics.NewTimer()
agent.CollectHTTPStat = true
if beego.BConfig.RunMode == "dev" {
agent.Verbose = true
}
if verbose, err := beego.AppConfig.Bool("newrelicVerbose"); err == nil {
agent.Verbose = verbose
}
// Checking if New Relic appname overrides the default appname
appname = beego.AppConfig.String("newrelicAppname")
if appname == "" {
// If not set revert to using beego appname as default
appname = beego.AppConfig.String("appname")
}
nameParts := []string{appname}
switch strings.ToLower(beego.AppConfig.String("newrelicAppnameRunmode")) {
case "append":
nameParts = append(nameParts, beego.BConfig.RunMode)
case "prepend":
nameParts = append([]string{beego.BConfig.RunMode}, nameParts...)
}
agent.NewrelicName = strings.Join(nameParts, SEPARATOR)
agent.Run()
beego.InsertFilter("*", beego.BeforeRouter, InitNewRelicTimer, false)
beego.InsertFilter("*", beego.FinishRouter, ReportMetricsToNewrelic, false)
beego.Info("NewRelic agent started")
}