forked from adamlaska/boulder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmetrics.go
More file actions
129 lines (108 loc) · 4.29 KB
/
metrics.go
File metadata and controls
129 lines (108 loc) · 4.29 KB
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
package sa
import (
"database/sql"
"github.com/prometheus/client_golang/prometheus"
)
type dbMetricsCollector struct {
db *sql.DB
dbSettings DbSettings
maxOpenConns *prometheus.Desc
maxIdleConns *prometheus.Desc
connMaxLifetime *prometheus.Desc
connMaxIdleTime *prometheus.Desc
openConns *prometheus.Desc
inUse *prometheus.Desc
idle *prometheus.Desc
waitCount *prometheus.Desc
waitDuration *prometheus.Desc
maxIdleClosed *prometheus.Desc
maxLifetimeClosed *prometheus.Desc
}
// Describe is implemented with DescribeByCollect. That's possible because the
// Collect method will always return the same metrics with the same descriptors.
func (dbc dbMetricsCollector) Describe(ch chan<- *prometheus.Desc) {
prometheus.DescribeByCollect(dbc, ch)
}
// Collect first triggers the dbMaps's sql.Db's Stats function. Then it
// creates constant metrics for each DBStats value on the fly based on the
// returned data.
//
// Note that Collect could be called concurrently, so we depend on
// Stats() to be concurrency-safe.
func (dbc dbMetricsCollector) Collect(ch chan<- prometheus.Metric) {
writeStat := func(stat *prometheus.Desc, typ prometheus.ValueType, val float64) {
ch <- prometheus.MustNewConstMetric(stat, typ, val)
}
writeCounter := func(stat *prometheus.Desc, val float64) {
writeStat(stat, prometheus.CounterValue, val)
}
writeGauge := func(stat *prometheus.Desc, val float64) {
writeStat(stat, prometheus.GaugeValue, val)
}
// Translate the DBMap's db.DBStats counter values into Prometheus metrics.
dbMapStats := dbc.db.Stats()
writeGauge(dbc.maxOpenConns, float64(dbMapStats.MaxOpenConnections))
writeGauge(dbc.maxIdleConns, float64(dbc.dbSettings.MaxIdleConns))
writeGauge(dbc.connMaxLifetime, float64(dbc.dbSettings.ConnMaxLifetime))
writeGauge(dbc.connMaxIdleTime, float64(dbc.dbSettings.ConnMaxIdleTime))
writeGauge(dbc.openConns, float64(dbMapStats.OpenConnections))
writeGauge(dbc.inUse, float64(dbMapStats.InUse))
writeGauge(dbc.idle, float64(dbMapStats.Idle))
writeCounter(dbc.waitCount, float64(dbMapStats.WaitCount))
writeCounter(dbc.waitDuration, dbMapStats.WaitDuration.Seconds())
writeCounter(dbc.maxIdleClosed, float64(dbMapStats.MaxIdleClosed))
writeCounter(dbc.maxLifetimeClosed, float64(dbMapStats.MaxLifetimeClosed))
}
// InitDBMetrics will register a Collector that translates the provided dbMap's
// stats and DbSettings into Prometheus metrics on the fly. The stat values will
// be translated from the gorp dbMap's inner sql.DBMap's DBStats structure values
func InitDBMetrics(db *sql.DB, stats prometheus.Registerer, dbSettings DbSettings, address string, user string) {
// Create a dbMetricsCollector and register it
dbc := dbMetricsCollector{db: db, dbSettings: dbSettings}
labels := prometheus.Labels{"address": address, "user": user}
dbc.maxOpenConns = prometheus.NewDesc(
"db_max_open_connections",
"Maximum number of DB connections allowed.",
nil, labels)
dbc.maxIdleConns = prometheus.NewDesc(
"db_max_idle_connections",
"Maximum number of idle DB connections allowed.",
nil, labels)
dbc.connMaxLifetime = prometheus.NewDesc(
"db_connection_max_lifetime",
"Maximum lifetime of DB connections allowed.",
nil, labels)
dbc.connMaxIdleTime = prometheus.NewDesc(
"db_connection_max_idle_time",
"Maximum lifetime of idle DB connections allowed.",
nil, labels)
dbc.openConns = prometheus.NewDesc(
"db_open_connections",
"Number of established DB connections (in-use and idle).",
nil, labels)
dbc.inUse = prometheus.NewDesc(
"db_inuse",
"Number of DB connections currently in use.",
nil, labels)
dbc.idle = prometheus.NewDesc(
"db_idle",
"Number of idle DB connections.",
nil, labels)
dbc.waitCount = prometheus.NewDesc(
"db_wait_count",
"Total number of DB connections waited for.",
nil, labels)
dbc.waitDuration = prometheus.NewDesc(
"db_wait_duration_seconds",
"The total time blocked waiting for a new connection.",
nil, labels)
dbc.maxIdleClosed = prometheus.NewDesc(
"db_max_idle_closed",
"Total number of connections closed due to SetMaxIdleConns.",
nil, labels)
dbc.maxLifetimeClosed = prometheus.NewDesc(
"db_max_lifetime_closed",
"Total number of connections closed due to SetConnMaxLifetime.",
nil, labels)
stats.MustRegister(dbc)
}