Skip to content

Commit c2295e1

Browse files
committed
feat: add replication info to /metrics endpoint
Exposes 4 new fields to the `/metrics` endpoint relating to replication info: - `master_link_status` - `master_last_io_seconds_ago` - `master_sync_in_progress` - `slave_repl_offset` This commit also adds integration testing to validate the new functionality. Fixes #6183 Signed-off-by: Eric <hayter.eric@gmail.com>
1 parent 5db9666 commit c2295e1

2 files changed

Lines changed: 41 additions & 1 deletion

File tree

src/server/server_family.cc

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2127,6 +2127,24 @@ void PrintPrometheusMetrics(uint64_t uptime, const Metrics& m, DflyCmd* dfly_cmd
21272127
&resp->body());
21282128
}
21292129

2130+
// Replication Info
2131+
if (m.replica_side_info) {
2132+
const ReplicaSummary& rsummary = m.replica_side_info->summary;
2133+
AppendMetricWithoutLabels("master_link_status", "1 if up 0 if down",
2134+
rsummary.master_link_established ? 1 : 0, MetricType::GAUGE,
2135+
&resp->body());
2136+
AppendMetricWithoutLabels("master_last_io_seconds_ago", "Last Master IO Seconds Ago",
2137+
rsummary.master_last_io_sec, MetricType::GAUGE, &resp->body());
2138+
AppendMetricWithoutLabels("master_sync_in_progress", "1 if true 0 if false",
2139+
rsummary.full_sync_in_progress ? 1 : 0, MetricType::GAUGE,
2140+
&resp->body());
2141+
// Print last known offset either during stable sync (online) or during disconnects when
2142+
// the full sync phase did not start yet.
2143+
if (rsummary.full_sync_done || (rsummary.passed_full_sync && !rsummary.master_link_established))
2144+
AppendMetricWithoutLabels("slave_repl_offset", "Slave Replication Offset",
2145+
rsummary.repl_offset_sum, MetricType::GAUGE, &resp->body());
2146+
}
2147+
21302148
// Stream access pattern metrics
21312149
if (m.shard_stats.stream_sequential_accesses || m.shard_stats.stream_random_accesses ||
21322150
m.shard_stats.stream_fetch_all_accesses) {

tests/dragonfly/server_family_test.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from redis.exceptions import ResponseError
88

99
from . import dfly_args
10-
from .instance import DflyInstance
10+
from .instance import DflyInstance, DflyInstanceFactory
1111
from .utility import *
1212

1313

@@ -313,3 +313,25 @@ async def check_metrics():
313313
assert m.samples[0].value > 0
314314

315315
await check_metrics()
316+
317+
318+
async def test_replication_metrics(df_factory: DflyInstanceFactory):
319+
master = df_factory.create(proactor_threads=2)
320+
replica = df_factory.create(proactor_threads=2)
321+
df_factory.start_all([master, replica])
322+
323+
c_replica = replica.client()
324+
await c_replica.execute_command("REPLICAOF", "localhost", master.port)
325+
await wait_for_replicas_state(c_replica)
326+
327+
# Replica should expose replication metrics
328+
metrics = await replica.metrics()
329+
assert metrics["dragonfly_master_link_status"].samples[0].value == 1
330+
assert metrics["dragonfly_master_sync_in_progress"].samples[0].value == 0
331+
assert metrics["dragonfly_master_last_io_seconds_ago"].samples[0].value >= 0
332+
assert "dragonfly_slave_repl_offset" in metrics
333+
334+
# Master should not expose replica-side metrics
335+
master_metrics = await master.metrics()
336+
assert "dragonfly_master_link_status" not in master_metrics
337+
assert "dragonfly_slave_repl_offset" not in master_metrics

0 commit comments

Comments
 (0)