Database

/app/lib/telemetry/dbmetrics/collector.go (3.2 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
package dbmetrics

import (
"database/sql"

"github.com/prometheus/client_golang/prometheus"
)

type StatsGetter interface {
Stats() sql.DBStats
}

type StatsCollector struct {
sg StatsGetter
maxOpenDesc *prometheus.Desc
openDesc *prometheus.Desc
inUseDesc *prometheus.Desc
idleDesc *prometheus.Desc
waitedForDesc *prometheus.Desc
blockedSecondsDesc *prometheus.Desc
closedMaxIdleDesc *prometheus.Desc
closedMaxLifetimeDesc *prometheus.Desc
closedMaxIdleTimeDesc *prometheus.Desc
}

func newStatsCollector(dbName string, sg StatsGetter) *StatsCollector {
labels := prometheus.Labels{"db_name": dbName}
x := func(key string, help string) *prometheus.Desc {
return prometheus.NewDesc(prometheus.BuildFQName("database", "database", key), help, nil, labels)
}
return &StatsCollector{
sg: sg,
maxOpenDesc: x("max_open", "Maximum number of open connections to the database."),
openDesc: x("open", "The number of established connections both in use and idle."),
inUseDesc: x("in_use", "The number of connections currently in use."),
idleDesc: x("idle", "The number of idle connections."),
waitedForDesc: x("waited_for", "The total number of connections waited for."),
blockedSecondsDesc: x("blocked_seconds", "The total time blocked waiting for a new connection."),
closedMaxIdleDesc: x("closed_max_idle", "The total number of connections closed due to SetMaxIdleConns."),
closedMaxLifetimeDesc: x("closed_max_lifetime", "The total number of connections closed due to SetConnMaxLifetime."),
closedMaxIdleTimeDesc: x("closed_max_idle_time", "The total number of connections closed due to SetConnMaxIdleTime."),
}
}

// Describe implements the prometheus.Collector interface.
func (c *StatsCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- c.maxOpenDesc
ch <- c.openDesc
ch <- c.inUseDesc
ch <- c.idleDesc
ch <- c.waitedForDesc
ch <- c.blockedSecondsDesc
ch <- c.closedMaxIdleDesc
ch <- c.closedMaxLifetimeDesc
ch <- c.closedMaxIdleTimeDesc
}

func (c *StatsCollector) Collect(ch chan<- prometheus.Metric) {
stats := c.sg.Stats()
ch <- prometheus.MustNewConstMetric(c.maxOpenDesc, prometheus.GaugeValue, float64(stats.MaxOpenConnections))
ch <- prometheus.MustNewConstMetric(c.openDesc, prometheus.GaugeValue, float64(stats.OpenConnections))
ch <- prometheus.MustNewConstMetric(c.inUseDesc, prometheus.GaugeValue, float64(stats.InUse))
ch <- prometheus.MustNewConstMetric(c.idleDesc, prometheus.GaugeValue, float64(stats.Idle))
ch <- prometheus.MustNewConstMetric(c.waitedForDesc, prometheus.CounterValue, float64(stats.WaitCount))
ch <- prometheus.MustNewConstMetric(c.blockedSecondsDesc, prometheus.CounterValue, stats.WaitDuration.Seconds())
ch <- prometheus.MustNewConstMetric(c.closedMaxIdleDesc, prometheus.CounterValue, float64(stats.MaxIdleClosed))
ch <- prometheus.MustNewConstMetric(c.closedMaxLifetimeDesc, prometheus.CounterValue, float64(stats.MaxLifetimeClosed))
ch <- prometheus.MustNewConstMetric(c.closedMaxIdleTimeDesc, prometheus.CounterValue, float64(stats.MaxIdleTimeClosed))
}