diff --git a/CHANGELOG.md b/CHANGELOG.md index 96236346..15d5ca72 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased] +### Added +- `counter:reset()` method [#288](https://github.com/tarantool/metrics/issues/288) ## [0.10.0] - 2021-08-03 ### Changed diff --git a/doc/monitoring/api_reference.rst b/doc/monitoring/api_reference.rst index 1737cffa..8181ef56 100644 --- a/doc/monitoring/api_reference.rst +++ b/doc/monitoring/api_reference.rst @@ -65,6 +65,13 @@ Counter :rtype: table + .. method:: reset(label_pairs) + + Set an observation under ``label_pairs`` to 0. + + :param table label_pairs: Table containing label names as keys, + label values as values. + .. _gauge: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/metrics/collectors/counter.lua b/metrics/collectors/counter.lua index 4f922c54..ddb9c48b 100644 --- a/metrics/collectors/counter.lua +++ b/metrics/collectors/counter.lua @@ -9,4 +9,8 @@ function Counter:inc(num, label_pairs) Shared.inc(self, num, label_pairs) end +function Counter:reset(label_pairs) + Shared.set(self, 0, label_pairs) +end + return Counter diff --git a/test/collectors_test.lua b/test/collectors_test.lua index 62c683ba..b728c02d 100755 --- a/test/collectors_test.lua +++ b/test/collectors_test.lua @@ -268,3 +268,11 @@ g.test_global_labels = function() { le = metrics.INF, alias = 'another alias', nlabel = 3 }, observations) t.assert_equals(obs_bucket_inf_hist_2.value, 1, "bucket +inf has 1 value: 2; observation global label has changed") end + +g.test_collector_reset = function() + local c = metrics.counter('cnt', 'some counter') + c:inc() + t.assert_equals(c:collect()[1].value, 1) + c:reset() + t.assert_equals(c:collect()[1].value, 0) +end