diff --git a/CHANGELOG.md b/CHANGELOG.md index 94ee47e7..e85e3baa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 (`metrics.cfg{include={'all'}}` can be used instead of `metrics.cfg{include='all'}`, `metrics.cfg{exclude={'all'}}` can be used instead of `metrics.cfg{include='none'}`) +- `tnt_election_leader_idle` metric. + ## [1.0.0] - 2023-05-22 ### Changed diff --git a/doc/monitoring/metrics_reference.rst b/doc/monitoring/metrics_reference.rst index 249fd09e..1e5c58ed 100644 --- a/doc/monitoring/metrics_reference.rst +++ b/doc/monitoring/metrics_reference.rst @@ -773,6 +773,9 @@ Shows the current state of a replica set node in regards to leader election. * - ``tnt_election_term`` - Current election term. + * - ``tnt_election_leader_idle`` + - Time in seconds since the last interaction with the known leader. + Memtx ----- diff --git a/metrics/tarantool/info.lua b/metrics/tarantool/info.lua index 80891725..3e63d2bc 100644 --- a/metrics/tarantool/info.lua +++ b/metrics/tarantool/info.lua @@ -91,6 +91,12 @@ local function update_info_metrics() collectors_list.election_term = utils.set_gauge('election_term', 'Current election term', info.election.term, nil, nil, {default = true}) + + if info.election.leader_idle ~= nil then + collectors_list.election_leader_idle = + utils.set_gauge('election_leader_idle', 'Current idle for elected leader', + info.election.leader_idle, nil, nil, {default = true}) + end end end diff --git a/test/tarantool/info_metrics_test.lua b/test/tarantool/info_metrics_test.lua index 3eebb8f2..c50f5c78 100644 --- a/test/tarantool/info_metrics_test.lua +++ b/test/tarantool/info_metrics_test.lua @@ -93,3 +93,26 @@ g.test_election = function(cg) t.assert_type(tnt_election_term[1].value, 'number') end) end + +g.test_election_leader_idle = function(cg) + t.skip_if(utils.is_version_less(_TARANTOOL, '2.10.0'), + 'Tarantool version is must be v2.10.0 or greater') + + cg.server:exec(function() + box.cfg{election_mode = 'candidate'} + local metrics = require('metrics') + local info = require('metrics.tarantool.info') + local utils = require('test.utils') -- luacheck: ignore 431 + + metrics.enable_default_metrics() + info.update() + local default_metrics = metrics.collect() + + local tnt_election_leader_idle = utils.find_metric( + 'tnt_election_leader_idle', default_metrics + ) + t.assert(tnt_election_leader_idle) + t.assert_type(tnt_election_leader_idle[1].value, 'number') + box.cfg{election_mode = 'off'} + end) +end