Skip to content
This repository was archived by the owner on Nov 28, 2022. It is now read-only.

Commit a115556

Browse files
committed
Add vacuum process running metric
1 parent 1ef6336 commit a115556

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

gauges/vacuum.go

+24
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package gauges
33
import (
44
"time"
55

6+
"github.com/ContaAzul/postgresql_exporter/postgres"
7+
"github.com/apex/log"
68
"github.com/prometheus/client_golang/prometheus"
79
)
810

@@ -94,3 +96,25 @@ func (g *Gauges) LastTimeAutoVacuumRan() *prometheus.GaugeVec {
9496

9597
return gauge
9698
}
99+
100+
// VacuumRunningTotal returns the number of backends (including autovacuum worker processes)
101+
// that is currently running a vacuuming (not include VACUUM FULL).
102+
//
103+
// This metric is only supported for PostgreSQL 9.6 or newer versions
104+
func (g *Gauges) VacuumRunningTotal() prometheus.Gauge {
105+
var gaugeOpts = prometheus.GaugeOpts{
106+
Name: "postgresql_vacuum_running_total",
107+
Help: "Number of backends (including autovacuum worker processes) that is currently vacuuming",
108+
ConstLabels: g.labels,
109+
}
110+
111+
const vacuumRunningQuery = `
112+
SELECT COUNT(*) FROM pg_stat_progress_vacuum WHERE datname = current_database()
113+
`
114+
115+
if !postgres.Version(g.version()).Is96Or10() {
116+
log.Warn("postgresql_vacuum_running_total disabled because it's only supported for PostgreSQL 9.6 or newer versions")
117+
return prometheus.NewGauge(gaugeOpts)
118+
}
119+
return g.new(gaugeOpts, vacuumRunningQuery)
120+
}

gauges/vacuum_test.go

+11
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,14 @@ func TestLastTimeAutoVacuumRan(t *testing.T) {
4141
assertEqual(t, 0, metrics[0])
4242
assertNoErrs(t, gauges)
4343
}
44+
45+
func TestVacuumRunningTotal(t *testing.T) {
46+
var assert = assert.New(t)
47+
_, gauges, close := prepare(t)
48+
defer close()
49+
50+
var metrics = evaluate(t, gauges.VacuumRunningTotal())
51+
assert.Len(metrics, 1)
52+
assertEqual(t, 0, metrics[0])
53+
assertNoErrs(t, gauges)
54+
}

main.go

+1
Original file line numberDiff line numberDiff line change
@@ -117,4 +117,5 @@ func watch(db *sql.DB, reg prometheus.Registerer, name string) {
117117
reg.MustRegister(gauges.UnvacuumedTransactions())
118118
reg.MustRegister(gauges.LastTimeVacuumRan())
119119
reg.MustRegister(gauges.LastTimeAutoVacuumRan())
120+
reg.MustRegister(gauges.VacuumRunningTotal())
120121
}

0 commit comments

Comments
 (0)