-
Notifications
You must be signed in to change notification settings - Fork 1
/
collector.go
127 lines (120 loc) · 4.66 KB
/
collector.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
package pgxpool_prometheus
import (
"github.com/jackc/pgx/v5/pgxpool"
"github.com/prometheus/client_golang/prometheus"
)
// PgxPoolStatsCollector is a Prometheus collector for pgx metrics.
// It implements the prometheus.Collector interface.
type PgxPoolStatsCollector struct {
db *pgxpool.Pool
acquireConns *prometheus.Desc
canceledAcquireCount *prometheus.Desc
constructingConns *prometheus.Desc
emptyAcquireCount *prometheus.Desc
idleConns *prometheus.Desc
maxConns *prometheus.Desc
totalConns *prometheus.Desc
newConnsCount *prometheus.Desc
maxLifetimeDestroyCount *prometheus.Desc
maxIdleDestroyCount *prometheus.Desc
}
// NewPgxPoolStatsCollector returns a new pgxCollector.
// The dbName parameter is used to set the "db" label on the metrics.
// The db parameter is the pgxpool.Pool to collect metrics from.
// The db parameter must not be nil.
// The dbName parameter must not be empty.
func NewPgxPoolStatsCollector(db *pgxpool.Pool, dbName string) *PgxPoolStatsCollector {
fqName := func(name string) string {
return prometheus.BuildFQName("pgx", "pool", name)
}
return &PgxPoolStatsCollector{
db: db,
acquireConns: prometheus.NewDesc(
fqName("acquire_connections"),
"Number of connections currently in the process of being acquired",
nil,
prometheus.Labels{"db": dbName},
),
canceledAcquireCount: prometheus.NewDesc(
fqName("canceled_acquire_count"),
"Number of times a connection acquire was canceled",
nil,
prometheus.Labels{"db": dbName},
),
constructingConns: prometheus.NewDesc(
fqName("constructing_connections"),
"Number of connections currently in the process of being constructed",
nil,
prometheus.Labels{"db": dbName},
),
emptyAcquireCount: prometheus.NewDesc(
fqName("empty_acquire_count"),
"Number of times a connection acquire was canceled",
nil,
prometheus.Labels{"db": dbName},
),
idleConns: prometheus.NewDesc(
fqName("idle_connections"),
"Number of idle connections in the pool",
nil,
prometheus.Labels{"db": dbName},
),
maxConns: prometheus.NewDesc(
fqName("max_connections"),
"Maximum number of connections allowed in the pool",
nil,
prometheus.Labels{"db": dbName},
),
totalConns: prometheus.NewDesc(
fqName("total_connections"),
"Total number of connections in the pool",
nil,
prometheus.Labels{"db": dbName},
),
newConnsCount: prometheus.NewDesc(
fqName("new_connections_count"),
"Number of new connections created",
nil,
prometheus.Labels{"db": dbName},
),
maxLifetimeDestroyCount: prometheus.NewDesc(
fqName("max_lifetime_destroy_count"),
"Number of connections destroyed due to MaxLifetime",
nil,
prometheus.Labels{"db": dbName},
),
maxIdleDestroyCount: prometheus.NewDesc(
fqName("max_idle_destroy_count"),
"Number of connections destroyed due to MaxIdleTime",
nil,
prometheus.Labels{"db": dbName},
),
}
}
// Describe implements the prometheus.Collector interface.
func (p PgxPoolStatsCollector) Describe(descs chan<- *prometheus.Desc) {
descs <- p.acquireConns
descs <- p.canceledAcquireCount
descs <- p.constructingConns
descs <- p.emptyAcquireCount
descs <- p.idleConns
descs <- p.maxConns
descs <- p.totalConns
descs <- p.newConnsCount
descs <- p.maxLifetimeDestroyCount
descs <- p.maxIdleDestroyCount
}
// Collect implements the prometheus.Collector interface.
func (p PgxPoolStatsCollector) Collect(metrics chan<- prometheus.Metric) {
stats := p.db.Stat()
metrics <- prometheus.MustNewConstMetric(p.acquireConns, prometheus.GaugeValue, float64(stats.AcquiredConns()))
metrics <- prometheus.MustNewConstMetric(p.canceledAcquireCount, prometheus.CounterValue, float64(stats.CanceledAcquireCount()))
metrics <- prometheus.MustNewConstMetric(p.constructingConns, prometheus.GaugeValue, float64(stats.ConstructingConns()))
metrics <- prometheus.MustNewConstMetric(p.emptyAcquireCount, prometheus.CounterValue, float64(stats.EmptyAcquireCount()))
metrics <- prometheus.MustNewConstMetric(p.idleConns, prometheus.GaugeValue, float64(stats.IdleConns()))
metrics <- prometheus.MustNewConstMetric(p.maxConns, prometheus.GaugeValue, float64(stats.MaxConns()))
metrics <- prometheus.MustNewConstMetric(p.totalConns, prometheus.GaugeValue, float64(stats.TotalConns()))
metrics <- prometheus.MustNewConstMetric(p.newConnsCount, prometheus.CounterValue, float64(stats.NewConnsCount()))
metrics <- prometheus.MustNewConstMetric(p.maxLifetimeDestroyCount, prometheus.CounterValue, float64(stats.MaxLifetimeDestroyCount()))
metrics <- prometheus.MustNewConstMetric(p.maxIdleDestroyCount, prometheus.CounterValue, float64(stats.MaxIdleDestroyCount()))
}