8000 Refactor everything out from `core.py` (but keep it for re-exports) · tyleracorn/client_python@b4d6020 · GitHub
[go: up one dir, main page]

Skip to content

Commit b4d6020

Browse files
akxbrian-brazil
authored andcommitted
Refactor everything out from core.py (but keep it for re-exports)
Signed-off-by: Aarni Koskela <akx@iki.fi>
1 parent 1d0168b commit b4d6020

20 files changed

+1479
-1402
lines changed

prometheus_client/__init__.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,21 @@
55
from . import gc_collector
66
from . import platform_collector
77
from . import process_collector
8+
from . import registry
9+
from . import metrics_core
10+
from . import metrics
811

912
__all__ = ['Counter', 'Gauge', 'Summary', 'Histogram', 'Info', 'Enum']
1013

11-
CollectorRegistry = core.CollectorRegistry
12-
REGISTRY = core.REGISTRY
13-
Metric = core.Metric
14-
Counter = core.Counter
15-
Gauge = core.Gauge
16-
Summary = core.Summary
17-
Histogram = core.Histogram
18-
Info = core.Info
19-
Enum = core.Enum
14+
CollectorRegistry = registry.CollectorRegistry
15+
REGISTRY = registry.REGISTRY
16+
Metric = metrics_core.Metric
17+
Counter = metrics.Counter
18+
Gauge = metrics.Gauge
19+
Summary = metrics.Summary
20+
Histogram = metrics.Histogram
21+
Info = metrics.Info
22+
Enum = metrics.Enum
2023

2124
CONTENT_TYPE_LATEST = exposition.CONTENT_TYPE_LATEST
2225
generate_latest = exposition.generate_latest

prometheus_client/bridge/graphite.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@
88
import time
99
from timeit import default_timer
1010

11-
from .. import core
11+
from ..registry import REGISTRY
1212

1313
# Roughly, have to keep to what works as a file name.
1414
# We also remove periods, so labels can be distinguished.
15+
1516
_INVALID_GRAPHITE_CHARS = re.compile(r"[^a-zA-Z0-9_-]")
1617

1718

@@ -45,7 +46,7 @@ def run(self):
4546

4647

4748
class GraphiteBridge(object):
48-
def __init__(self, address, registry=core.REGISTRY, timeout_seconds=30, _timer=time.time):
49+
def __init__(self, address, registry=REGISTRY, timeout_seconds=30, _timer=time.time):
4950
self._address = address
5051
self._registry = registry
5152
self._timeout = timeout_seconds

prometheus_client/context_managers.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
from __future__ import unicode_literals
2+
3+
from timeit import default_timer
4+
5+
from .decorator import decorate
6+
7+
8+
class ExceptionCounter(object):
9+
def __init__(self, counter, exception):
10+
self._counter = counter
11+
self._exception = exception
12+
13+
def __enter__(self):
14+
pass
15+
16+
def __exit__(self, typ, value, traceback):
17+
if isinstance(value, self._exception):
18+
self._counter.inc()
19+
20+
def __call__(self, f):
21+
def wrapped(func, *args, **kwargs):
22+
with self:
23+
return func(*args, **kwargs)
24+
25+
return decorate(f, wrapped)
26+
27+
28+
class InprogressTracker(object):
29+
def __init__(self, gauge):
30+
self._gauge = gauge
31+
32+
def __enter__(self):
33+
self._gauge.inc()
34+
35+
def __exit__(self, typ, value, traceback):
36+
self._gauge.dec()
37+
38+
def __call__(self, f):
39+
def wrapped(func, *args, **kwargs):
40+
with self:
41+
return func(*args, **kwargs)
42+
43+
return decorate(f, wrapped)
44+
45+
46+
class Timer(object):
47+
def __init__(self, callback):
48+
self._callback = callback
49+
50+
def _new_timer(self):
51+
return self.__class__(self._callback)
52+
53+
def __enter__(self):
54+
self._start = default_timer()
55+
56+
def __exit__(self, typ, value, traceback):
57+
# Time can go backwards.
58+
duration = max(default_timer() - self._start, 0)
59+
self._callback(duration)
60+
61+
def __call__(self, f):
62+
def wrapped(func, *args, **kwargs):
63+
# Obtaining new instance of timer every time
64+
# ensures thread safety and reentrancy.
65+
with self._new_timer():
66+
return func(*args, **kwargs)
67+
68+
return decorate(f, wrapped)

0 commit comments

Comments
 (0)
0