/
metrics.go
339 lines (277 loc) · 9.57 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
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
package stoserver
import (
"context"
"encoding/json"
"io"
"io/ioutil"
"net/http"
"strconv"
"sync"
"sync/atomic"
"time"
"github.com/felixge/httpsnoop"
"github.com/function61/gokit/promconstmetrics"
"github.com/function61/varasto/pkg/blobstore"
"github.com/function61/varasto/pkg/stoserver/stodb"
"github.com/function61/varasto/pkg/stoserver/stoservertypes"
"github.com/function61/varasto/pkg/stotypes"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"go.etcd.io/bbolt"
)
type metricsController struct {
registry *prometheus.Registry
volumes map[int]*volumeMetrics
httpRequests *prometheus.CounterVec
scheduledJobRuntime *promconstmetrics.Ref
constMetricsCollector *promconstmetrics.Collector
}
// metrics for a single volume
type volumeMetrics struct {
// const metrics b/c these are difficult / non-performant to populate in realtime, so
// there are refreshed at interval, but readings can be much older (like SMART temperature
// reading) and thus they have a specific "value at" timestamp
blobs *promconstmetrics.Ref
spaceUsed *promconstmetrics.Ref
spaceFree *promconstmetrics.Ref
replicationProgress *promconstmetrics.Ref
temperature *promconstmetrics.Ref
// using (totalRequests, errors) instead of (successes, errors) b/c:
// https://promcon.io/2017-munich/slides/best-practices-and-beastly-pitfalls.pdf
readRequests prometheus.Counter
readBytes prometheus.Counter
readErrors prometheus.Counter
writeRequests prometheus.Counter
writtenBytes prometheus.Counter
writeErrors prometheus.Counter
}
func newMetricsController() *metricsController {
reg := prometheus.NewRegistry()
constMetricsCollector := promconstmetrics.NewCollector()
m := &metricsController{
registry: reg,
volumes: map[int]*volumeMetrics{},
httpRequests: prometheus.NewCounterVec(prometheus.CounterOpts{
Name: "sto_http_requests_total",
Help: "HTTP server's handled requests",
}, []string{"code", "method"}),
scheduledJobRuntime: constMetricsCollector.Register("sto_scheduledjob_runtime_seconds", "Scheduled job's runtime (seconds)", prometheus.Labels{}, "job"),
constMetricsCollector: constMetricsCollector,
}
reg.MustRegister(m.httpRequests)
reg.MustRegister(m.constMetricsCollector)
return m
}
// builds a cancellable metrics collection task that can be given to taskrunner
func (m *metricsController) Task(conf *ServerConfig, db *bbolt.DB) func(context.Context) error {
return func(ctx context.Context) error {
metricsCollectionInterval := time.NewTicker(5 * time.Second)
for {
select {
case <-ctx.Done():
return nil
case <-metricsCollectionInterval.C:
if err := m.collectMetrics(conf, db); err != nil {
return err
}
}
}
}
}
func (m *metricsController) MetricsHttpHandler() http.Handler {
return promhttp.HandlerFor(m.registry, promhttp.HandlerOpts{})
}
// instruments a HTTP handler
func (m *metricsController) WrapHttpServer(actual http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
stats := httpsnoop.CaptureMetrics(actual, w, r)
m.httpRequests.With(prometheus.Labels{
"code": strconv.Itoa(stats.Code),
"method": r.Method,
}).Inc()
})
}
// decorates a blobstore driver with a proxy driver that doesn't change any behaviour, but
// records metrics for the operations
func (m *metricsController) WrapDriver(
origin blobstore.Driver,
volId int,
volUuid string,
volLabel string,
) blobstore.Driver {
volMetrics := m.createVolumeMetrics(volUuid, volLabel)
m.volumes[volId] = volMetrics
return &proxyDriver{origin, volMetrics}
}
func (m *metricsController) createVolumeMetrics(volUuid string, volLabel string) *volumeMetrics {
// shorthand for new'ing and registering
counter := func(opts prometheus.CounterOpts) prometheus.Counter {
c := prometheus.NewCounter(opts)
m.registry.MustRegister(c)
return c
}
volMetricLabels := prometheus.Labels{
"uuid": volUuid,
"label": volLabel,
}
constMetrics := m.constMetricsCollector // shorthand
vm := &volumeMetrics{
blobs: constMetrics.Register("sto_vol_blobs", "Blobs in a given volume", volMetricLabels),
spaceUsed: constMetrics.Register("sto_vol_space_used_bytes", "Actual used space (after deduplication & compression)", volMetricLabels),
spaceFree: constMetrics.Register("sto_vol_space_free_bytes", "Free space (quota - used)", volMetricLabels),
replicationProgress: constMetrics.Register("sto_vol_replication_progress", "Volume replication controller's progress %", volMetricLabels),
temperature: constMetrics.Register("sto_vol_temperature_celsius", "Disk temperature", volMetricLabels),
readRequests: counter(prometheus.CounterOpts{
Name: "sto_vol_read_requests_total",
Help: "Volume read operations (incl. errors)",
ConstLabels: volMetricLabels,
}),
readBytes: counter(prometheus.CounterOpts{
Name: "sto_vol_read_bytes_total",
Help: "Volume read bytes",
ConstLabels: volMetricLabels,
}),
readErrors: counter(prometheus.CounterOpts{
Name: "sto_vol_read_errors_total",
Help: "Volume failed read operations",
ConstLabels: volMetricLabels,
}),
writeRequests: counter(prometheus.CounterOpts{
Name: "sto_vol_write_requests_total",
Help: "Volume write operations (incl. errors)",
ConstLabels: volMetricLabels,
}),
writtenBytes: counter(prometheus.CounterOpts{
Name: "sto_vol_write_bytes_total",
Help: "Volume written bytes",
ConstLabels: volMetricLabels,
}),
writeErrors: counter(prometheus.CounterOpts{
Name: "sto_vol_write_errors_total",
Help: "Volume failed write operations",
ConstLabels: volMetricLabels,
}),
}
return vm
}
func (m *metricsController) collectMetrics(conf *ServerConfig, db *bbolt.DB) error {
tx, rollback, err := readTx(db)
if err != nil {
return err
}
defer rollback()
now := time.Now()
constMetrics := m.constMetricsCollector // shorthand
for volId, volMetrics := range m.volumes {
vol, err := stodb.Read(tx).Volume(volId)
if err != nil {
return err
}
constMetrics.Observe(volMetrics.blobs, float64(vol.BlobCount), now)
constMetrics.Observe(volMetrics.spaceUsed, float64(vol.BlobSizeTotal), now)
constMetrics.Observe(volMetrics.spaceFree, float64(vol.Quota-vol.BlobSizeTotal), now)
if ctrl, has := conf.ReplicationControllers[vol.ID]; has {
constMetrics.Observe(volMetrics.replicationProgress, float64(ctrl.Progress())/100, now)
}
// TODO: filter out old SMART reports
if vol.SmartReport != "" {
report := &stoservertypes.SmartReport{}
if err := json.Unmarshal([]byte(vol.SmartReport), report); err != nil {
return err
}
if report.Temperature != nil {
constMetrics.Observe(volMetrics.temperature, float64(*report.Temperature), report.Time)
}
}
}
jobs := []stotypes.ScheduledJob{}
if err := stodb.ScheduledJobRepository.Each(stodb.ScheduledJobAppender(&jobs), tx); err != nil {
return err
}
for _, job := range jobs {
if job.LastRun != nil {
lastrun := job.LastRun // shorthand
constMetrics.Observe(m.scheduledJobRuntime, lastrun.Runtime().Seconds(), lastrun.Finished, job.Description)
}
}
return nil
}
// wraps origin store in a new driver that doesn't change the behaviour, but is used to
// record metrics
type proxyDriver struct {
blobstore.Driver
volMetrics *volumeMetrics
}
func (p *proxyDriver) RawStore(ctx context.Context, ref stotypes.BlobRef, content io.Reader) error {
p.volMetrics.writeRequests.Inc()
err := p.Driver.RawStore(ctx, ref, newReadCounter(content, func(bytesRead int64, errRead error) {
if errRead == nil {
// not all succesfully read bytes were necessarily written to the volume in error
// cases, but this is the least invasive way grab this info where and it's accurate
// on successes
p.volMetrics.writtenBytes.Add(float64(bytesRead))
}
}))
if err != nil {
p.volMetrics.writeErrors.Inc()
}
return err
}
func (p *proxyDriver) RawFetch(ctx context.Context, ref stotypes.BlobRef) (io.ReadCloser, error) {
// will be called (once) much later than we return from this func
readFinished := func(bytesRead int64, err error) {
p.volMetrics.readBytes.Add(float64(bytesRead))
if err != nil {
p.volMetrics.readErrors.Inc()
}
}
p.volMetrics.readRequests.Inc()
content, err := p.Driver.RawFetch(ctx, ref)
contentMaybeWrapped := content
if err != nil {
readFinished(0, err)
} else {
contentMaybeWrapped = newReadCounter(content, readFinished)
}
return contentMaybeWrapped, err
}
type readCounter struct {
bytesRead int64 // has to be first b/c sync/atomic alignment rules
io.ReadCloser
stats func(int64, error)
statsOnce sync.Once
}
// "stats" will only be called once, and when:
// a) all reads succeeded to io.EOF OR
// b) first read failed
func newReadCounter(content io.Reader, stats func(int64, error)) io.ReadCloser {
rc, ok := content.(io.ReadCloser)
if !ok {
rc = ioutil.NopCloser(content)
}
return &readCounter{
ReadCloser: rc,
stats: stats,
}
}
var _ io.ReadCloser = (*readCounter)(nil)
func (r *readCounter) Read(p []byte) (int, error) {
n, err := r.ReadCloser.Read(p)
// probably won't be used concurrently, but let's be safe anyway
atomic.AddInt64(&r.bytesRead, int64(n))
// stream read will always end up in either..
if err != nil {
if err == io.EOF { // a) pseudo-error
_ = r.emitStats(nil)
} else { // b) actual error
_ = r.emitStats(err)
}
}
return n, err
}
func (r *readCounter) emitStats(err error) error {
r.statsOnce.Do(func() {
r.stats(r.bytesRead, err)
})
return err
}