-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathmetrics.go
69 lines (62 loc) · 2.78 KB
/
metrics.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
package nq
import (
"github.com/prometheus/client_golang/prometheus"
)
// Metrics provide insights to the current state of the app
type Metrics struct {
numStreams *prometheus.GaugeVec
streamDurationSec *prometheus.HistogramVec
receivedBytes *prometheus.CounterVec
receivedMsgs *prometheus.CounterVec
sentBytes *prometheus.CounterVec
sentMsgs *prometheus.CounterVec
lostBytes *prometheus.CounterVec
lostMsgs *prometheus.CounterVec
numReconnects *prometheus.CounterVec
}
const (
lAddr = "addr"
)
// NewMetrics constructs and registers Metrics
func NewMetrics(reg prometheus.Registerer) *Metrics {
const ns = "nanoq"
l := []string{lAddr}
metrics := &Metrics{
receivedBytes: prometheus.NewCounterVec(prometheus.CounterOpts{Namespace: ns, Name: "received_bytes"}, l),
receivedMsgs: prometheus.NewCounterVec(prometheus.CounterOpts{Namespace: ns, Name: "received_msgs"}, l),
sentBytes: prometheus.NewCounterVec(prometheus.CounterOpts{Namespace: ns, Name: "sent_bytes"}, l),
sentMsgs: prometheus.NewCounterVec(prometheus.CounterOpts{Namespace: ns, Name: "sent_msgs"}, l),
lostBytes: prometheus.NewCounterVec(prometheus.CounterOpts{Namespace: ns, Name: "lost_bytes"}, l),
lostMsgs: prometheus.NewCounterVec(prometheus.CounterOpts{Namespace: ns, Name: "lost_msgs"}, l),
numReconnects: prometheus.NewCounterVec(prometheus.CounterOpts{Namespace: ns, Name: "reconnects"}, l),
numStreams: prometheus.NewGaugeVec(prometheus.GaugeOpts{Namespace: ns, Name: "streams"}, l),
streamDurationSec: prometheus.NewHistogramVec(prometheus.HistogramOpts{Namespace: ns, Name: "stream_duration_sec", Buckets: dayOfSecondsBuckets()}, l),
}
reg.MustRegister(metrics.receivedBytes)
reg.MustRegister(metrics.receivedMsgs)
reg.MustRegister(metrics.sentBytes)
reg.MustRegister(metrics.sentMsgs)
reg.MustRegister(metrics.lostBytes)
reg.MustRegister(metrics.lostMsgs)
reg.MustRegister(metrics.numReconnects)
reg.MustRegister(metrics.numStreams)
reg.MustRegister(metrics.streamDurationSec)
return metrics
}
// NewDefaultMetrics constructs and registers Metrics with the prometheus.DefaultRegisterer
func NewDefaultMetrics() *Metrics {
return NewMetrics(prometheus.DefaultRegisterer)
}
// dayOfSecondsBuckets for streamDurationSec histogram
func dayOfSecondsBuckets() []float64 {
var (
unit float64 = 1
buckets []float64
)
buckets = append(buckets, []float64{1 * unit, 2 * unit, 5 * unit, 10 * unit, 15 * unit, 30 * unit, 45 * unit}...) // sec
unit *= 60
buckets = append(buckets, []float64{1 * unit, 2 * unit, 5 * unit, 10 * unit, 15 * unit, 30 * unit, 45 * unit}...) // min
unit *= 60
buckets = append(buckets, []float64{1 * unit, 2 * unit, 5 * unit, 12 * unit, 18 * unit, 24 * unit}...) // hr
return buckets
}