Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PMM-13448 Support PG17 to provide pre PG17 metrics support. #281

Merged
merged 1 commit into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 44 additions & 9 deletions collector/pg_stat_bgwriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ package collector
import (
"context"
"database/sql"

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

Expand Down Expand Up @@ -170,7 +170,7 @@ var statBGWriter = map[string]*prometheus.Desc{
),
}

const statBGWriterQuery = `SELECT
const statBGWriterQueryPrePG17 = `SELECT
checkpoints_timed
,checkpoints_req
,checkpoint_write_time
Expand All @@ -184,19 +184,55 @@ const statBGWriterQuery = `SELECT
,stats_reset
FROM pg_stat_bgwriter;`

func (PGStatBGWriterCollector) Update(ctx context.Context, instance *instance, ch chan<- prometheus.Metric) error {
const statBGWriterQueryPost17 = `SELECT
buffers_clean
,maxwritten_clean
,buffers_alloc
,stats_reset
FROM pg_stat_bgwriter;`

const statCheckpointerQuery = `SELECT
num_timed
,num_requested
,restartpoints_timed
,restartpoints_req
,restartpoints_done
,write_time
,sync_time
,buffers_written
,stats_reset
FROM pg_stat_checkpointer;`

func (p PGStatBGWriterCollector) Update(ctx context.Context, instance *instance, ch chan<- prometheus.Metric) error {
db := instance.getDB()
row := db.QueryRowContext(ctx,
statBGWriterQuery)

var cpt, cpr, bcp, bc, mwc, bb, bbf, ba sql.NullInt64
var cpwt, cpst sql.NullFloat64
var sr sql.NullTime

err := row.Scan(&cpt, &cpr, &cpwt, &cpst, &bcp, &bc, &mwc, &bb, &bbf, &ba, &sr)
if err != nil {
return err
if instance.version.GE(semver.MustParse("17.0.0")) {
row := db.QueryRowContext(ctx,
statBGWriterQueryPost17)
err := row.Scan(&bc, &mwc, &ba, &sr)
if err != nil {
return err
}
var rpt, rpr, rpd sql.NullInt64
var csr sql.NullTime
// these variables are not used, but I left them here for reference
row = db.QueryRowContext(ctx,
statCheckpointerQuery)
err = row.Scan(&cpt, &cpr, &rpt, &rpr, &rpd, &cpwt, &cpst, &bcp, &csr)
if err != nil {
return err
}
} else {
row := db.QueryRowContext(ctx,
statBGWriterQueryPrePG17)
err := row.Scan(&cpt, &cpr, &cpwt, &cpst, &bcp, &bc, &mwc, &bb, &bbf, &ba, &sr)
if err != nil {
return err
}
}

cptMetric := 0.0
Expand Down Expand Up @@ -400,6 +436,5 @@ func (PGStatBGWriterCollector) Update(ctx context.Context, instance *instance, c
"exporter",
instance.name,
)

return nil
}
4 changes: 2 additions & 2 deletions collector/pg_stat_bgwriter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func TestPGStatBGWriterCollector(t *testing.T) {

rows := sqlmock.NewRows(columns).
AddRow(354, 4945, 289097744, 1242257, int64(3275602074), 89320867, 450139, 2034563757, 0, int64(2725688749), srT)
mock.ExpectQuery(sanitizeQuery(statBGWriterQuery)).WillReturnRows(rows)
mock.ExpectQuery(sanitizeQuery(statBGWriterQueryPrePG17)).WillReturnRows(rows)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we cover this change with unit tests for PG >= 17 as well?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's a temporary solution until we get proper solution from upstream


ch := make(chan prometheus.Metric)
go func() {
Expand Down Expand Up @@ -114,7 +114,7 @@ func TestPGStatBGWriterCollectorNullValues(t *testing.T) {

rows := sqlmock.NewRows(columns).
AddRow(nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil)
mock.ExpectQuery(sanitizeQuery(statBGWriterQuery)).WillReturnRows(rows)
mock.ExpectQuery(sanitizeQuery(statBGWriterQueryPrePG17)).WillReturnRows(rows)

ch := make(chan prometheus.Metric)
go func() {
Expand Down