Skip to content

Commit

Permalink
Skip pg_stat_checkpointer collector if pg<17 (#1112)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
n-rodriguez and michael-todorovic authored Feb 20, 2025
1 parent 4c170ed commit 8bb1a41
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
16 changes: 13 additions & 3 deletions collector/pg_stat_checkpointer.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ package collector
import (
"context"
"database/sql"
"log/slog"

"github.com/blang/semver/v4"
"github.com/prometheus/client_golang/prometheus"
)

Expand All @@ -29,10 +31,11 @@ func init() {
}

type PGStatCheckpointerCollector struct {
log *slog.Logger
}

func NewPGStatCheckpointerCollector(collectorConfig) (Collector, error) {
return &PGStatCheckpointerCollector{}, nil
func NewPGStatCheckpointerCollector(config collectorConfig) (Collector, error) {
return &PGStatCheckpointerCollector{log: config.logger}, nil
}

var (
Expand Down Expand Up @@ -104,8 +107,15 @@ var (
FROM pg_stat_checkpointer;`
)

func (PGStatCheckpointerCollector) Update(ctx context.Context, instance *instance, ch chan<- prometheus.Metric) error {
func (c PGStatCheckpointerCollector) Update(ctx context.Context, instance *instance, ch chan<- prometheus.Metric) error {
db := instance.getDB()

before17 := instance.version.LT(semver.MustParse("17.0.0"))
if before17 {
c.log.Warn("pg_stat_checkpointer collector is not available on PostgreSQL < 17.0.0, skipping")
return nil
}

row := db.QueryRowContext(ctx, statCheckpointerQuery)

// num_timed = nt = bigint
Expand Down
5 changes: 3 additions & 2 deletions collector/pg_stat_checkpointer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"time"

"github.com/DATA-DOG/go-sqlmock"
"github.com/blang/semver/v4"
"github.com/prometheus/client_golang/prometheus"
dto "github.com/prometheus/client_model/go"
"github.com/smartystreets/goconvey/convey"
Expand All @@ -30,7 +31,7 @@ func TestPGStatCheckpointerCollector(t *testing.T) {
}
defer db.Close()

inst := &instance{db: db}
inst := &instance{db: db, version: semver.MustParse("17.0.0")}

columns := []string{
"num_timed",
Expand Down Expand Up @@ -92,7 +93,7 @@ func TestPGStatCheckpointerCollectorNullValues(t *testing.T) {
}
defer db.Close()

inst := &instance{db: db}
inst := &instance{db: db, version: semver.MustParse("17.0.0")}

columns := []string{
"num_timed",
Expand Down

0 comments on commit 8bb1a41

Please sign in to comment.