From 329b965c90cc3d962d016ea61cd29bef3c09cd73 Mon Sep 17 00:00:00 2001 From: Paul Melnikov Date: Tue, 6 Feb 2024 10:33:24 +0000 Subject: [PATCH 1/4] Add .reset method to Counter metric Signed-off-by: Paul Melnikov --- prometheus_client/metrics.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/prometheus_client/metrics.py b/prometheus_client/metrics.py index 34305a17..376983c8 100644 --- a/prometheus_client/metrics.py +++ b/prometheus_client/metrics.py @@ -310,6 +310,12 @@ def inc(self, amount: float = 1, exemplar: Optional[Dict[str, str]] = None) -> N _validate_exemplar(exemplar) self._value.set_exemplar(Exemplar(exemplar, amount, time.time())) + def reset(self) -> None: + """Reset the metric to zero.""" + self._value.set(0) + if _use_created: + self._created = time.time() + def count_exceptions(self, exception: Union[Type[BaseException], Tuple[Type[BaseException], ...]] = Exception) -> ExceptionCounter: """Count exceptions in a block of code or function. From a2d823290397345adfebe293eab44c457e192fe7 Mon Sep 17 00:00:00 2001 From: Paul Melnikov Date: Tue, 6 Feb 2024 10:39:02 +0000 Subject: [PATCH 2/4] Update docstrings Signed-off-by: Paul Melnikov --- prometheus_client/metrics.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/prometheus_client/metrics.py b/prometheus_client/metrics.py index 376983c8..580406e6 100644 --- a/prometheus_client/metrics.py +++ b/prometheus_client/metrics.py @@ -292,6 +292,12 @@ def f(): # Count only one type of exception with c.count_exceptions(ValueError): pass + + You can also reset the counter to zero in case your logical "process" restarts + without restarting the actual python process. + + c.reset() + """ _type = 'counter' @@ -313,8 +319,7 @@ def inc(self, amount: float = 1, exemplar: Optional[Dict[str, str]] = None) -> N def reset(self) -> None: """Reset the metric to zero.""" self._value.set(0) - if _use_created: - self._created = time.time() + self._created = time.time() def count_exceptions(self, exception: Union[Type[BaseException], Tuple[Type[BaseException], ...]] = Exception) -> ExceptionCounter: """Count exceptions in a block of code or function. From 2459cb9848cdeeafd499e6adf34ad6745b235e26 Mon Sep 17 00:00:00 2001 From: Paul Melnikov Date: Tue, 13 Feb 2024 04:53:18 +0700 Subject: [PATCH 3/4] Update method docstring Co-authored-by: Chris Marchbanks Signed-off-by: Paul Melnikov --- prometheus_client/metrics.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/prometheus_client/metrics.py b/prometheus_client/metrics.py index 580406e6..91cd9ecf 100644 --- a/prometheus_client/metrics.py +++ b/prometheus_client/metrics.py @@ -317,7 +317,7 @@ def inc(self, amount: float = 1, exemplar: Optional[Dict[str, str]] = None) -> N self._value.set_exemplar(Exemplar(exemplar, amount, time.time())) def reset(self) -> None: - """Reset the metric to zero.""" + """Reset the counter to zero. Use this when a logical process restarts without restarting the actual python process.""" self._value.set(0) self._created = time.time() From 2114b1b4c334f31642b53fa54693d4cab285c2ef Mon Sep 17 00:00:00 2001 From: Paul Melnikov Date: Tue, 13 Feb 2024 16:17:24 +0000 Subject: [PATCH 4/4] Add a test for .reset() method Signed-off-by: Paul Melnikov --- tests/test_core.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/test_core.py b/tests/test_core.py index 6f7c9d1c..30f9e0ad 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -43,6 +43,16 @@ def test_increment(self): self.counter.inc(7) self.assertEqual(8, self.registry.get_sample_value('c_total')) + def test_reset(self): + self.counter.inc() + self.assertNotEqual(0, self.registry.get_sample_value('c_total')) + created = self.registry.get_sample_value('c_created') + time.sleep(0.05) + self.counter.reset() + self.assertEqual(0, self.registry.get_sample_value('c_total')) + created_after_reset = self.registry.get_sample_value('c_created') + self.assertLess(created, created_after_reset) + def test_repr(self): self.assertEqual(repr(self.counter), "prometheus_client.metrics.Counter(c)")