-
Notifications
You must be signed in to change notification settings - Fork 29
/
metric.go
144 lines (124 loc) · 3.64 KB
/
metric.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package monitoring
import (
"encoding/json"
"os"
"strings"
"sync"
"time"
"code.cloudfoundry.org/lager"
"github.com/cloudfoundry-community/splunk-firehose-nozzle/eventwriter"
"github.com/cloudfoundry-community/splunk-firehose-nozzle/utils"
)
const (
splunkMetric = "metric_name:"
)
type Metrics struct {
CallerFuncs map[string]MonitorFunc
Counters map[string][]utils.Counter
logger lager.Logger
interval time.Duration
ticker *time.Ticker
writer eventwriter.Writer
selectedMonitoringMetrics *utils.Set
tickerMutex sync.Mutex
}
func NewMetricsMonitor(logger lager.Logger, interval time.Duration, writer eventwriter.Writer, filter string) Monitor {
if monitor != nil {
monitor.Stop()
}
monitor = &Metrics{
CallerFuncs: make(map[string]MonitorFunc),
Counters: make(map[string][]utils.Counter),
logger: logger,
interval: interval,
writer: writer,
selectedMonitoringMetrics: setValuesForSet(filter),
}
return monitor.(*Metrics)
}
func (m *Metrics) RegisterFunc(id string, mFunc MonitorFunc) {
if m.selectedMonitoringMetrics.Contains(id) && m.interval > 0*time.Second {
m.CallerFuncs[id] = mFunc
}
}
func (m *Metrics) RegisterCounter(id string, varType utils.CounterType) utils.Counter {
if m.selectedMonitoringMetrics.Contains(id) && m.interval > 0*time.Second {
if varType == utils.UintType {
ctr := new(utils.IntCounter)
m.Counters[id] = append(m.Counters[id], ctr)
return ctr
}
}
return &utils.NopCounter{}
}
func (m *Metrics) extractFunc(metricEvent map[string]interface{}) {
for key, Func := range m.CallerFuncs {
valofFunc := Func()
splunkKey := splunkMetric + key
metricEvent[splunkKey] = valofFunc
}
}
func (m *Metrics) extractCounter(metricEvent map[string]interface{}) {
for key, listofCounters := range m.Counters {
InitSum := listofCounters[0]
for i := 1; i < len(listofCounters); i++ {
InitSum.Add(listofCounters[i].Value())
}
splunkKey := splunkMetric + key
metricEvent[splunkKey] = InitSum.Value()
}
}
func (m *Metrics) Start() {
if m.selectedMonitoringMetrics.Len() > 0 {
ticker := time.NewTicker(m.interval)
m.tickerMutex.Lock()
m.ticker = ticker
m.tickerMutex.Unlock()
metricEvent := make(map[string]interface{})
for {
select {
case <-ticker.C:
m.extractFunc(metricEvent)
m.extractCounter(metricEvent)
finalMetricEvent := prepareBatch(metricEvent)
events := []map[string]interface{}{
finalMetricEvent,
}
m.writer.Write(events)
}
}
}
}
func (m *Metrics) Stop() error {
m.tickerMutex.Lock()
if m.ticker != nil {
m.ticker.Stop()
}
m.tickerMutex.Unlock()
return nil
}
func prepareBatch(event map[string]interface{}) map[string]interface{} {
finalevent := make(map[string]interface{})
event["instance_index"] = os.Getenv("INSTANCE_INDEX")
finalevent["fields"] = event
finalevent["event"] = "metric"
finalevent["sourcetype"] = "cf:nozzlemetrics"
finalevent["time"] = utils.NanoSecondsToSeconds(time.Now().UnixNano())
return finalevent
}
func setValuesForSet(selectedMetrics string) *utils.Set {
s := utils.NewSet()
listofSelectedMetrics := ParseSelectedMetrics(selectedMetrics)
for i := 0; i < len(listofSelectedMetrics); i++ {
s.Add(listofSelectedMetrics[i])
}
return s
}
func ParseSelectedMetrics(wantedMetrics string) []string {
wantedMetrics = strings.TrimSpace(wantedMetrics)
var events []string
if err := json.Unmarshal([]byte(wantedMetrics), &events); err != nil {
events = strings.Split(wantedMetrics, ",")
}
return events
}