...
1 package dbmetrics
2
3 import (
4 "context"
5 "fmt"
6
7 "github.com/prometheus/client_golang/prometheus"
8 ctrl "sigs.k8s.io/controller-runtime"
9
10 "edge-infra.dev/pkg/k8s/runtime/controller/metrics"
11 )
12
13 type DBMetrics struct {
14 statusWritesTotal *prometheus.CounterVec
15 errorsTotal *prometheus.CounterVec
16 }
17
18 const (
19 DBStatusWritesTotal = "db_status_writes_total"
20 DBErrorsTotal = "db_errors_total"
21 )
22
23 func New(prefix string) *DBMetrics {
24 writesTotal := prometheus.NewCounterVec(prometheus.CounterOpts{
25 Name: metrics.Name(prefix, DBStatusWritesTotal),
26 Help: "number of times the controller writes the status to the database",
27 }, []string{"status", "kind"})
28
29 errorsTotal := prometheus.NewCounterVec(prometheus.CounterOpts{
30 Name: metrics.Name(prefix, DBErrorsTotal),
31 Help: "number of database errors",
32 }, []string{"kind", "name"})
33
34 return &DBMetrics{
35 statusWritesTotal: writesTotal,
36 errorsTotal: errorsTotal,
37 }
38 }
39
40 func (dbm *DBMetrics) Collectors() []prometheus.Collector {
41 return []prometheus.Collector{
42 dbm.statusWritesTotal,
43 dbm.errorsTotal,
44 }
45 }
46
47 func (dbm *DBMetrics) RecordDBStatusWritesTotal(ctx context.Context, kind, status string) {
48 if dbm.statusWritesTotal == nil {
49 ctrl.LoggerFrom(ctx).Error(
50 fmt.Errorf("databaseWritesTotal not set"), "unable to record database writes total",
51 )
52 return
53 }
54
55 var l = prometheus.Labels{
56 "kind": kind,
57 "status": status,
58 }
59 dbm.statusWritesTotal.With(l).Inc()
60 }
61
62 func (dbm *DBMetrics) RecordDBErrorsTotal(ctx context.Context, kind, name string) {
63 if dbm.errorsTotal == nil {
64 ctrl.LoggerFrom(ctx).Error(
65 fmt.Errorf("databaseErrorsTotal not set"), "unable to record database errors total",
66 )
67 return
68 }
69
70 var l = prometheus.Labels{
71 "kind": kind,
72 "name": name,
73 }
74 dbm.errorsTotal.With(l).Inc()
75 }
76
View as plain text