Skip to content

Commit 5eed986

Browse files
committed
Report never-set gauges in mostrecent multiprocess mode
In the mostrecent/livemostrecent accumulation branch, sample_timestamps is a defaultdict(float), so the stored timestamp of a series defaults to 0.0. A gauge child that was created but never set() also has a stored timestamp of 0, so the strict 'current_timestamp < timestamp' check was never satisfied and the series was dropped from collection entirely. Every other gauge mode (sum, min, max, all) reports such a child as 0, so mostrecent silently omitting it was inconsistent. Register the sample when it is first seen, regardless of timestamp, and still let a strictly newer timestamp win afterwards. Add test_gauge_mostrecent_never_set, asserting a mostrecent gauge that is created but never set is reported as 0 rather than missing. Signed-off-by: Sean Kim <skim8705@gmail.com>
1 parent a96f6f4 commit 5eed986

2 files changed

Lines changed: 11 additions & 1 deletion

File tree

prometheus_client/multiprocess.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ def _accumulate_metrics(metrics, accumulate):
119119
elif metric._multiprocess_mode in ('mostrecent', 'livemostrecent'):
120120
current_timestamp = sample_timestamps[labels][name]
121121
timestamp = float(timestamp or 0)
122-
if current_timestamp < timestamp:
122+
if (name, labels) not in samples[labels] or current_timestamp < timestamp:
123123
samples[labels][(name, labels)] = value
124124
sample_timestamps[labels][name] = timestamp
125125
else: # all/liveall

tests/test_multiprocess.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,16 @@ def test_gauge_livemostrecent(self):
205205
mark_process_dead(123, os.environ['PROMETHEUS_MULTIPROC_DIR'])
206206
self.assertEqual(2, self.registry.get_sample_value('g'))
207207

208+
def test_gauge_mostrecent_never_set(self):
209+
# A child created but never set() has a stored timestamp of 0. It must
210+
# still be reported as 0, like every other gauge mode, rather than
211+
# dropped from collection because 0 is not strictly greater than the
212+
# default timestamp of 0.
213+
Gauge('g', 'help', registry=None, multiprocess_mode='mostrecent')
214+
values.ValueClass = MultiProcessValue(lambda: 456)
215+
Gauge('g', 'help', registry=None, multiprocess_mode='mostrecent')
216+
self.assertEqual(0, self.registry.get_sample_value('g'))
217+
208218
def test_namespace_subsystem(self):
209219
c1 = Counter('c', 'help', registry=None, namespace='ns', subsystem='ss')
210220
c1.inc(1)

0 commit comments

Comments
 (0)