|
|
|
@ -11,6 +11,10 @@ from monotonic import monotonic
|
|
|
|
|
import prometheus_client as prom
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# need to keep global track of what metrics we've registered
|
|
|
|
|
# because we're not allowed to re-register
|
|
|
|
|
metrics = {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def timed(name=None,
|
|
|
|
|
buckets=[10.**x for x in range(-9, 5)], normalized_buckets=None,
|
|
|
|
@ -25,6 +29,7 @@ def timed(name=None,
|
|
|
|
|
if you're using gevent and the wrapped function blocks) and do not include subprocesses.
|
|
|
|
|
|
|
|
|
|
NAME defaults to the wrapped function's name.
|
|
|
|
|
NAME must be unique OR have the exact same labels as other timed() calls with that name.
|
|
|
|
|
|
|
|
|
|
Any labels passed in are included. Given label values may be callable, in which case
|
|
|
|
|
they are passed the input and result from the wrapped function and should return a label value.
|
|
|
|
@ -86,6 +91,9 @@ def timed(name=None,
|
|
|
|
|
# can't safely assign to name inside closure, we use a new _name variable instead
|
|
|
|
|
_name = fn.__name__ if name is None else name
|
|
|
|
|
|
|
|
|
|
if name in metrics:
|
|
|
|
|
latency, cputime = metrics[name]
|
|
|
|
|
else:
|
|
|
|
|
latency = prom.Histogram(
|
|
|
|
|
"{}_latency".format(_name),
|
|
|
|
|
"Wall clock time taken to execute {}".format(_name),
|
|
|
|
@ -98,7 +106,12 @@ def timed(name=None,
|
|
|
|
|
labels.keys() + ['error', 'type'],
|
|
|
|
|
buckets=buckets,
|
|
|
|
|
)
|
|
|
|
|
metrics[name] = latency, cputime
|
|
|
|
|
if normalize:
|
|
|
|
|
normname = '{} normalized'.format(name)
|
|
|
|
|
if normname in metrics:
|
|
|
|
|
normal_latency, normal_cputime = metrics[normname]
|
|
|
|
|
else:
|
|
|
|
|
normal_latency = prom.Histogram(
|
|
|
|
|
"{}_latency_normalized".format(_name),
|
|
|
|
|
"Wall clock time taken to execute {} per unit of work".format(_name),
|
|
|
|
@ -111,6 +124,7 @@ def timed(name=None,
|
|
|
|
|
labels.keys() + ['error', 'type'],
|
|
|
|
|
buckets=normalized_buckets,
|
|
|
|
|
)
|
|
|
|
|
metrics[normname] = normal_latency, normal_cputime
|
|
|
|
|
|
|
|
|
|
@functools.wraps(fn)
|
|
|
|
|
def wrapper(*args, **kwargs):
|
|
|
|
|