-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmetrics.go
133 lines (109 loc) · 4.03 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
package metrics
import (
"context"
"fmt"
"github.com/alexfalkowski/go-service/os"
"github.com/alexfalkowski/go-service/version"
"github.com/jmoiron/sqlx"
"github.com/linxGnu/mssqlx"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
)
// Register for metrics.
//
//nolint:funlen
func Register(dbs *mssqlx.DBs, version version.Version, meter metric.Meter) error {
opts := metric.WithAttributes(
attribute.Key("name").String(os.ExecutableName()),
attribute.Key("version").String(string(version)),
attribute.Key("db_driver").String(dbs.DriverName()),
)
maxOpen, err := meter.Int64ObservableGauge("sql_max_open_total", metric.WithDescription("Maximum number of open connections to the database."))
if err != nil {
return err
}
open, err := meter.Int64ObservableGauge("sql_open_total", metric.WithDescription("The number of established connections both in use and idle."))
if err != nil {
return err
}
inUse, err := meter.Int64ObservableGauge("sql_in_use_total", metric.WithDescription("The number of connections currently in use."))
if err != nil {
return err
}
idle, err := meter.Int64ObservableGauge("sql_idle_total", metric.WithDescription("The number of idle connections."))
if err != nil {
return err
}
waited, err := meter.Int64ObservableCounter("sql_waited_for_total", metric.WithDescription("The total number of connections waited for."))
if err != nil {
return err
}
blocked, err := meter.Float64ObservableCounter("sql_blocked_seconds_total", metric.WithDescription("The total time blocked waiting for a new connection."))
if err != nil {
return err
}
maxIdleClosed, err := meter.Int64ObservableCounter("sql_closed_max_idle_total", metric.WithDescription("The total number of connections closed due to SetMaxIdleConns."))
if err != nil {
return err
}
maxIdleTimeClosed, err := meter.Int64ObservableCounter("sql_closed_max_lifetime_total",
metric.WithDescription("The total number of connections closed due to SetConnMaxIdleTime."))
if err != nil {
return err
}
maxLifetimeClosed, err := meter.Int64ObservableCounter("sql_closed_max_idle_time_total",
metric.WithDescription("The total number of connections closed due to SetConnMaxLifetime."))
if err != nil {
return err
}
m := &metrics{
dbs: dbs, opts: opts,
mo: maxOpen, o: open, iu: inUse,
i: idle, w: waited, b: blocked,
mic: maxIdleClosed, mitc: maxIdleTimeClosed, mlc: maxLifetimeClosed,
}
meter.RegisterCallback(m.callback, maxOpen, open, inUse, idle, waited, blocked, maxIdleClosed, maxIdleTimeClosed, maxLifetimeClosed)
return nil
}
type metrics struct {
dbs *mssqlx.DBs
opts metric.MeasurementOption
mo metric.Int64ObservableGauge
o metric.Int64ObservableGauge
iu metric.Int64ObservableGauge
i metric.Int64ObservableGauge
w metric.Int64ObservableCounter
b metric.Float64ObservableCounter
mic metric.Int64ObservableCounter
mitc metric.Int64ObservableCounter
mlc metric.Int64ObservableCounter
}
func (m *metrics) callback(_ context.Context, o metric.Observer) error {
ms, _ := m.dbs.GetAllMasters()
for i, ma := range ms {
opts := metric.WithAttributes(
attribute.Key("db_name").String(fmt.Sprintf("master_%d", i)),
)
m.collect(ma, o, opts)
}
ss, _ := m.dbs.GetAllSlaves()
for i, s := range ss {
opts := metric.WithAttributes(
attribute.Key("db_name").String(fmt.Sprintf("slave_%d", i)),
)
m.collect(s, o, opts)
}
return nil
}
func (m *metrics) collect(db *sqlx.DB, o metric.Observer, opts metric.MeasurementOption) {
stats := db.Stats()
o.ObserveInt64(m.mo, int64(stats.MaxOpenConnections), m.opts, opts)
o.ObserveInt64(m.o, int64(stats.OpenConnections), m.opts, opts)
o.ObserveInt64(m.iu, int64(stats.InUse), m.opts, opts)
o.ObserveInt64(m.i, int64(stats.Idle), m.opts, opts)
o.ObserveInt64(m.w, stats.WaitCount, m.opts, opts)
o.ObserveFloat64(m.b, stats.WaitDuration.Seconds(), m.opts, opts)
o.ObserveInt64(m.mic, stats.MaxIdleClosed, m.opts, opts)
o.ObserveInt64(m.mitc, stats.MaxIdleTimeClosed, m.opts, opts)
o.ObserveInt64(m.mlc, stats.MaxLifetimeClosed, m.opts, opts)
}