-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstats_publisher.go
106 lines (94 loc) · 3.12 KB
/
stats_publisher.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
package main
import (
"os"
"fmt"
"time"
"errors"
"github.com/quipo/statsd"
"github.com/golang/glog"
"github.com/prometheus/client_golang/prometheus"
)
type prometheusPublisher struct {
counters map[string]*prometheus.CounterVec
summaries map[string]*prometheus.SummaryVec
}
type StatsPublisher struct {
To string
NodeName string
prometheus *prometheusPublisher
statsd *statsd.StatsdClient
}
func NewStatsPublisher(to string) (*StatsPublisher, error) {
nodeName := os.Getenv("NODE_NAME")
publisher := &StatsPublisher{To: to, NodeName: nodeName}
switch to {
case "prometheus":
publisher.prometheus = &prometheusPublisher{
counters: make(map[string]*prometheus.CounterVec),
summaries: make(map[string]*prometheus.SummaryVec),
}
case "statsd":
prefix := "hyperpilot.resource-worker-service."
statsdHost := os.Getenv("STATSD_HOST")
if statsdHost == "" {
return nil, errors.New("STATSD_HOST environment variable must be defined")
}
statsdPort := os.Getenv("STATSD_PORT")
if statsdPort == "" {
glog.Warning("STATSD_PORT environment variable not defined, using default 8125")
statsdPort = "8125"
}
statsdUrl := fmt.Sprintf("%s:%s", statsdHost, statsdPort)
statsdClient := statsd.NewStatsdClient(statsdUrl, prefix)
if err := statsdClient.CreateSocket(); err != nil {
return nil, errors.New("Failed to create statsd client: " + err.Error())
}
publisher.statsd = statsdClient
default:
return nil, errors.New("Unrecognized stats publisher service: " + to)
}
return publisher, nil
}
func (publisher *StatsPublisher) RegisterCounter(metric string, help string) {
if publisher.To == "prometheus" {
publisher.prometheus.counters[metric] = prometheus.NewCounterVec(
prometheus.CounterOpts{Name: metric, Help: help},
[]string{"label", "node"},
)
prometheus.MustRegister(publisher.prometheus.counters[metric])
}
}
func (publisher *StatsPublisher) RegisterTimer(metric string, help string) {
if publisher.To == "prometheus" {
publisher.prometheus.summaries[metric] = prometheus.NewSummaryVec(
prometheus.SummaryOpts{Name: metric, Help: help},
[]string{"label", "node"},
)
prometheus.MustRegister(publisher.prometheus.summaries[metric])
}
}
func (publisher *StatsPublisher) makeStatsdMetric(metric string, label string) string {
return fmt.Sprintf("%s.%s.%s", metric, publisher.NodeName, label)
}
func (publisher *StatsPublisher) Inc(metric string, label string) error {
switch publisher.To {
case "prometheus":
publisher.prometheus.counters[metric].With(
prometheus.Labels{"label": label, "node": publisher.NodeName},
).Inc()
case "statsd":
return publisher.statsd.Incr(publisher.makeStatsdMetric(metric, label), 1)
}
return nil
}
func (publisher *StatsPublisher) Timing(metric string, label string, duration time.Duration) error {
switch publisher.To {
case "prometheus":
publisher.prometheus.summaries[metric].With(
prometheus.Labels{"label": label, "node": publisher.NodeName},
).Observe(duration.Seconds() * 1000)
case "statsd":
return publisher.statsd.Timing(publisher.makeStatsdMetric(metric, label), int64(duration.Seconds() * 1000))
}
return nil
}