Skip to content

Commit f5b613a

Browse files
authored
pg_stat_database: added support for active_time counter (#961)
* feat(pg_stat_database): active time metric --------- Signed-off-by: Jiri Sveceny <[email protected]>
1 parent 5ceae7f commit f5b613a

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

Diff for: collector/pg_stat_database.go

+23-1
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,15 @@ var (
206206
[]string{"datid", "datname"},
207207
prometheus.Labels{},
208208
)
209+
statDatabaseActiveTime = prometheus.NewDesc(prometheus.BuildFQName(
210+
namespace,
211+
statDatabaseSubsystem,
212+
"active_time_seconds_total",
213+
),
214+
"Time spent executing SQL statements in this database, in seconds",
215+
[]string{"datid", "datname"},
216+
prometheus.Labels{},
217+
)
209218

210219
statDatabaseQuery = `
211220
SELECT
@@ -227,6 +236,7 @@ var (
227236
,deadlocks
228237
,blk_read_time
229238
,blk_write_time
239+
,active_time
230240
,stats_reset
231241
FROM pg_stat_database;
232242
`
@@ -244,7 +254,7 @@ func (c *PGStatDatabaseCollector) Update(ctx context.Context, instance *instance
244254

245255
for rows.Next() {
246256
var datid, datname sql.NullString
247-
var numBackends, xactCommit, xactRollback, blksRead, blksHit, tupReturned, tupFetched, tupInserted, tupUpdated, tupDeleted, conflicts, tempFiles, tempBytes, deadlocks, blkReadTime, blkWriteTime sql.NullFloat64
257+
var numBackends, xactCommit, xactRollback, blksRead, blksHit, tupReturned, tupFetched, tupInserted, tupUpdated, tupDeleted, conflicts, tempFiles, tempBytes, deadlocks, blkReadTime, blkWriteTime, activeTime sql.NullFloat64
248258
var statsReset sql.NullTime
249259

250260
err := rows.Scan(
@@ -266,6 +276,7 @@ func (c *PGStatDatabaseCollector) Update(ctx context.Context, instance *instance
266276
&deadlocks,
267277
&blkReadTime,
268278
&blkWriteTime,
279+
&activeTime,
269280
&statsReset,
270281
)
271282
if err != nil {
@@ -344,6 +355,10 @@ func (c *PGStatDatabaseCollector) Update(ctx context.Context, instance *instance
344355
level.Debug(c.log).Log("msg", "Skipping collecting metric because it has no blk_write_time")
345356
continue
346357
}
358+
if !activeTime.Valid {
359+
level.Debug(c.log).Log("msg", "Skipping collecting metric because it has no active_time")
360+
continue
361+
}
347362

348363
statsResetMetric := 0.0
349364
if !statsReset.Valid {
@@ -467,6 +482,13 @@ func (c *PGStatDatabaseCollector) Update(ctx context.Context, instance *instance
467482
labels...,
468483
)
469484

485+
ch <- prometheus.MustNewConstMetric(
486+
statDatabaseActiveTime,
487+
prometheus.CounterValue,
488+
activeTime.Float64/1000.0,
489+
labels...,
490+
)
491+
470492
ch <- prometheus.MustNewConstMetric(
471493
statDatabaseStatsReset,
472494
prometheus.CounterValue,

Diff for: collector/pg_stat_database_test.go

+17
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ func TestPGStatDatabaseCollector(t *testing.T) {
5252
"deadlocks",
5353
"blk_read_time",
5454
"blk_write_time",
55+
"active_time",
5556
"stats_reset",
5657
}
5758

@@ -80,6 +81,7 @@ func TestPGStatDatabaseCollector(t *testing.T) {
8081
925,
8182
16,
8283
823,
84+
33,
8385
srT)
8486

8587
mock.ExpectQuery(sanitizeQuery(statDatabaseQuery)).WillReturnRows(rows)
@@ -113,6 +115,7 @@ func TestPGStatDatabaseCollector(t *testing.T) {
113115
{labels: labelMap{"datid": "pid", "datname": "postgres"}, metricType: dto.MetricType_COUNTER, value: 925},
114116
{labels: labelMap{"datid": "pid", "datname": "postgres"}, metricType: dto.MetricType_COUNTER, value: 16},
115117
{labels: labelMap{"datid": "pid", "datname": "postgres"}, metricType: dto.MetricType_COUNTER, value: 823},
118+
{labels: labelMap{"datid": "pid", "datname": "postgres"}, metricType: dto.MetricType_COUNTER, value: 0.033},
116119
{labels: labelMap{"datid": "pid", "datname": "postgres"}, metricType: dto.MetricType_COUNTER, value: 1685059842},
117120
}
118121

@@ -159,6 +162,7 @@ func TestPGStatDatabaseCollectorNullValues(t *testing.T) {
159162
"deadlocks",
160163
"blk_read_time",
161164
"blk_write_time",
165+
"active_time",
162166
"stats_reset",
163167
}
164168

@@ -182,6 +186,7 @@ func TestPGStatDatabaseCollectorNullValues(t *testing.T) {
182186
925,
183187
16,
184188
823,
189+
32,
185190
srT).
186191
AddRow(
187192
"pid",
@@ -202,6 +207,7 @@ func TestPGStatDatabaseCollectorNullValues(t *testing.T) {
202207
925,
203208
16,
204209
823,
210+
32,
205211
srT)
206212
mock.ExpectQuery(sanitizeQuery(statDatabaseQuery)).WillReturnRows(rows)
207213

@@ -234,6 +240,7 @@ func TestPGStatDatabaseCollectorNullValues(t *testing.T) {
234240
{labels: labelMap{"datid": "pid", "datname": "postgres"}, metricType: dto.MetricType_COUNTER, value: 925},
235241
{labels: labelMap{"datid": "pid", "datname": "postgres"}, metricType: dto.MetricType_COUNTER, value: 16},
236242
{labels: labelMap{"datid": "pid", "datname": "postgres"}, metricType: dto.MetricType_COUNTER, value: 823},
243+
{labels: labelMap{"datid": "pid", "datname": "postgres"}, metricType: dto.MetricType_COUNTER, value: 0.032},
237244
{labels: labelMap{"datid": "pid", "datname": "postgres"}, metricType: dto.MetricType_COUNTER, value: 1685059842},
238245
}
239246

@@ -275,6 +282,7 @@ func TestPGStatDatabaseCollectorRowLeakTest(t *testing.T) {
275282
"deadlocks",
276283
"blk_read_time",
277284
"blk_write_time",
285+
"active_time",
278286
"stats_reset",
279287
}
280288

@@ -303,6 +311,7 @@ func TestPGStatDatabaseCollectorRowLeakTest(t *testing.T) {
303311
925,
304312
16,
305313
823,
314+
14,
306315
srT).
307316
AddRow(
308317
nil,
@@ -324,6 +333,7 @@ func TestPGStatDatabaseCollectorRowLeakTest(t *testing.T) {
324333
nil,
325334
nil,
326335
nil,
336+
nil,
327337
).
328338
AddRow(
329339
"pid",
@@ -344,6 +354,7 @@ func TestPGStatDatabaseCollectorRowLeakTest(t *testing.T) {
344354
926,
345355
17,
346356
824,
357+
15,
347358
srT)
348359
mock.ExpectQuery(sanitizeQuery(statDatabaseQuery)).WillReturnRows(rows)
349360

@@ -376,7 +387,9 @@ func TestPGStatDatabaseCollectorRowLeakTest(t *testing.T) {
376387
{labels: labelMap{"datid": "pid", "datname": "postgres"}, metricType: dto.MetricType_COUNTER, value: 925},
377388
{labels: labelMap{"datid": "pid", "datname": "postgres"}, metricType: dto.MetricType_COUNTER, value: 16},
378389
{labels: labelMap{"datid": "pid", "datname": "postgres"}, metricType: dto.MetricType_COUNTER, value: 823},
390+
{labels: labelMap{"datid": "pid", "datname": "postgres"}, metricType: dto.MetricType_COUNTER, value: 0.014},
379391
{labels: labelMap{"datid": "pid", "datname": "postgres"}, metricType: dto.MetricType_COUNTER, value: 1685059842},
392+
380393
{labels: labelMap{"datid": "pid", "datname": "postgres"}, metricType: dto.MetricType_GAUGE, value: 355},
381394
{labels: labelMap{"datid": "pid", "datname": "postgres"}, metricType: dto.MetricType_COUNTER, value: 4946},
382395
{labels: labelMap{"datid": "pid", "datname": "postgres"}, metricType: dto.MetricType_COUNTER, value: 289097745},
@@ -393,6 +406,7 @@ func TestPGStatDatabaseCollectorRowLeakTest(t *testing.T) {
393406
{labels: labelMap{"datid": "pid", "datname": "postgres"}, metricType: dto.MetricType_COUNTER, value: 926},
394407
{labels: labelMap{"datid": "pid", "datname": "postgres"}, metricType: dto.MetricType_COUNTER, value: 17},
395408
{labels: labelMap{"datid": "pid", "datname": "postgres"}, metricType: dto.MetricType_COUNTER, value: 824},
409+
{labels: labelMap{"datid": "pid", "datname": "postgres"}, metricType: dto.MetricType_COUNTER, value: 0.015},
396410
{labels: labelMap{"datid": "pid", "datname": "postgres"}, metricType: dto.MetricType_COUNTER, value: 1685059842},
397411
}
398412

@@ -435,6 +449,7 @@ func TestPGStatDatabaseCollectorTestNilStatReset(t *testing.T) {
435449
"deadlocks",
436450
"blk_read_time",
437451
"blk_write_time",
452+
"active_time",
438453
"stats_reset",
439454
}
440455

@@ -458,6 +473,7 @@ func TestPGStatDatabaseCollectorTestNilStatReset(t *testing.T) {
458473
925,
459474
16,
460475
823,
476+
7,
461477
nil)
462478

463479
mock.ExpectQuery(sanitizeQuery(statDatabaseQuery)).WillReturnRows(rows)
@@ -491,6 +507,7 @@ func TestPGStatDatabaseCollectorTestNilStatReset(t *testing.T) {
491507
{labels: labelMap{"datid": "pid", "datname": "postgres"}, metricType: dto.MetricType_COUNTER, value: 925},
492508
{labels: labelMap{"datid": "pid", "datname": "postgres"}, metricType: dto.MetricType_COUNTER, value: 16},
493509
{labels: labelMap{"datid": "pid", "datname": "postgres"}, metricType: dto.MetricType_COUNTER, value: 823},
510+
{labels: labelMap{"datid": "pid", "datname": "postgres"}, metricType: dto.MetricType_COUNTER, value: 0.007},
494511
{labels: labelMap{"datid": "pid", "datname": "postgres"}, metricType: dto.MetricType_COUNTER, value: 0},
495512
}
496513

0 commit comments

Comments
 (0)