From 5df1730ac9e286b12f082f33848e9dc1198e6260 Mon Sep 17 00:00:00 2001 From: George Mossessian Date: Fri, 6 Dec 2019 14:43:27 -0500 Subject: [PATCH 1/3] time can not go backwards if it is monotonic Signed-off-by: George Mossessian --- prometheus_client/context_managers.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/prometheus_client/context_managers.py b/prometheus_client/context_managers.py index 2b271a7c..25d8f73a 100644 --- a/prometheus_client/context_managers.py +++ b/prometheus_client/context_managers.py @@ -1,6 +1,6 @@ from __future__ import unicode_literals -from timeit import default_timer +from time import monotonic from .decorator import decorate @@ -51,11 +51,10 @@ def _new_timer(self): return self.__class__(self._callback) def __enter__(self): - self._start = default_timer() + self._start = monotonic() def __exit__(self, typ, value, traceback): - # Time can go backwards. - duration = max(default_timer() - self._start, 0) + duration = monotonic() - self._start self._callback(duration) def __call__(self, f): From 43fa724129c08ecba0f7411e1fcb293c4dc61d38 Mon Sep 17 00:00:00 2001 From: George Mossessian Date: Fri, 6 Dec 2019 14:51:36 -0500 Subject: [PATCH 2/3] use backport of monotonic for py<3.3 Signed-off-by: George Mossessian --- prometheus_client/context_managers.py | 2 +- tox.ini | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/prometheus_client/context_managers.py b/prometheus_client/context_managers.py index 25d8f73a..59076964 100644 --- a/prometheus_client/context_managers.py +++ b/prometheus_client/context_managers.py @@ -1,6 +1,6 @@ from __future__ import unicode_literals -from time import monotonic +from monotonic import monotonic from .decorator import decorate diff --git a/tox.ini b/tox.ini index a849cd95..ae764a2b 100644 --- a/tox.ini +++ b/tox.ini @@ -6,6 +6,7 @@ envlist = coverage-clean,py26,py27,py34,py35,py36,py37,py38,pypy,pypy3,{py27,py3 deps = coverage pytest + monotonic [testenv:py26] ; Last pytest and py version supported on py26 . From ebe1ebfdb9c3b8a795e5e628792e05be4daec3a4 Mon Sep 17 00:00:00 2001 From: George Mossessian Date: Fri, 6 Dec 2019 15:02:08 -0500 Subject: [PATCH 3/3] add monotonic to py2.6 deps Signed-off-by: George Mossessian --- tox.ini | 1 + 1 file changed, 1 insertion(+) diff --git a/tox.ini b/tox.ini index ae764a2b..88a5fe4b 100644 --- a/tox.ini +++ b/tox.ini @@ -16,6 +16,7 @@ deps = pytest==2.9.2 coverage futures + monotonic [testenv:py27] deps =