Skip to content

Commit 8ed082c

Browse files
committed
don't measure archive lag on replicas
1 parent b920f36 commit 8ed082c

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

collector/pg_stat_archiver_lag.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ var (
4646
statArchiverLagQuery = `
4747
SELECT
4848
last_archived_wal,
49-
pg_current_wal_lsn() AS current_lsn
49+
CASE WHEN pg_is_in_recovery() THEN NULL ELSE pg_current_wal_lsn() END AS current_lsn
5050
FROM pg_stat_archiver
5151
WHERE last_archived_wal IS NOT NULL
5252
AND last_archived_wal != ''

collector/pg_stat_archiver_lag_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,3 +263,43 @@ func TestBytesBetweenLSN(t *testing.T) {
263263
})
264264
}
265265
}
266+
267+
func TestPGStatArchiverLagCollectorReplica(t *testing.T) {
268+
db, mock, err := sqlmock.New()
269+
if err != nil {
270+
t.Fatalf("Error opening a stub db connection: %s", err)
271+
}
272+
defer db.Close()
273+
274+
inst := &Instance{db: db}
275+
276+
columns := []string{"last_archived_wal", "current_lsn"}
277+
// Simulate replica where current_lsn is NULL (which is what the query returns when in recovery)
278+
rows := sqlmock.NewRows(columns).
279+
AddRow("000000010000000000000001", nil)
280+
mock.ExpectQuery(sanitizeQuery(statArchiverLagQuery)).WillReturnRows(rows)
281+
282+
ch := make(chan prometheus.Metric)
283+
go func() {
284+
defer close(ch)
285+
c := PGStatArchiverLagCollector{}
286+
287+
if err := c.Update(context.Background(), inst, ch); err != nil {
288+
t.Errorf("Error calling PGStatArchiverLagCollector.Update: %s", err)
289+
}
290+
}()
291+
292+
expected := []MetricResult{
293+
{labels: labelMap{}, value: 0, metricType: dto.MetricType_GAUGE},
294+
}
295+
296+
convey.Convey("Metrics comparison", t, func() {
297+
for _, expect := range expected {
298+
m := readMetric(<-ch)
299+
convey.So(expect, convey.ShouldResemble, m)
300+
}
301+
})
302+
if err := mock.ExpectationsWereMet(); err != nil {
303+
t.Errorf("there were unfulfilled exceptions: %s", err)
304+
}
305+
}

0 commit comments

Comments
 (0)