Skip to content

Commit 9586668

Browse files
shamilartemgavrilov
authored andcommitted
pg_replication_slot: add slot type label (prometheus-community#960)
Signed-off-by: Alex Simenduev <[email protected]>
1 parent ab10107 commit 9586668

File tree

2 files changed

+35
-29
lines changed

2 files changed

+35
-29
lines changed

collector/pg_replication_slot.go

+17-11
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ var (
4343
"slot_current_wal_lsn",
4444
),
4545
"current wal lsn value",
46-
[]string{"slot_name"}, nil,
46+
[]string{"slot_name", "slot_type"}, nil,
4747
)
4848
pgReplicationSlotCurrentFlushDesc = prometheus.NewDesc(
4949
prometheus.BuildFQName(
@@ -52,7 +52,7 @@ var (
5252
"slot_confirmed_flush_lsn",
5353
),
5454
"last lsn confirmed flushed to the replication slot",
55-
[]string{"slot_name"}, nil,
55+
[]string{"slot_name", "slot_type"}, nil,
5656
)
5757
pgReplicationSlotIsActiveDesc = prometheus.NewDesc(
5858
prometheus.BuildFQName(
@@ -61,17 +61,18 @@ var (
6161
"slot_is_active",
6262
),
6363
"whether the replication slot is active or not",
64-
[]string{"slot_name"}, nil,
64+
[]string{"slot_name", "slot_type"}, nil,
6565
)
6666

6767
pgReplicationSlotQuery = `SELECT
6868
slot_name,
69-
CASE WHEN pg_is_in_recovery() THEN
69+
slot_type,
70+
CASE WHEN pg_is_in_recovery() THEN
7071
pg_last_wal_receive_lsn() - '0/0'
71-
ELSE
72-
pg_current_wal_lsn() - '0/0'
72+
ELSE
73+
pg_current_wal_lsn() - '0/0'
7374
END AS current_wal_lsn,
74-
COALESCE(confirmed_flush_lsn, '0/0') - '0/0',
75+
COALESCE(confirmed_flush_lsn, '0/0') - '0/0' AS confirmed_flush_lsn,
7576
active
7677
FROM pg_replication_slots;`
7778
)
@@ -87,10 +88,11 @@ func (PGReplicationSlotCollector) Update(ctx context.Context, instance *instance
8788

8889
for rows.Next() {
8990
var slotName sql.NullString
91+
var slotType sql.NullString
9092
var walLSN sql.NullFloat64
9193
var flushLSN sql.NullFloat64
9294
var isActive sql.NullBool
93-
if err := rows.Scan(&slotName, &walLSN, &flushLSN, &isActive); err != nil {
95+
if err := rows.Scan(&slotName, &slotType, &walLSN, &flushLSN, &isActive); err != nil {
9496
return err
9597
}
9698

@@ -102,14 +104,18 @@ func (PGReplicationSlotCollector) Update(ctx context.Context, instance *instance
102104
if slotName.Valid {
103105
slotNameLabel = slotName.String
104106
}
107+
slotTypeLabel := "unknown"
108+
if slotType.Valid {
109+
slotTypeLabel = slotType.String
110+
}
105111

106112
var walLSNMetric float64
107113
if walLSN.Valid {
108114
walLSNMetric = walLSN.Float64
109115
}
110116
ch <- prometheus.MustNewConstMetric(
111117
pgReplicationSlotCurrentWalDesc,
112-
prometheus.GaugeValue, walLSNMetric, slotNameLabel,
118+
prometheus.GaugeValue, walLSNMetric, slotNameLabel, slotTypeLabel,
113119
)
114120
if isActive.Valid && isActive.Bool {
115121
var flushLSNMetric float64
@@ -118,12 +124,12 @@ func (PGReplicationSlotCollector) Update(ctx context.Context, instance *instance
118124
}
119125
ch <- prometheus.MustNewConstMetric(
120126
pgReplicationSlotCurrentFlushDesc,
121-
prometheus.GaugeValue, flushLSNMetric, slotNameLabel,
127+
prometheus.GaugeValue, flushLSNMetric, slotNameLabel, slotTypeLabel,
122128
)
123129
}
124130
ch <- prometheus.MustNewConstMetric(
125131
pgReplicationSlotIsActiveDesc,
126-
prometheus.GaugeValue, isActiveValue, slotNameLabel,
132+
prometheus.GaugeValue, isActiveValue, slotNameLabel, slotTypeLabel,
127133
)
128134
}
129135
return rows.Err()

collector/pg_replication_slot_test.go

+18-18
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ func TestPgReplicationSlotCollectorActive(t *testing.T) {
3131

3232
inst := &instance{db: db}
3333

34-
columns := []string{"slot_name", "current_wal_lsn", "confirmed_flush_lsn", "active"}
34+
columns := []string{"slot_name", "slot_type", "current_wal_lsn", "confirmed_flush_lsn", "active"}
3535
rows := sqlmock.NewRows(columns).
36-
AddRow("test_slot", 5, 3, true)
36+
AddRow("test_slot", "physical", 5, 3, true)
3737
mock.ExpectQuery(sanitizeQuery(pgReplicationSlotQuery)).WillReturnRows(rows)
3838

3939
ch := make(chan prometheus.Metric)
@@ -47,9 +47,9 @@ func TestPgReplicationSlotCollectorActive(t *testing.T) {
4747
}()
4848

4949
expected := []MetricResult{
50-
{labels: labelMap{"slot_name": "test_slot"}, value: 5, metricType: dto.MetricType_GAUGE},
51-
{labels: labelMap{"slot_name": "test_slot"}, value: 3, metricType: dto.MetricType_GAUGE},
52-
{labels: labelMap{"slot_name": "test_slot"}, value: 1, metricType: dto.MetricType_GAUGE},
50+
{labels: labelMap{"slot_name": "test_slot", "slot_type": "physical"}, value: 5, metricType: dto.MetricType_GAUGE},
51+
{labels: labelMap{"slot_name": "test_slot", "slot_type": "physical"}, value: 3, metricType: dto.MetricType_GAUGE},
52+
{labels: labelMap{"slot_name": "test_slot", "slot_type": "physical"}, value: 1, metricType: dto.MetricType_GAUGE},
5353
}
5454

5555
convey.Convey("Metrics comparison", t, func() {
@@ -72,9 +72,9 @@ func TestPgReplicationSlotCollectorInActive(t *testing.T) {
7272

7373
inst := &instance{db: db}
7474

75-
columns := []string{"slot_name", "current_wal_lsn", "confirmed_flush_lsn", "active"}
75+
columns := []string{"slot_name", "slot_type", "current_wal_lsn", "confirmed_flush_lsn", "active"}
7676
rows := sqlmock.NewRows(columns).
77-
AddRow("test_slot", 6, 12, false)
77+
AddRow("test_slot", "physical", 6, 12, false)
7878
mock.ExpectQuery(sanitizeQuery(pgReplicationSlotQuery)).WillReturnRows(rows)
7979

8080
ch := make(chan prometheus.Metric)
@@ -88,8 +88,8 @@ func TestPgReplicationSlotCollectorInActive(t *testing.T) {
8888
}()
8989

9090
expected := []MetricResult{
91-
{labels: labelMap{"slot_name": "test_slot"}, value: 6, metricType: dto.MetricType_GAUGE},
92-
{labels: labelMap{"slot_name": "test_slot"}, value: 0, metricType: dto.MetricType_GAUGE},
91+
{labels: labelMap{"slot_name": "test_slot", "slot_type": "physical"}, value: 6, metricType: dto.MetricType_GAUGE},
92+
{labels: labelMap{"slot_name": "test_slot", "slot_type": "physical"}, value: 0, metricType: dto.MetricType_GAUGE},
9393
}
9494

9595
convey.Convey("Metrics comparison", t, func() {
@@ -113,9 +113,9 @@ func TestPgReplicationSlotCollectorActiveNil(t *testing.T) {
113113

114114
inst := &instance{db: db}
115115

116-
columns := []string{"slot_name", "current_wal_lsn", "confirmed_flush_lsn", "active"}
116+
columns := []string{"slot_name", "slot_type", "current_wal_lsn", "confirmed_flush_lsn", "active"}
117117
rows := sqlmock.NewRows(columns).
118-
AddRow("test_slot", 6, 12, nil)
118+
AddRow("test_slot", "physical", 6, 12, nil)
119119
mock.ExpectQuery(sanitizeQuery(pgReplicationSlotQuery)).WillReturnRows(rows)
120120

121121
ch := make(chan prometheus.Metric)
@@ -129,8 +129,8 @@ func TestPgReplicationSlotCollectorActiveNil(t *testing.T) {
129129
}()
130130

131131
expected := []MetricResult{
132-
{labels: labelMap{"slot_name": "test_slot"}, value: 6, metricType: dto.MetricType_GAUGE},
133-
{labels: labelMap{"slot_name": "test_slot"}, value: 0, metricType: dto.MetricType_GAUGE},
132+
{labels: labelMap{"slot_name": "test_slot", "slot_type": "physical"}, value: 6, metricType: dto.MetricType_GAUGE},
133+
{labels: labelMap{"slot_name": "test_slot", "slot_type": "physical"}, value: 0, metricType: dto.MetricType_GAUGE},
134134
}
135135

136136
convey.Convey("Metrics comparison", t, func() {
@@ -153,9 +153,9 @@ func TestPgReplicationSlotCollectorTestNilValues(t *testing.T) {
153153

154154
inst := &instance{db: db}
155155

156-
columns := []string{"slot_name", "current_wal_lsn", "confirmed_flush_lsn", "active"}
156+
columns := []string{"slot_name", "slot_type", "current_wal_lsn", "confirmed_flush_lsn", "active"}
157157
rows := sqlmock.NewRows(columns).
158-
AddRow(nil, nil, nil, true)
158+
AddRow(nil, nil, nil, nil, true)
159159
mock.ExpectQuery(sanitizeQuery(pgReplicationSlotQuery)).WillReturnRows(rows)
160160

161161
ch := make(chan prometheus.Metric)
@@ -169,9 +169,9 @@ func TestPgReplicationSlotCollectorTestNilValues(t *testing.T) {
169169
}()
170170

171171
expected := []MetricResult{
172-
{labels: labelMap{"slot_name": "unknown"}, value: 0, metricType: dto.MetricType_GAUGE},
173-
{labels: labelMap{"slot_name": "unknown"}, value: 0, metricType: dto.MetricType_GAUGE},
174-
{labels: labelMap{"slot_name": "unknown"}, value: 1, metricType: dto.MetricType_GAUGE},
172+
{labels: labelMap{"slot_name": "unknown", "slot_type": "unknown"}, value: 0, metricType: dto.MetricType_GAUGE},
173+
{labels: labelMap{"slot_name": "unknown", "slot_type": "unknown"}, value: 0, metricType: dto.MetricType_GAUGE},
174+
{labels: labelMap{"slot_name": "unknown", "slot_type": "unknown"}, value: 1, metricType: dto.MetricType_GAUGE},
175175
}
176176

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

0 commit comments

Comments
 (0)