Skip to content

Commit 5401e23

Browse files
committed
Add autovacuum active
Signed-off-by: Felix Yuan <[email protected]>
1 parent 68f67d0 commit 5401e23

File tree

4 files changed

+154
-2
lines changed

4 files changed

+154
-2
lines changed

collector/pg_stat_activity_autovacuum.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020
"github.com/prometheus/client_golang/prometheus"
2121
)
2222

23-
const statActivityAutovacuumSubsystem = "slow"
23+
const statActivityAutovacuumSubsystem = "stat_activity_autovacuum"
2424

2525
func init() {
2626
registerCollector(statActivityAutovacuumSubsystem, defaultEnabled, NewPGStatActivityAutovacuumCollector)
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
// Copyright 2023 The Prometheus Authors
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
package collector
15+
16+
import (
17+
"context"
18+
19+
"github.com/go-kit/log"
20+
"github.com/prometheus/client_golang/prometheus"
21+
)
22+
23+
const statActivityAutovacuumActiveSubsystem = "stat_activity_autovacuum_active"
24+
25+
func init() {
26+
registerCollector(statActivityAutovacuumActiveSubsystem, defaultEnabled, NewPGStatActivityAutovacuumActiveCollector)
27+
}
28+
29+
type PGStatActivityAutovacuumActiveCollector struct {
30+
log log.Logger
31+
}
32+
33+
func NewPGStatActivityAutovacuumActiveCollector(config collectorConfig) (Collector, error) {
34+
return &PGStatActivityAutovacuumActiveCollector{log: config.logger}, nil
35+
}
36+
37+
var (
38+
statActivityAutovacuumActiveWorkersCount = prometheus.NewDesc(
39+
prometheus.BuildFQName(namespace, statActivityAutovacuumActiveSubsystem, "workers_count"),
40+
"Current number of statActivityAutovacuumActive queries",
41+
[]string{"phase", "mode"},
42+
prometheus.Labels{},
43+
)
44+
45+
statActivityAutovacuumActiveQuery = `
46+
SELECT
47+
v.phase,
48+
CASE
49+
when a.query ~ '^autovacuum.*to prevent wraparound' then 'wraparound'
50+
when a.query ~* '^vacuum' then 'user'
51+
when a.pid is null then 'idle'
52+
ELSE 'regular'
53+
END as mode,
54+
count(1) as workers_count
55+
FROM pg_stat_progress_vacuum v
56+
LEFT JOIN pg_catalog.pg_stat_activity a using (pid)
57+
GROUP BY 1,2
58+
`
59+
)
60+
61+
func (PGStatActivityAutovacuumActiveCollector) Update(ctx context.Context, instance *instance, ch chan<- prometheus.Metric) error {
62+
db := instance.getDB()
63+
rows, err := db.QueryContext(ctx,
64+
statActivityAutovacuumActiveQuery)
65+
66+
if err != nil {
67+
return err
68+
}
69+
defer rows.Close()
70+
71+
for rows.Next() {
72+
var phase, mode string
73+
var workers_count float64
74+
75+
if err := rows.Scan(&phase, &mode, &workers_count); err != nil {
76+
return err
77+
}
78+
79+
ch <- prometheus.MustNewConstMetric(
80+
statActivityAutovacuumActiveWorkersCount,
81+
prometheus.GaugeValue,
82+
workers_count,
83+
phase, mode,
84+
)
85+
}
86+
if err := rows.Err(); err != nil {
87+
return err
88+
}
89+
return nil
90+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Copyright 2023 The Prometheus Authors
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
package collector
14+
15+
import (
16+
"context"
17+
"testing"
18+
19+
"github.com/DATA-DOG/go-sqlmock"
20+
"github.com/prometheus/client_golang/prometheus"
21+
dto "github.com/prometheus/client_model/go"
22+
"github.com/smartystreets/goconvey/convey"
23+
)
24+
25+
func TestPGStatActivityAutovacuumActiveCollector(t *testing.T) {
26+
db, mock, err := sqlmock.New()
27+
if err != nil {
28+
t.Fatalf("Error opening a stub db connection: %s", err)
29+
}
30+
defer db.Close()
31+
inst := &instance{db: db}
32+
columns := []string{
33+
"phase",
34+
"mode",
35+
"workers_count",
36+
}
37+
rows := sqlmock.NewRows(columns).
38+
AddRow("Scanning heap", "regular", 2)
39+
mock.ExpectQuery(sanitizeQuery(statActivityAutovacuumActiveQuery)).WillReturnRows(rows)
40+
41+
ch := make(chan prometheus.Metric)
42+
go func() {
43+
defer close(ch)
44+
c := PGStatActivityAutovacuumActiveCollector{}
45+
46+
if err := c.Update(context.Background(), inst, ch); err != nil {
47+
t.Errorf("Error calling PGStatActivityAutovacuumActiveCollector.Update: %s", err)
48+
}
49+
}()
50+
expected := []MetricResult{
51+
{labels: labelMap{"phase": "Scanning heap", "mode": "regular"}, value: 2, metricType: dto.MetricType_GAUGE},
52+
}
53+
convey.Convey("Metrics comparison", t, func() {
54+
for _, expect := range expected {
55+
m := readMetric(<-ch)
56+
convey.So(expect, convey.ShouldResemble, m)
57+
}
58+
})
59+
if err := mock.ExpectationsWereMet(); err != nil {
60+
t.Errorf("there were unfulfilled exceptions: %s", err)
61+
}
62+
}

collector/pg_stat_activity_marginalia.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020
"github.com/prometheus/client_golang/prometheus"
2121
)
2222

23-
const statActivityMarginaliaSubsystem = "slow"
23+
const statActivityMarginaliaSubsystem = "stat_activity_marginalia"
2424

2525
func init() {
2626
registerCollector(statActivityMarginaliaSubsystem, defaultEnabled, NewPGStatActivityMarginaliaCollector)

0 commit comments

Comments
 (0)