-
Notifications
You must be signed in to change notification settings - Fork 5
/
metricsmanager.go
76 lines (62 loc) · 1.79 KB
/
metricsmanager.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
package gamq
import (
"fmt"
"github.com/FireEater64/gamq/message"
log "github.com/cihub/seelog"
"github.com/quipo/statsd"
"time"
)
const (
metricsQueueName = "metrics"
)
type MetricsManager struct {
metricsChannel chan *Metric
statsBuffer *statsd.StatsdBuffer
statsdEnabled bool
queueManager *queueManager
}
func NewMetricsManager(givenQueueManager *queueManager) *MetricsManager {
m := MetricsManager{}
m.queueManager = givenQueueManager
m.metricsChannel = make(chan *Metric, 100)
if Configuration.StatsDEndpoint != "" {
m.statsdEnabled = true
statsClient := statsd.NewStatsdClient(Configuration.StatsDEndpoint, "gamq.")
statsClient.CreateSocket()
interval := time.Second
m.statsBuffer = statsd.NewStatsdBuffer(interval, statsClient)
log.Debugf("Initialized StatsD - sending metrics to: %s", Configuration.StatsDEndpoint)
} else {
m.statsdEnabled = false
}
go m.listenForMetrics()
return &m
}
func (m *MetricsManager) listenForMetrics() {
if m.statsdEnabled {
defer m.statsBuffer.Close()
}
var metric *Metric
for {
metric = <-m.metricsChannel
log.Debugf("Received metric: %s - %v", metric.Name, metric.Value)
if m.statsdEnabled {
log.Debugf("Logging metrics")
switch metric.Type {
case "counter":
m.statsBuffer.Incr(metric.Name, metric.Value)
case "guage":
m.statsBuffer.Gauge(metric.Name, metric.Value)
case "timing":
m.statsBuffer.Timing(metric.Name, metric.Value)
default:
log.Errorf("Unknown metric type received: %s", metric.Type)
}
}
stringToPublish := fmt.Sprintf("%s:%d", metric.Name, metric.Value)
messageHeaders := make(map[string]string)
messageBody := []byte(stringToPublish)
metricMessage := message.NewMessage(&messageHeaders, &messageBody)
m.queueManager.Publish(metricsQueueName, metricMessage)
}
}