Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions prometheus_client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,21 @@
from . import gc_collector
from . import platform_collector
from . import process_collector
from . import registry
from . import metrics_core
from . import metrics

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

CollectorRegistry = core.CollectorRegistry
REGISTRY = core.REGISTRY
Metric = core.Metric
Counter = core.Counter
Gauge = core.Gauge
Summary = core.Summary
Histogram = core.Histogram
Info = core.Info
Enum = core.Enum
CollectorRegistry = registry.CollectorRegistry
REGISTRY = registry.REGISTRY
Metric = metrics_core.Metric
Counter = metrics.Counter
Gauge = metrics.Gauge
Summary = metrics.Summary
Histogram = metrics.Histogram
Info = metrics.Info
Enum = metrics.Enum

CONTENT_TYPE_LATEST = exposition.CONTENT_TYPE_LATEST
generate_latest = exposition.generate_latest
Expand Down
5 changes: 3 additions & 2 deletions prometheus_client/bridge/graphite.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
import time
from timeit import default_timer

from .. import core
from ..registry import REGISTRY

# Roughly, have to keep to what works as a file name.
# We also remove periods, so labels can be distinguished.

_INVALID_GRAPHITE_CHARS = re.compile(r"[^a-zA-Z0-9_-]")


Expand Down Expand Up @@ -45,7 +46,7 @@ def run(self):


class GraphiteBridge(object):
def __init__(self, address, registry=core.REGISTRY, timeout_seconds=30, _timer=time.time):
def __init__(self, address, registry=REGISTRY, timeout_seconds=30, _timer=time.time):
self._address = address
self._registry = registry
self._timeout = timeout_seconds
Expand Down
68 changes: 68 additions & 0 deletions prometheus_client/context_managers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
from __future__ import unicode_literals

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