-
Notifications
You must be signed in to change notification settings - Fork 496
/
metrics.go
543 lines (487 loc) · 16.6 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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
package db
import (
"context"
"database/sql"
"errors"
"fmt"
"strings"
"time"
"github.com/Masterminds/squirrel"
"github.com/lib/pq"
"github.com/prometheus/client_golang/prometheus"
)
type CtxKey string
var RouteContextKey = CtxKey("route")
var QueryTypeContextKey = CtxKey("query_type")
type Subservice string
var CoreSubservice = Subservice("core")
var HistoryPrimarySubservice = Subservice("history_primary")
var HistorySubservice = Subservice("history")
var IngestSubservice = Subservice("ingest")
type QueryType string
var AdvisoryLockQueryType = QueryType("advisory_lock")
var DeleteQueryType = QueryType("delete")
var InsertQueryType = QueryType("insert")
var SelectQueryType = QueryType("select")
var UndefinedQueryType = QueryType("undefined")
var UpdateQueryType = QueryType("update")
var UpsertQueryType = QueryType("upsert")
// contextRoute returns a string representing the request endpoint, or "undefined" if it wasn't found
func contextRoute(ctx context.Context) string {
if endpoint, ok := ctx.Value(&RouteContextKey).(string); ok {
return endpoint
}
return "undefined"
}
type SessionWithMetrics struct {
SessionInterface
registry *prometheus.Registry
queryCounter *prometheus.CounterVec
queryDurationSummary *prometheus.SummaryVec
maxOpenConnectionsGauge prometheus.GaugeFunc
openConnectionsGauge prometheus.GaugeFunc
inUseConnectionsGauge prometheus.GaugeFunc
idleConnectionsGauge prometheus.GaugeFunc
waitCountCounter prometheus.CounterFunc
waitDurationCounter prometheus.CounterFunc
maxIdleClosedCounter prometheus.CounterFunc
maxIdleTimeClosedCounter prometheus.CounterFunc
maxLifetimeClosedCounter prometheus.CounterFunc
roundTripProbe *roundTripProbe
roundTripTimeSummary prometheus.Summary
errorCounter *prometheus.CounterVec
}
func RegisterMetrics(base *Session, namespace string, sub Subservice, registry *prometheus.Registry) SessionInterface {
s := &SessionWithMetrics{
SessionInterface: base,
registry: registry,
}
base.AddErrorHandler(s.handleErrorEvent)
s.queryCounter = prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: namespace,
Subsystem: "db",
Name: "query_total",
ConstLabels: prometheus.Labels{"subservice": string(sub)},
},
[]string{"query_type", "error", "route"},
)
registry.MustRegister(s.queryCounter)
s.queryDurationSummary = prometheus.NewSummaryVec(
prometheus.SummaryOpts{
Namespace: namespace, Subsystem: "db",
Name: "query_duration_seconds",
ConstLabels: prometheus.Labels{"subservice": string(sub)},
Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001},
},
[]string{"query_type", "error", "route"},
)
registry.MustRegister(s.queryDurationSummary)
// txnCounter: prometheus.NewCounter(
// prometheus.CounterOpts{Namespace: namespace, Subsystem: "db", Name: "transaction_total"},
// ),
// registry.MustRegister(s.txnCounter)
// txnDuration: prometheus.NewHistogram(
// prometheus.HistogramOpts{
// Namespace: namespace, Subsystem: "db",
// Name: "transaction_duration_seconds",
// Buckets: prometheus.ExponentialBuckets(0.1, 3, 5),
// },
// ),
// registry.MustRegister(s.txnDuration)
s.maxOpenConnectionsGauge = prometheus.NewGaugeFunc(
prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: "db",
Name: "max_open_connections",
ConstLabels: prometheus.Labels{"subservice": string(sub)},
},
func() float64 {
// Right now MaxOpenConnections in Horizon is static however it's possible that
// it will change one day. In such case, using GaugeFunc is very cheap and will
// prevent issues with this metric in the future.
return float64(base.DB.Stats().MaxOpenConnections)
},
)
registry.MustRegister(s.maxOpenConnectionsGauge)
s.openConnectionsGauge = prometheus.NewGaugeFunc(
prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: "db",
Name: "open_connections",
ConstLabels: prometheus.Labels{"subservice": string(sub)},
},
func() float64 {
return float64(base.DB.Stats().OpenConnections)
},
)
registry.MustRegister(s.openConnectionsGauge)
s.inUseConnectionsGauge = prometheus.NewGaugeFunc(
prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: "db",
Name: "in_use_connections",
ConstLabels: prometheus.Labels{"subservice": string(sub)},
},
func() float64 {
return float64(base.DB.Stats().InUse)
},
)
registry.MustRegister(s.inUseConnectionsGauge)
s.idleConnectionsGauge = prometheus.NewGaugeFunc(
prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: "db",
Name: "idle_connections",
ConstLabels: prometheus.Labels{"subservice": string(sub)},
},
func() float64 {
return float64(base.DB.Stats().Idle)
},
)
registry.MustRegister(s.idleConnectionsGauge)
s.waitCountCounter = prometheus.NewCounterFunc(
prometheus.CounterOpts{
Namespace: namespace,
Subsystem: "db",
Name: "wait_count_total",
Help: "total number of number of connections waited for",
ConstLabels: prometheus.Labels{"subservice": string(sub)},
},
func() float64 {
return float64(base.DB.Stats().WaitCount)
},
)
registry.MustRegister(s.waitCountCounter)
s.waitDurationCounter = prometheus.NewCounterFunc(
prometheus.CounterOpts{
Namespace: namespace,
Subsystem: "db",
Name: "wait_duration_seconds_total",
Help: "total time blocked waiting for a new connection",
ConstLabels: prometheus.Labels{"subservice": string(sub)},
},
func() float64 {
return base.DB.Stats().WaitDuration.Seconds()
},
)
registry.MustRegister(s.waitDurationCounter)
s.maxIdleClosedCounter = prometheus.NewCounterFunc(
prometheus.CounterOpts{
Namespace: namespace,
Subsystem: "db",
Name: "max_idle_closed_total",
Help: "total number of number of connections closed due to SetMaxIdleConns",
ConstLabels: prometheus.Labels{"subservice": string(sub)},
},
func() float64 {
return float64(base.DB.Stats().MaxIdleClosed)
},
)
registry.MustRegister(s.maxIdleClosedCounter)
s.maxIdleTimeClosedCounter = prometheus.NewCounterFunc(
prometheus.CounterOpts{
Namespace: namespace,
Subsystem: "db",
Name: "max_idle_time_closed_total",
Help: "total number of number of connections closed due to SetConnMaxIdleTime",
ConstLabels: prometheus.Labels{"subservice": string(sub)},
},
func() float64 {
return float64(base.DB.Stats().MaxIdleTimeClosed)
},
)
registry.MustRegister(s.maxIdleTimeClosedCounter)
s.maxLifetimeClosedCounter = prometheus.NewCounterFunc(
prometheus.CounterOpts{
Namespace: namespace,
Subsystem: "db",
Name: "max_lifetime_closed_total",
Help: "total number of number of connections closed due to SetConnMaxLifetime",
ConstLabels: prometheus.Labels{"subservice": string(sub)},
},
func() float64 {
return float64(base.DB.Stats().MaxLifetimeClosed)
},
)
registry.MustRegister(s.maxLifetimeClosedCounter)
s.errorCounter = prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: namespace,
Subsystem: "db",
Name: "error_total",
Help: "total number of db related errors, details are captured in labels",
ConstLabels: prometheus.Labels{"subservice": string(sub)},
},
[]string{"ctx_error", "db_error", "db_error_extra"},
)
registry.MustRegister(s.errorCounter)
s.roundTripTimeSummary = prometheus.NewSummary(
prometheus.SummaryOpts{
Namespace: namespace,
Subsystem: "db",
Name: "round_trip_time_seconds",
Help: "time required to run `select 1` query in a DB - effectively measures round trip time, if time exceeds 1s it will be recorded as 1",
ConstLabels: prometheus.Labels{"subservice": string(sub)},
Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001},
},
)
registry.MustRegister(s.roundTripTimeSummary)
s.roundTripProbe = &roundTripProbe{
session: base,
roundTripTimeSummary: s.roundTripTimeSummary,
}
s.roundTripProbe.start()
return s
}
func (s *SessionWithMetrics) Close() error {
s.roundTripProbe.close()
s.registry.Unregister(s.queryCounter)
s.registry.Unregister(s.queryDurationSummary)
// s.registry.Unregister(s.txnCounter)
// s.registry.Unregister(s.txnDurationSummary)
s.registry.Unregister(s.maxOpenConnectionsGauge)
s.registry.Unregister(s.openConnectionsGauge)
s.registry.Unregister(s.inUseConnectionsGauge)
s.registry.Unregister(s.idleConnectionsGauge)
s.registry.Unregister(s.waitCountCounter)
s.registry.Unregister(s.waitDurationCounter)
s.registry.Unregister(s.maxIdleClosedCounter)
s.registry.Unregister(s.maxIdleTimeClosedCounter)
s.registry.Unregister(s.maxLifetimeClosedCounter)
s.registry.Unregister(s.errorCounter)
return s.SessionInterface.Close()
}
func (s *SessionWithMetrics) TruncateTables(ctx context.Context, tables []string) (err error) {
timer := prometheus.NewTimer(prometheus.ObserverFunc(func(v float64) {
s.queryDurationSummary.With(prometheus.Labels{
"query_type": "truncate_tables",
"error": fmt.Sprint(err != nil),
"route": contextRoute(ctx),
}).Observe(v)
}))
defer func() {
timer.ObserveDuration()
s.queryCounter.With(prometheus.Labels{
"query_type": "truncate_tables",
"error": fmt.Sprint(err != nil),
"route": contextRoute(ctx),
}).Inc()
}()
err = s.SessionInterface.TruncateTables(ctx, tables)
return err
}
func (s *SessionWithMetrics) Clone() SessionInterface {
return &SessionWithMetrics{
SessionInterface: s.SessionInterface.Clone(),
// Note that cloned Session will point at the same roundTripProbe
// to avoid starting multiple go routines.
roundTripProbe: s.roundTripProbe,
registry: s.registry,
queryCounter: s.queryCounter,
queryDurationSummary: s.queryDurationSummary,
// txnCounter: s.txnCounter,
// txnDurationSummary: s.txnDurationSummary,
maxOpenConnectionsGauge: s.maxOpenConnectionsGauge,
openConnectionsGauge: s.openConnectionsGauge,
inUseConnectionsGauge: s.inUseConnectionsGauge,
idleConnectionsGauge: s.idleConnectionsGauge,
waitCountCounter: s.waitCountCounter,
waitDurationCounter: s.waitDurationCounter,
maxIdleClosedCounter: s.maxIdleClosedCounter,
maxIdleTimeClosedCounter: s.maxIdleTimeClosedCounter,
maxLifetimeClosedCounter: s.maxLifetimeClosedCounter,
errorCounter: s.errorCounter,
}
}
func getQueryType(ctx context.Context, query squirrel.Sqlizer) QueryType {
// Do we have an explicit query type set in the context? For raw execs, in
// lieu of better detection. e.g. "upsert"
if q, ok := ctx.Value(&QueryTypeContextKey).(QueryType); ok {
return q
}
// is it a squirrel builder?
if _, ok := query.(squirrel.DeleteBuilder); ok {
return DeleteQueryType
}
if _, ok := query.(squirrel.InsertBuilder); ok {
return InsertQueryType
}
if _, ok := query.(squirrel.SelectBuilder); ok {
return SelectQueryType
}
if _, ok := query.(squirrel.UpdateBuilder); ok {
return UpdateQueryType
}
// Try to guess based on the first word of the string.
// e.g. "SELECT * FROM table"
str, _, err := query.ToSql()
words := strings.Fields(strings.TrimSpace(strings.ToLower(str)))
if err == nil && len(words) > 0 {
// Make sure we don't only get known keywords here, incase it's a more
// complex query.
for _, word := range []string{"delete", "insert", "select", "update"} {
if word == words[0] {
return QueryType(word)
}
}
}
// Fresh out of ideas.
return UndefinedQueryType
}
// derive the db 'error_total' metric from the err returned by libpq
//
// dbErr - the error returned by any libpq method call
// ctx - the caller's context used on libpb method call
func (s *SessionWithMetrics) handleErrorEvent(dbErr error, ctx context.Context) {
if dbErr == nil || s.NoRows(dbErr) {
return
}
ctxError := "n/a"
dbError := "other"
errorExtra := "n/a"
var pqErr *pq.Error
switch {
case errors.As(dbErr, &pqErr):
dbError = string(pqErr.Code)
switch pqErr.Message {
case "canceling statement due to user request":
errorExtra = "user_request"
case "canceling statement due to statement timeout":
errorExtra = "statement_timeout"
}
case strings.Contains(dbErr.Error(), "driver: bad connection"):
dbError = "driver_bad_connection"
case strings.Contains(dbErr.Error(), "sql: transaction has already been committed or rolled back"):
dbError = "tx_already_rollback"
case errors.Is(dbErr, context.Canceled):
dbError = "canceled"
case errors.Is(dbErr, context.DeadlineExceeded):
dbError = "deadline_exceeded"
}
switch {
case errors.Is(ctx.Err(), context.Canceled):
ctxError = "canceled"
case errors.Is(ctx.Err(), context.DeadlineExceeded):
ctxError = "deadline_exceeded"
}
s.errorCounter.With(prometheus.Labels{
"ctx_error": ctxError,
"db_error": dbError,
"db_error_extra": errorExtra,
}).Inc()
}
func (s *SessionWithMetrics) Get(ctx context.Context, dest interface{}, query squirrel.Sqlizer) (err error) {
queryType := string(getQueryType(ctx, query))
timer := prometheus.NewTimer(prometheus.ObserverFunc(func(v float64) {
s.queryDurationSummary.With(prometheus.Labels{
"query_type": queryType,
"error": fmt.Sprint(err != nil),
"route": contextRoute(ctx),
}).Observe(v)
}))
defer func() {
timer.ObserveDuration()
s.queryCounter.With(prometheus.Labels{
"query_type": queryType,
"error": fmt.Sprint(err != nil),
"route": contextRoute(ctx),
}).Inc()
}()
err = s.SessionInterface.Get(ctx, dest, query)
return err
}
func (s *SessionWithMetrics) GetRaw(ctx context.Context, dest interface{}, query string, args ...interface{}) (err error) {
return s.Get(ctx, dest, squirrel.Expr(query, args...))
}
func (s *SessionWithMetrics) Select(ctx context.Context, dest interface{}, query squirrel.Sqlizer) (err error) {
queryType := string(getQueryType(ctx, query))
timer := prometheus.NewTimer(prometheus.ObserverFunc(func(v float64) {
s.queryDurationSummary.With(prometheus.Labels{
"query_type": queryType,
"error": fmt.Sprint(err != nil),
"route": contextRoute(ctx),
}).Observe(v)
}))
defer func() {
timer.ObserveDuration()
s.queryCounter.With(prometheus.Labels{
"query_type": queryType,
"error": fmt.Sprint(err != nil),
"route": contextRoute(ctx),
}).Inc()
}()
err = s.SessionInterface.Select(ctx, dest, query)
return err
}
func (s *SessionWithMetrics) SelectRaw(ctx context.Context, dest interface{}, query string, args ...interface{}) (err error) {
return s.Select(ctx, dest, squirrel.Expr(query, args...))
}
func (s *SessionWithMetrics) Exec(ctx context.Context, query squirrel.Sqlizer) (result sql.Result, err error) {
queryType := string(getQueryType(ctx, query))
timer := prometheus.NewTimer(prometheus.ObserverFunc(func(v float64) {
s.queryDurationSummary.With(prometheus.Labels{
"query_type": queryType,
"error": fmt.Sprint(err != nil),
"route": contextRoute(ctx),
}).Observe(v)
}))
defer func() {
timer.ObserveDuration()
s.queryCounter.With(prometheus.Labels{
"query_type": queryType,
"error": fmt.Sprint(err != nil),
"route": contextRoute(ctx),
}).Inc()
}()
result, err = s.SessionInterface.Exec(ctx, query)
return result, err
}
func (s *SessionWithMetrics) ExecRaw(ctx context.Context, query string, args ...interface{}) (result sql.Result, err error) {
return s.Exec(ctx, squirrel.Expr(query, args...))
}
func (s *SessionWithMetrics) Ping(ctx context.Context, timeout time.Duration) (err error) {
queryType := "ping"
timer := prometheus.NewTimer(prometheus.ObserverFunc(func(v float64) {
s.queryDurationSummary.With(prometheus.Labels{
"query_type": queryType,
"error": fmt.Sprint(err != nil),
"route": contextRoute(ctx),
}).Observe(v)
}))
defer func() {
timer.ObserveDuration()
s.queryCounter.With(prometheus.Labels{
"query_type": queryType,
"error": fmt.Sprint(err != nil),
"route": contextRoute(ctx),
}).Inc()
}()
err = s.SessionInterface.Ping(ctx, timeout)
return err
}
func (s *SessionWithMetrics) DeleteRange(
ctx context.Context,
start, end int64,
table string,
idCol string,
) (err error) {
queryType := "delete"
timer := prometheus.NewTimer(prometheus.ObserverFunc(func(v float64) {
s.queryDurationSummary.With(prometheus.Labels{
"query_type": queryType,
"error": fmt.Sprint(err != nil),
"route": contextRoute(ctx),
}).Observe(v)
}))
defer func() {
timer.ObserveDuration()
s.queryCounter.With(prometheus.Labels{
"query_type": queryType,
"error": fmt.Sprint(err != nil),
"route": contextRoute(ctx),
}).Inc()
}()
err = s.SessionInterface.DeleteRange(ctx, start, end, table, idCol)
return err
}