addressing comments

Signed-off-by: Zachary Caldarola <zachary.caldarola@reddit.com>
This commit is contained in:
Zachary Caldarola 2023-01-25 11:15:24 -05:00
parent 300750001c
commit 9b13780d14

View File

@ -1,4 +1,4 @@
// Copyright 2022 The Prometheus Authors // Copyright 2023 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License"); // Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License. // you may not use this file except in compliance with the License.
// You may obtain a copy of the License at // You may obtain a copy of the License at
@ -34,9 +34,14 @@ func NewPGReplicationSlotCollector(logger log.Logger) (Collector, error) {
} }
var pgReplicationSlot = map[string]*prometheus.Desc{ var pgReplicationSlot = map[string]*prometheus.Desc{
"lsn_distance": prometheus.NewDesc( "current_wal_lsn": prometheus.NewDesc(
"pg_replication_slot_lsn_distance", "pg_replication_slot_current_wal_lsn",
"Disk space used by the database", "current wal lsn value",
[]string{"slot_name"}, nil,
),
"confirmed_flush_lsn": prometheus.NewDesc(
"pg_replication_slot_confirmed_flush_lsn",
"last lsn confirmed flushed to the replication slot",
[]string{"slot_name"}, nil, []string{"slot_name"}, nil,
), ),
} }
@ -45,7 +50,8 @@ func (PGReplicationSlotCollector) Update(ctx context.Context, db *sql.DB, ch cha
rows, err := db.QueryContext(ctx, rows, err := db.QueryContext(ctx,
`SELECT `SELECT
slot_name, slot_name,
(pg_current_wal_lsn() - confirmed_flush_lsn) AS lsn_distance pg_current_wal_lsn() AS current_wal_lsn,
confirmed_flush_lsn
FROM FROM
pg_replication_slots;`) pg_replication_slots;`)
if err != nil { if err != nil {
@ -55,14 +61,19 @@ func (PGReplicationSlotCollector) Update(ctx context.Context, db *sql.DB, ch cha
for rows.Next() { for rows.Next() {
var slot_name string var slot_name string
var size int64 var wal_lsn int64
if err := rows.Scan(&slot_name, &size); err != nil { var flush_lsn int64
if err := rows.Scan(&slot_name, &wal_lsn, &flush_lsn); err != nil {
return err return err
} }
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
pgReplicationSlot["size_bytes"], pgReplicationSlot["current_wal_lsn"],
prometheus.GaugeValue, float64(size), slot_name, prometheus.GaugeValue, float64(wal_lsn), slot_name,
)
ch <- prometheus.MustNewConstMetric(
pgReplicationSlot["confirmed_flush_lsn"],
prometheus.GaugeValue, float64(flush_lsn), slot_name,
) )
} }
if err := rows.Err(); err != nil { if err := rows.Err(); err != nil {