-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmanagers.py
55 lines (37 loc) · 1.23 KB
/
managers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
pyprometheus.values
~~~~~~~~~~~~~~~~~~~
Prometheus instrumentation library for Python applications
:copyright: (c) 2017 by Alexandr Lispython.
:license: , see LICENSE for more details.
:github: http://github.com/Lispython/pyprometheus
"""
import time
from functools import wraps
default_timer = time.time
class BaseManager(object):
def __call__(self, f):
@wraps(f)
def wrapper(*args, **kwargs):
with self:
return f(*args, **kwargs)
return wrapper
class TimerManager(BaseManager):
def __init__(self, collector):
self._collector = collector
def __enter__(self):
self._start_time = default_timer()
def __exit__(self, exc_type, exc_value, traceback):
self._collector.observe(default_timer() - self._start_time)
class InprogressTrackerManager(BaseManager):
def __init__(self, gauge):
self._gauge = gauge
def __enter__(self):
self._gauge.inc()
def __exit__(self, exc_info, exc_value, traceback):
self._gauge.dec()
class GaugeTimerManager(TimerManager):
def __exit__(self, exc_type, exc_value, traceback):
self._collector.set(default_timer() - self._start_time)