Skip to content

Commit 8bb1a41

Browse files
Skip pg_stat_checkpointer collector if pg<17 (#1112)
* fix: skip collector if pg<17 Signed-off-by: Michael Todorovic <[email protected]> * fix: better condition Signed-off-by: Michael Todorovic <[email protected]> * fix: fix PGStatCheckpointerCollector tests Signed-off-by: Nicolas Rodriguez <[email protected]> --------- Signed-off-by: Michael Todorovic <[email protected]> Signed-off-by: Nicolas Rodriguez <[email protected]> Co-authored-by: Michael Todorovic <[email protected]>
1 parent 4c170ed commit 8bb1a41

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

collector/pg_stat_checkpointer.go

+13-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ package collector
1616
import (
1717
"context"
1818
"database/sql"
19+
"log/slog"
1920

21+
"github.com/blang/semver/v4"
2022
"github.com/prometheus/client_golang/prometheus"
2123
)
2224

@@ -29,10 +31,11 @@ func init() {
2931
}
3032

3133
type PGStatCheckpointerCollector struct {
34+
log *slog.Logger
3235
}
3336

34-
func NewPGStatCheckpointerCollector(collectorConfig) (Collector, error) {
35-
return &PGStatCheckpointerCollector{}, nil
37+
func NewPGStatCheckpointerCollector(config collectorConfig) (Collector, error) {
38+
return &PGStatCheckpointerCollector{log: config.logger}, nil
3639
}
3740

3841
var (
@@ -104,8 +107,15 @@ var (
104107
FROM pg_stat_checkpointer;`
105108
)
106109

107-
func (PGStatCheckpointerCollector) Update(ctx context.Context, instance *instance, ch chan<- prometheus.Metric) error {
110+
func (c PGStatCheckpointerCollector) Update(ctx context.Context, instance *instance, ch chan<- prometheus.Metric) error {
108111
db := instance.getDB()
112+
113+
before17 := instance.version.LT(semver.MustParse("17.0.0"))
114+
if before17 {
115+
c.log.Warn("pg_stat_checkpointer collector is not available on PostgreSQL < 17.0.0, skipping")
116+
return nil
117+
}
118+
109119
row := db.QueryRowContext(ctx, statCheckpointerQuery)
110120

111121
// num_timed = nt = bigint

collector/pg_stat_checkpointer_test.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"time"
1919

2020
"github.com/DATA-DOG/go-sqlmock"
21+
"github.com/blang/semver/v4"
2122
"github.com/prometheus/client_golang/prometheus"
2223
dto "github.com/prometheus/client_model/go"
2324
"github.com/smartystreets/goconvey/convey"
@@ -30,7 +31,7 @@ func TestPGStatCheckpointerCollector(t *testing.T) {
3031
}
3132
defer db.Close()
3233

33-
inst := &instance{db: db}
34+
inst := &instance{db: db, version: semver.MustParse("17.0.0")}
3435

3536
columns := []string{
3637
"num_timed",
@@ -92,7 +93,7 @@ func TestPGStatCheckpointerCollectorNullValues(t *testing.T) {
9293
}
9394
defer db.Close()
9495

95-
inst := &instance{db: db}
96+
inst := &instance{db: db, version: semver.MustParse("17.0.0")}
9697

9798
columns := []string{
9899
"num_timed",

0 commit comments

Comments
 (0)