Skip to content

Commit 09c3129

Browse files
authored
ensure that 0-valued metrics are not collected if their reset_on_collect flag is set (#615)
1 parent b379ea2 commit 09c3129

File tree

3 files changed

+46
-4
lines changed

3 files changed

+46
-4
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# Changelog
22

3+
## Unreleased
4+
[Check the diff](https://github.com/elastic/apm-agent-python/compare/v5.2.2...master)
5+
6+
### Bugfixes
7+
8+
* ensure that metrics with value 0 are not collected if they have the `reset_on_collect` flag set (#615)
9+
310
## v5.2.2
411
[Check the diff](https://github.com/elastic/apm-agent-python/compare/v5.2.1...v5.2.2)
512

elasticapm/metrics/base_metrics.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -198,21 +198,26 @@ def collect(self):
198198
if self._counters:
199199
for (name, labels), c in compat.iteritems(self._counters):
200200
if c is not noop_metric:
201-
samples[labels].update({name: {"value": c.val}})
201+
val = c.val
202+
if val or not c.reset_on_collect:
203+
samples[labels].update({name: {"value": val}})
202204
if c.reset_on_collect:
203205
c.reset()
204206
if self._gauges:
205207
for (name, labels), g in compat.iteritems(self._gauges):
206208
if g is not noop_metric:
207-
samples[labels].update({name: {"value": g.val}})
209+
val = g.val
210+
if val or not g.reset_on_collect:
211+
samples[labels].update({name: {"value": val}})
208212
if g.reset_on_collect:
209213
g.reset()
210214
if self._timers:
211215
for (name, labels), t in compat.iteritems(self._timers):
212216
if t is not noop_metric:
213217
val, count = t.val
214-
samples[labels].update({name + ".sum.us": {"value": int(val * 1000000)}})
215-
samples[labels].update({name + ".count": {"value": count}})
218+
if val or not t.reset_on_collect:
219+
samples[labels].update({name + ".sum.us": {"value": int(val * 1000000)}})
220+
samples[labels].update({name + ".count": {"value": count}})
216221
if t.reset_on_collect:
217222
t.reset()
218223
if samples:

tests/metrics/base_tests.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,3 +163,33 @@ def test_metric_limit(caplog):
163163
assert len(caplog.records) == 1
164164
record = caplog.records[0]
165165
assert "The limit of 3 metricsets has been reached" in record.message
166+
167+
168+
def test_metrics_not_collected_if_zero_and_reset():
169+
m = MetricsSet(MetricsRegistry(0, lambda x: None))
170+
counter = m.counter("counter", reset_on_collect=False)
171+
resetting_counter = m.counter("resetting_counter", reset_on_collect=True)
172+
gauge = m.gauge("gauge", reset_on_collect=False)
173+
resetting_gauge = m.gauge("resetting_gauge", reset_on_collect=True)
174+
timer = m.timer("timer", reset_on_collect=False)
175+
resetting_timer = m.timer("resetting_timer", reset_on_collect=True)
176+
177+
counter.inc(), resetting_counter.inc()
178+
gauge.val = 5
179+
resetting_gauge.val = 5
180+
timer.update(1, 1)
181+
resetting_timer.update(1, 1)
182+
183+
data = list(m.collect())
184+
more_data = list(m.collect())
185+
assert set(data[0]["samples"].keys()) == {
186+
"counter",
187+
"resetting_counter",
188+
"gauge",
189+
"resetting_gauge",
190+
"timer.count",
191+
"timer.sum.us",
192+
"resetting_timer.count",
193+
"resetting_timer.sum.us",
194+
}
195+
assert set(more_data[0]["samples"].keys()) == {"counter", "gauge", "timer.count", "timer.sum.us"}

0 commit comments

Comments
 (0)