-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecorder.go
198 lines (177 loc) · 6.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package main
import (
"fmt"
"time"
"github.com/prometheus/client_golang/prometheus"
)
const (
prefix = "" // TODO
)
type Recorder interface {
measureConnected(up int)
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
measureSpeedUp(speed int)
measureSpeedDown(speed int)
measureBytesUp(bytes int)
measureBytesDown(bytes int)
measureSysUptime(ts int)
measureConnUptime(ts int)
}
func NewRecorder(reg prometheus.Registerer) Recorder {
r := &prometheusRecorder{
btsmarthub2Polls: prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: prefix,
Name: "btsmarthub2_polls",
Help: "Number of polls we have attempted",
}, []string{"state"}),
btsmarthub2Connected: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: prefix,
Name: "btsmarthub2_connected",
Help: "Whether the Broadband is connected",
}),
btsmarthub2StatusCodeCount: prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: prefix,
Name: "btsmarthub2_status_code_count",
Help: "A count of each status code encountered",
}, []string{"statusCode"}),
btsmarthub2LastGoodPollSeconds: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: prefix,
Name: "btsmarthub2_last_good_poll_seconds",
Help: "The UNIX timestamp in seconds of the last good poll",
}),
btsmarthub2LastPollSeconds: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: prefix,
Name: "btsmarthub2_last_poll_seconds",
Help: "The UNIX timestamp in seconds of the last poll",
}),
btsmarthub2PollDurSeconds: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: prefix,
Name: "btsmarthub2_poll_duration",
Help: "The total duration of polling",
}),
btsmarthub2ParseDurSeconds: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: prefix,
Name: "btsmarthub2_parse_duration",
Help: "The total duration of parsing",
}),
btsmarthub2LastErrorTimeSeconds: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: prefix,
Name: "btsmarthub2_last_error_time_seconds",
Help: "The UNIX timestamp in seconds of the last error",
}),
btsmarthub2SpeedUp: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: prefix,
Name: "btsmarthub2_speed_up",
Help: "The upload broadband speed in bps",
}),
btsmarthub2SpeedDown: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: prefix,
Name: "btsmarthub2_speed_down",
Help: "The download broadband speed in bps",
}),
btsmarthub2BytesUp: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: prefix,
Name: "btsmarthub2_bytes_up",
Help: "The number of bytes uploaded",
}),
btsmarthub2BytesDown: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: prefix,
Name: "btsmarthub2_bytes_down",
Help: "The number of bytes downloaded",
}),
btsmarthub2SysUptimeSeconds: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: prefix,
Name: "btsmarthub2_sysuptime_seconds",
Help: "The sysuptime of the BB router in seconds",
}),
btsmarthub2ConnUptimeSeconds: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: prefix,
Name: "btsmarthub2_connuptime_seconds",
Help: "The connuptime of the BB router in seconds",
}),
}
reg.MustRegister(r.btsmarthub2Polls, r.btsmarthub2Connected, r.btsmarthub2StatusCodeCount, r.btsmarthub2LastGoodPollSeconds,
r.btsmarthub2LastPollSeconds, r.btsmarthub2PollDurSeconds, r.btsmarthub2ParseDurSeconds,
r.btsmarthub2LastErrorTimeSeconds, r.btsmarthub2SpeedUp, r.btsmarthub2SpeedDown, r.btsmarthub2BytesUp,
r.btsmarthub2BytesDown, r.btsmarthub2SysUptimeSeconds, r.btsmarthub2ConnUptimeSeconds)
return r
}
type prometheusRecorder struct {
btsmarthub2Polls *prometheus.CounterVec
btsmarthub2Connected prometheus.Gauge
btsmarthub2StatusCodeCount *prometheus.CounterVec
btsmarthub2LastGoodPollSeconds prometheus.Gauge
btsmarthub2LastPollSeconds prometheus.Gauge
btsmarthub2PollDurSeconds prometheus.Counter
btsmarthub2ParseDurSeconds prometheus.Counter
btsmarthub2LastErrorTimeSeconds prometheus.Gauge
btsmarthub2SpeedUp prometheus.Gauge
btsmarthub2SpeedDown prometheus.Gauge
btsmarthub2BytesUp prometheus.Gauge
btsmarthub2BytesDown prometheus.Gauge
btsmarthub2SysUptimeSeconds prometheus.Gauge
btsmarthub2ConnUptimeSeconds prometheus.Gauge
}
// Count the number of polls we have made
func (r prometheusRecorder) measurePolls(state string) {
r.btsmarthub2Polls.WithLabelValues(state).Inc()
}
// Whether the Broadband is connected
func (r prometheusRecorder) measureConnected(up int) {
r.btsmarthub2Connected.Set(float64(up))
}
// A count of each status code encountered
func (r prometheusRecorder) measureStatusCode(code int) {
codeStr := fmt.Sprintf("%d", code)
r.btsmarthub2StatusCodeCount.WithLabelValues(codeStr).Add(1)
}
// The UNIX timestamp in seconds of the last good poll
func (r prometheusRecorder) measureLastGoodPoll(ts time.Time) {
r.btsmarthub2LastGoodPollSeconds.Set(float64(ts.Unix()))
}
// The UNIX timestamp in seconds of the last poll
func (r prometheusRecorder) measureLastPoll(ts time.Time) {
r.btsmarthub2LastPollSeconds.Set(float64(ts.Unix()))
}
// The duration of the poll
func (r prometheusRecorder) measurePollDur(duration time.Duration) {
r.btsmarthub2PollDurSeconds.Add(duration.Seconds())
}
// The duration of the parse
func (r prometheusRecorder) measureParseDur(duration time.Duration) {
r.btsmarthub2ParseDurSeconds.Add(duration.Seconds())
}
// The UNIX timestamp in seconds of the last error
func (r prometheusRecorder) measureLastError(ts time.Time) {
r.btsmarthub2LastErrorTimeSeconds.Set(float64(ts.Unix()))
}
// The upload broadband speed in bps
func (r prometheusRecorder) measureSpeedUp(speed int) {
r.btsmarthub2SpeedUp.Set(float64(speed))
}
// The download broadband speed in bps
func (r prometheusRecorder) measureSpeedDown(speed int) {
r.btsmarthub2SpeedDown.Set(float64(speed))
}
// The number of bytes uploaded
func (r prometheusRecorder) measureBytesUp(bytes int) {
r.btsmarthub2BytesUp.Set(float64(bytes))
}
// The number of bytes downloaded
func (r prometheusRecorder) measureBytesDown(bytes int) {
r.btsmarthub2BytesDown.Set(float64(bytes))
}
// The sysuptime of the BB router in seconds
func (r prometheusRecorder) measureSysUptime(ts int) {
r.btsmarthub2SysUptimeSeconds.Set(float64(ts))
}
// The connuptime of the BB router in seconds
func (r prometheusRecorder) measureConnUptime(ts int) {
r.btsmarthub2ConnUptimeSeconds.Set(float64(ts))
}