Skip to content
Open
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
25 changes: 23 additions & 2 deletions collector/pg_wal.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,26 @@ var (
[]string{}, nil,
)

pgWALLSNInBytes = prometheus.NewDesc(
prometheus.BuildFQName(
namespace,
walSubsystem,
"lsn_location_bytes",
),
"WAL Log Sequence Number in bytes",
[]string{}, nil,
)

pgWALQuery = `
SELECT
COUNT(*) AS segments,
SUM(size) AS size
SUM(size) AS size,
(SELECT CASE
WHEN pg_is_in_recovery() THEN
pg_wal_lsn_diff(pg_last_wal_replay_lsn(), '0/0')::int8
ELSE
pg_wal_lsn_diff(pg_current_wal_lsn(), '0/0')::int8
END) AS lsn_location_bytes
FROM pg_ls_waldir()
WHERE name ~ '^[0-9A-F]{24}$'`
)
Expand All @@ -68,7 +84,8 @@ func (c PGWALCollector) Update(ctx context.Context, instance *instance, ch chan<

var segments uint64
var size uint64
err := row.Scan(&segments, &size)
var lsnbytes uint64
err := row.Scan(&segments, &size, &lsnbytes)
if err != nil {
return err
}
Expand All @@ -80,5 +97,9 @@ func (c PGWALCollector) Update(ctx context.Context, instance *instance, ch chan<
pgWALSize,
prometheus.GaugeValue, float64(size),
)
ch <- prometheus.MustNewConstMetric(
pgWALLSNInBytes,
prometheus.GaugeValue, float64(lsnbytes),
)
return nil
}
5 changes: 3 additions & 2 deletions collector/pg_wal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ func TestPgWALCollector(t *testing.T) {

inst := &instance{db: db}

columns := []string{"segments", "size"}
columns := []string{"segments", "size", "lsn_location_bytes"}
rows := sqlmock.NewRows(columns).
AddRow(47, 788529152)
AddRow(47, 788529152, 123456789)
mock.ExpectQuery(sanitizeQuery(pgWALQuery)).WillReturnRows(rows)

ch := make(chan prometheus.Metric)
Expand All @@ -49,6 +49,7 @@ func TestPgWALCollector(t *testing.T) {
expected := []MetricResult{
{labels: labelMap{}, value: 47, metricType: dto.MetricType_GAUGE},
{labels: labelMap{}, value: 788529152, metricType: dto.MetricType_GAUGE},
{labels: labelMap{}, value: 123456789, metricType: dto.MetricType_GAUGE},
}

convey.Convey("Metrics comparison", t, func() {
Expand Down