-
Notifications
You must be signed in to change notification settings - Fork 0
/
recorder.go
149 lines (132 loc) · 4.73 KB
/
recorder.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
145
146
147
148
149
package main
import (
"fmt"
"time"
"github.com/prometheus/client_golang/prometheus"
)
const (
prefix = "elgato_keylight" // TODO - ability to specify/override
)
type Recorder interface {
measurePolls(state string)
measureStatusCode(code int)
measureLastGoodPoll(ts time.Time)
measureLastPoll(ts time.Time) // Time (UTC) of last poll
measurePollDur(duration time.Duration)
measureParseDur(duration time.Duration)
measureLastError(ts time.Time) // Time (UTC) of last errored poll
measureOnOff(onoff int)
measureBrightness(val int)
measureTemperature(temp int)
}
func NewRecorder(reg prometheus.Registerer) Recorder {
r := &prometheusRecorder{
elgatoPolls: prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: prefix,
Name: "polls",
Help: "Number of polls we have attempted",
}, []string{"state"}),
elgatoStatusCodeCount: prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: prefix,
Name: "status_code_count",
Help: "A count of each status code encountered",
}, []string{"statusCode"}),
elgatoLastGoodPollSeconds: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: prefix,
Name: "last_good_poll_seconds",
Help: "The UNIX timestamp in seconds of the last good poll",
}),
elgatoLastPollSeconds: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: prefix,
Name: "last_poll_seconds",
Help: "The UNIX timestamp in seconds of the last poll",
}),
elgatoPollDurSeconds: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: prefix,
Name: "poll_duration",
Help: "The total duration of polling",
}),
elgatoParseDurSeconds: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: prefix,
Name: "parse_duration",
Help: "The total duration of parsing",
}),
elgatoLastErrorTimeSeconds: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: prefix,
Name: "last_error_time_seconds",
Help: "The UNIX timestamp in seconds of the last error",
}),
elgatoOnOff: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: prefix,
Name: "onoff",
Help: "Whether the keylight is on or off",
}),
elgatoBrightness: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: prefix,
Name: "brightness",
Help: "The brightness of the keylight",
}),
elgatoTemperature: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: prefix,
Name: "temperature",
Help: "The temperature of the keylight",
}),
}
reg.MustRegister(r.elgatoPolls, r.elgatoStatusCodeCount, r.elgatoLastGoodPollSeconds,
r.elgatoLastPollSeconds, r.elgatoPollDurSeconds, r.elgatoParseDurSeconds,
r.elgatoLastErrorTimeSeconds, r.elgatoOnOff, r.elgatoBrightness, r.elgatoTemperature)
return r
}
type prometheusRecorder struct {
elgatoPolls *prometheus.CounterVec
elgatoStatusCodeCount *prometheus.CounterVec
elgatoLastGoodPollSeconds prometheus.Gauge
elgatoLastPollSeconds prometheus.Gauge
elgatoPollDurSeconds prometheus.Counter
elgatoParseDurSeconds prometheus.Counter
elgatoLastErrorTimeSeconds prometheus.Gauge
elgatoOnOff prometheus.Gauge
elgatoBrightness prometheus.Gauge
elgatoTemperature prometheus.Gauge
}
// Count the number of polls we have made
func (r prometheusRecorder) measurePolls(state string) {
r.elgatoPolls.WithLabelValues(state).Inc()
}
// A count of each status code encountered
func (r prometheusRecorder) measureStatusCode(code int) {
codeStr := fmt.Sprintf("%d", code)
r.elgatoStatusCodeCount.WithLabelValues(codeStr).Add(1)
}
// The UNIX timestamp in seconds of the last good poll
func (r prometheusRecorder) measureLastGoodPoll(ts time.Time) {
r.elgatoLastGoodPollSeconds.Set(float64(ts.Unix()))
}
// The UNIX timestamp in seconds of the last poll
func (r prometheusRecorder) measureLastPoll(ts time.Time) {
r.elgatoLastPollSeconds.Set(float64(ts.Unix()))
}
// The duration of the poll
func (r prometheusRecorder) measurePollDur(duration time.Duration) {
r.elgatoPollDurSeconds.Add(duration.Seconds())
}
// The duration of the parse
func (r prometheusRecorder) measureParseDur(duration time.Duration) {
r.elgatoParseDurSeconds.Add(duration.Seconds())
}
// The UNIX timestamp in seconds of the last error
func (r prometheusRecorder) measureLastError(ts time.Time) {
r.elgatoLastErrorTimeSeconds.Set(float64(ts.Unix()))
}
// Whether the keylight is on or off
func (r prometheusRecorder) measureOnOff(onoff int) {
r.elgatoOnOff.Set(float64(onoff))
}
// The brightness of the keylight
func (r prometheusRecorder) measureBrightness(bright int) {
r.elgatoBrightness.Set(float64(bright))
}
// The temperature of the keylight
func (r prometheusRecorder) measureTemperature(temp int) {
r.elgatoTemperature.Set(float64(temp))
}