Skip to content
8 changes: 5 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
build
dist
*.egg-info
*.pyc
*.swp
.coverage.*
.*cache
.coverage
.coverage.*
.tox
build
dist
htmlcov
63 changes: 63 additions & 0 deletions prometheus_client/context_managers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
from timeit import default_timer

from .decorator import decorate


class _ExceptionCounter(object):
def __init__(self, counter, exception):
self._counter = counter
self._exception = exception

def __enter__(self):
pass

def __exit__(self, typ, value, traceback):
if isinstance(value, self._exception):
self._counter.inc()

def __call__(self, f):
def wrapped(func, *args, **kwargs):
with self:
return func(*args, **kwargs)
return decorate(f, wrapped)


class _InprogressTracker(object):
def __init__(self, gauge):
self._gauge = gauge

def __enter__(self):
self._gauge.inc()

def __exit__(self, typ, value, traceback):
self._gauge.dec()

def __call__(self, f):
def wrapped(func, *args, **kwargs):
with self:
return func(*args, **kwargs)
return decorate(f, wrapped)


class _Timer(object):
def __init__(self, callback):
self._callback = callback

def _new_timer(self):
return self.__class__(self._callback)

def __enter__(self):
self._start = default_timer()

def __exit__(self, typ, value, traceback):
# Time can go backwards.
duration = max(default_timer() - self._start, 0)
self._callback(duration)

def __call__(self, f):
def wrapped(func, *args, **kwargs):
# Obtaining new instance of timer every time
# ensures thread safety and reentrancy.
with self._new_timer():
return func(*args, **kwargs)
return decorate(f, wrapped)
Loading