Skip to content

Commit 3007500

Browse files
author
Zachary Caldarola
committed
adding codified functionality for logical replication metrics
Signed-off-by: Zachary Caldarola <zachary.caldarola@reddit.com>
1 parent d4af942 commit 3007500

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

Diff for: collector/pg_replication_slots.go

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// Copyright 2022 The Prometheus Authors
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
package collector
15+
16+
import (
17+
"context"
18+
"database/sql"
19+
20+
"github.com/go-kit/log"
21+
"github.com/prometheus/client_golang/prometheus"
22+
)
23+
24+
func init() {
25+
registerCollector("replication_slot", defaultEnabled, NewPGReplicationSlotCollector)
26+
}
27+
28+
type PGReplicationSlotCollector struct {
29+
log log.Logger
30+
}
31+
32+
func NewPGReplicationSlotCollector(logger log.Logger) (Collector, error) {
33+
return &PGReplicationSlotCollector{log: logger}, nil
34+
}
35+
36+
var pgReplicationSlot = map[string]*prometheus.Desc{
37+
"lsn_distance": prometheus.NewDesc(
38+
"pg_replication_slot_lsn_distance",
39+
"Disk space used by the database",
40+
[]string{"slot_name"}, nil,
41+
),
42+
}
43+
44+
func (PGReplicationSlotCollector) Update(ctx context.Context, db *sql.DB, ch chan<- prometheus.Metric) error {
45+
rows, err := db.QueryContext(ctx,
46+
`SELECT
47+
slot_name,
48+
(pg_current_wal_lsn() - confirmed_flush_lsn) AS lsn_distance
49+
FROM
50+
pg_replication_slots;`)
51+
if err != nil {
52+
return err
53+
}
54+
defer rows.Close()
55+
56+
for rows.Next() {
57+
var slot_name string
58+
var size int64
59+
if err := rows.Scan(&slot_name, &size); err != nil {
60+
return err
61+
}
62+
63+
ch <- prometheus.MustNewConstMetric(
64+
pgReplicationSlot["size_bytes"],
65+
prometheus.GaugeValue, float64(size), slot_name,
66+
)
67+
}
68+
if err := rows.Err(); err != nil {
69+
return err
70+
}
71+
return nil
72+
}

0 commit comments

Comments
 (0)