Skip to content

Commit

Permalink
Add counter:reset method
Browse files Browse the repository at this point in the history
  • Loading branch information
yngvar-antonsson committed Aug 19, 2021
1 parent 91629f7 commit 762ef62
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 [#260](https://github.com/tarantool/metrics/issues/260)

## [0.10.0] - 2021-08-03
### Changed
Expand Down
7 changes: 7 additions & 0 deletions doc/monitoring/api_reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down
4 changes: 4 additions & 0 deletions metrics/collectors/counter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 8 additions & 0 deletions test/collectors_test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 762ef62

Please sign in to comment.