From 28d0f96bbcc405c7c70d065f05c4719685017084 Mon Sep 17 00:00:00 2001 From: Ratan Boddu Date: Wed, 22 May 2019 19:46:11 +0530 Subject: [PATCH] Added _delta in Histogram and Summary Signed-off-by: Ratan Boddu --- prometheus_client/metrics.py | 10 +++++++++- tests/openmetrics/test_exposition.py | 2 ++ tests/test_exposition.py | 2 ++ 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/prometheus_client/metrics.py b/prometheus_client/metrics.py index b7c5e5a4..b4fd1534 100644 --- a/prometheus_client/metrics.py +++ b/prometheus_client/metrics.py @@ -412,12 +412,15 @@ def _metric_init(self): self._count = values.ValueClass(self._type, self._name, self._name + '_count', self._labelnames, self._labelvalues) self._sum = values.ValueClass(self._type, self._name, self._name + '_sum', self._labelnames, self._labelvalues) + self._delta = 0.0 self._created = time.time() + def observe(self, amount): """Observe the given amount.""" self._count.inc(1) self._sum.inc(amount) + self._delta = amount def time(self): """Time a block of code or function, and observe the duration in seconds. @@ -430,7 +433,8 @@ def _child_samples(self): return ( ('_count', {}, self._count.get()), ('_sum', {}, self._sum.get()), - ('_created', {}, self._created)) + ('_created', {}, self._created), + ('_delta', {}, self._delta)) class Histogram(MetricWrapperBase): @@ -511,6 +515,7 @@ def _prepare_buckets(self, buckets): def _metric_init(self): self._buckets = [] self._created = time.time() + self._delta = 0.0 bucket_labelnames = self._labelnames + ('le',) self._sum = values.ValueClass(self._type, self._name, self._name + '_sum', self._labelnames, self._labelvalues) for b in self._upper_bounds: @@ -525,6 +530,7 @@ def _metric_init(self): def observe(self, amount): """Observe the given amount.""" self._sum.inc(amount) + self._delta = amount for i, bound in enumerate(self._upper_bounds): if amount <= bound: self._buckets[i].inc(1) @@ -546,6 +552,8 @@ def _child_samples(self): samples.append(('_count', {}, acc)) samples.append(('_sum', {}, self._sum.get())) samples.append(('_created', {}, self._created)) + samples.append(('_delta', {}, self._delta)) + return tuple(samples) diff --git a/tests/openmetrics/test_exposition.py b/tests/openmetrics/test_exposition.py index 502a45e0..29d47653 100644 --- a/tests/openmetrics/test_exposition.py +++ b/tests/openmetrics/test_exposition.py @@ -61,6 +61,7 @@ def test_summary(self): ss_count{a="c",b="d"} 1.0 ss_sum{a="c",b="d"} 17.0 ss_created{a="c",b="d"} 123.456 +ss_delta{a="c",b="d"} 17.0 # EOF """, generate_latest(self.registry)) @@ -88,6 +89,7 @@ def test_histogram(self): hh_count 1.0 hh_sum 0.05 hh_created 123.456 +hh_delta 0.05 # EOF """, generate_latest(self.registry)) diff --git a/tests/test_exposition.py b/tests/test_exposition.py index 00f39b47..f3db768d 100644 --- a/tests/test_exposition.py +++ b/tests/test_exposition.py @@ -82,6 +82,7 @@ def test_summary(self): # TYPE ss summary ss_count{a="c",b="d"} 1.0 ss_sum{a="c",b="d"} 17.0 +ss_delta{a="c",b="d"} 17.0 # TYPE ss_created gauge ss_created{a="c",b="d"} 123.456 """, generate_latest(self.registry)) @@ -109,6 +110,7 @@ def test_histogram(self): hh_bucket{le="+Inf"} 1.0 hh_count 1.0 hh_sum 0.05 +hh_delta 0.05 # TYPE hh_created gauge hh_created 123.456 """, generate_latest(self.registry))