-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmetrics-collector.go
96 lines (78 loc) · 1.97 KB
/
metrics-collector.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 metrics
import (
"encoding/json"
"fmt"
"os"
"github.com/devopsfaith/krakend/config"
"github.com/devopsfaith/krakend/logging"
newrelic "github.com/newrelic/go-agent"
)
// Namespace for krakend_newrelic
const Namespace = "github_com/letgoapp/krakend_newrelic"
var (
app *Application
isDebugEnabled bool
)
// Config struct for NewRelic
type Config struct {
newrelic.Config
InstrumentationRate int `json:"rate"`
}
type Application struct {
newrelic.Application
Config Config
}
// ConfigGetter gets config for NewRelic
func ConfigGetter(cfg config.ExtraConfig) (Config, error) {
result := Config{}
v, ok := cfg[Namespace]
if !ok {
return result, fmt.Errorf("unknown Namespace %s", Namespace)
}
tmp, ok := v.(map[string]interface{})
if !ok {
return result, fmt.Errorf("Cannot map config to map string interface")
}
// check whether compulsory fields are present
if _, ok := tmp["license"]; !ok {
return result, fmt.Errorf("Config should have the field license defined")
}
if _, ok = tmp["appName"]; !ok {
return result, fmt.Errorf("Config should have the field appName defined")
}
// check whether debug enabled
var val interface{}
if val, ok = tmp["debugEnabled"]; !ok {
isDebugEnabled = false
} else {
valB, ok := val.(bool)
if !ok || !valB {
isDebugEnabled = false
} else {
isDebugEnabled = true
}
}
marshaledConf, err := json.Marshal(tmp)
if err != nil {
return result, err
}
err = json.Unmarshal(marshaledConf, &result)
return result, err
}
// Register registers the NewRelic app
func Register(cfg config.ExtraConfig, logger logging.Logger) {
conf, err := ConfigGetter(cfg)
if err != nil {
logger.Debug("no config for the NR module:", err.Error())
return
}
if isDebugEnabled {
conf.Config.Logger = newrelic.NewDebugLogger(os.Stdout)
}
nrApp, err := newrelic.NewApplication(conf.Config)
if err != nil {
logger.Debug("unable to start the NR module:", err.Error())
return
}
app = &Application{nrApp, conf}
}