forked from go-go/gin-gorelic
/
gorelic.go
62 lines (51 loc) · 1.31 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
package gorelic
import (
"fmt"
"time"
"github.com/gin-gonic/gin"
"github.com/yvasiyarov/go-metrics"
"github.com/yvasiyarov/gorelic"
)
var agent *gorelic.Agent
//Metric defines an interface for monitoring metrics
type Metric interface {
GetName() string
GetUnits() string
GetValue() (float64, error)
}
//addCustomMetric allows the addition of a metric for monitoring
func addCustomMetric(metric Metric) {
if agent != nil {
agent.AddCustomMetric(metric)
}
}
//addCustomMetrics allows the addition of new metrics for monitoring
func addCustomMetrics(metrics []Metric) {
for _, metric := range metrics {
addCustomMetric(metric)
}
}
// Handler for the agent
func Handler(c *gin.Context) {
startTime := time.Now()
c.Next()
if agent != nil {
agent.HTTPTimer.UpdateSince(startTime)
}
}
// InitNewrelicAgent creates the new relic agent
func InitNewrelicAgent(license string, appname string, verbose bool, customMetrics []Metric) (gin.HandlerFunc, error) {
if license == "" {
return nil, fmt.Errorf("Please specify a NewRelic license")
}
agent = gorelic.NewAgent()
agent.NewrelicLicense = license
agent.HTTPTimer = metrics.NewTimer()
agent.CollectHTTPStat = true
agent.Verbose = verbose
agent.NewrelicName = appname
if customMetrics != nil {
addCustomMetrics(customMetrics)
}
return Handler, agent.Run()
}