Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add counter:reset method #288

Merged
merged 1 commit into from
Aug 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
### Added
- collector's method `remove` to clear observations with given label pairs [#263](https://github.com/tarantool/metrics/issues/263)
- `counter:reset()` method [#260](https://github.com/tarantool/metrics/issues/260)

## [0.10.0] - 2021-08-03
### Changed
Expand Down
4 changes: 4 additions & 0 deletions doc/monitoring/api_reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ Counter

Removes an observation with ``label_pairs``.

.. 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.

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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The original request was about counter initialization. Initialization with counter:reset(label_pairs) seems kinda weird. How about adding an alias for reset as a little syntactic sugar?

Counter.initialize = reset

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

init / reinit / reset, who cares. Reset is better than inc(0), and that's the purpose.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Init which resets the value may be considered weird too.

Shared.set(self, 0, label_pairs)
end

return Counter
9 changes: 8 additions & 1 deletion test/collectors_test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,6 @@ g.test_global_labels = function()
t.assert_equals(obs_bucket_inf_hist_2.value, 1, "bucket +inf has 1 value: 2; observation global label has changed")
end


g.test_counter_remove_metric_by_label = function()
local c = metrics.counter('cnt')

Expand Down Expand Up @@ -303,3 +302,11 @@ g.test_gauge_remove_metric_by_label = function()
{'gauge', 1, {label = 2}},
})
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