Skip to content

Commit e1e5390

Browse files
committed
Expose EmptyAcquireWaitTime as prometheus counter
Signed-off-by: Bryan Frimin <bryan@getprobo.com>
1 parent 6745498 commit e1e5390

2 files changed

Lines changed: 67 additions & 12 deletions

File tree

pg/client_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,46 @@ func TestNewClient(t *testing.T) {
222222
)
223223
},
224224
)
225+
226+
t.Run(
227+
"exposes expected pgxpool metrics",
228+
func(t *testing.T) {
229+
reg := prometheus.NewRegistry()
230+
_, err := pg.NewClient(
231+
pg.WithAddr("localhost:5432"),
232+
pg.WithRegisterer(reg),
233+
)
234+
require.NoError(t, err)
235+
236+
families, err := reg.Gather()
237+
require.NoError(t, err)
238+
239+
got := make(map[string]struct{}, len(families))
240+
for _, fam := range families {
241+
got[fam.GetName()] = struct{}{}
242+
}
243+
244+
expected := []string{
245+
"pgxpool_acquire_total",
246+
"pgxpool_acquire_duration_seconds",
247+
"pgxpool_acquired_connections",
248+
"pgxpool_canceled_acquire_total",
249+
"pgxpool_constructing_connections",
250+
"pgxpool_empty_acquire_total",
251+
"pgxpool_empty_acquire_wait_time_seconds",
252+
"pgxpool_idle_connections",
253+
"pgxpool_max_connections",
254+
"pgxpool_total_connections",
255+
"pgxpool_new_connections_total",
256+
"pgxpool_max_lifetime_destroy_total",
257+
"pgxpool_max_idle_destroy_total",
258+
}
259+
for _, name := range expected {
260+
_, ok := got[name]
261+
assert.Truef(t, ok, "metric %q not exported", name)
262+
}
263+
},
264+
)
225265
}
226266

227267
// ---------------------------------------------------------------------------

pg/collector.go

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,19 @@ type (
2525
collector struct {
2626
pool *pgxpool.Pool
2727

28-
acquireTotal *prometheus.Desc
29-
acquireDurationSeconds *prometheus.Desc
30-
acquiredConnections *prometheus.Desc
31-
canceledAcquireTotal *prometheus.Desc
32-
constructingConnections *prometheus.Desc
33-
emptyAcquireTotal *prometheus.Desc
34-
idleConnections *prometheus.Desc
35-
maxConnections *prometheus.Desc
36-
totalConnections *prometheus.Desc
37-
newConnectionsTotal *prometheus.Desc
38-
maxLifetimeDestroyTotal *prometheus.Desc
39-
maxIdleDestroyTotal *prometheus.Desc
28+
acquireTotal *prometheus.Desc
29+
acquireDurationSeconds *prometheus.Desc
30+
acquiredConnections *prometheus.Desc
31+
canceledAcquireTotal *prometheus.Desc
32+
constructingConnections *prometheus.Desc
33+
emptyAcquireTotal *prometheus.Desc
34+
emptyAcquireWaitTimeSeconds *prometheus.Desc
35+
idleConnections *prometheus.Desc
36+
maxConnections *prometheus.Desc
37+
totalConnections *prometheus.Desc
38+
newConnectionsTotal *prometheus.Desc
39+
maxLifetimeDestroyTotal *prometheus.Desc
40+
maxIdleDestroyTotal *prometheus.Desc
4041
}
4142

4243
statWrapper struct {
@@ -84,6 +85,12 @@ func newCollector(pool *pgxpool.Pool, labels map[string]string) *collector {
8485
nil,
8586
labels,
8687
),
88+
emptyAcquireWaitTimeSeconds: prometheus.NewDesc(
89+
"pgxpool_empty_acquire_wait_time_seconds",
90+
"Cumulative time in seconds waited for successful acquires from the pool that had to wait for a resource to be released or constructed because the pool was empty. Dividing by pgxpool_empty_acquire_total yields the mean slow-path acquire latency, which isolates connection construction and contention from the fast-path (idle conn handover).",
91+
nil,
92+
labels,
93+
),
8794
idleConnections: prometheus.NewDesc(
8895
"pgxpool_idle_connections",
8996
"Number of currently idle connections in the pool.",
@@ -160,6 +167,11 @@ func (c *collector) Collect(metrics chan<- prometheus.Metric) {
160167
prometheus.CounterValue,
161168
stats.emptyAcquireCount(),
162169
)
170+
metrics <- prometheus.MustNewConstMetric(
171+
c.emptyAcquireWaitTimeSeconds,
172+
prometheus.CounterValue,
173+
stats.emptyAcquireWaitTime(),
174+
)
163175
metrics <- prometheus.MustNewConstMetric(
164176
c.idleConnections,
165177
prometheus.GaugeValue,
@@ -210,6 +222,9 @@ func (w *statWrapper) constructingConns() float64 {
210222
func (w *statWrapper) emptyAcquireCount() float64 {
211223
return float64(w.stats.EmptyAcquireCount())
212224
}
225+
func (w *statWrapper) emptyAcquireWaitTime() float64 {
226+
return w.stats.EmptyAcquireWaitTime().Seconds()
227+
}
213228
func (w *statWrapper) idleConns() float64 {
214229
return float64(w.stats.IdleConns())
215230
}

0 commit comments

Comments
 (0)