aioprometheus.decorators module

This module provides some convenience decorators for metrics

aioprometheus.decorators.count_exceptions(metric: aioprometheus.collectors.Counter, labels: Optional[Dict[str, str]] = None) Callable[[...], Any]

This decorator wraps a callable with code to count how many times the callable generates an exception.

Parameters
  • metric – a metric to increment when an exception is caught. The metric object must be a Counter metric object.

  • labels – a dict of extra labels to associate with the metric.

Returns

a callable wrapping the decorated function. The callable will be awaitable if the wrapped function was a coroutine function.

aioprometheus.decorators.inprogress(metric: aioprometheus.collectors.Gauge, labels: Optional[Dict[str, str]] = None) Callable[[...], Any]

This decorator wraps a callables with code to track whether it is currently in progress. The metric is incremented before calling the callable and is decremented when the callable is complete.

Parameters
  • metric – a metric to increment and decrement. The metric object must be a Gauge metric object.

  • labels – a dict of extra labels to associate with the metric.

Returns

a callable wrapping the decorated function. The callable will be awaitable if the wrapped function was a coroutine function.

aioprometheus.decorators.timer(metric: aioprometheus.collectors.Summary, labels: Optional[Dict[str, str]] = None) Callable[[...], Any]

This decorator wraps a callable with code to calculate how long the callable takes to execute and updates the metric with the duration.

Parameters
  • metric – a metric to update with the calculated function duration. The metric object must be a Summary metric object.

  • labels – a dict of extra labels to associate with the metric.

Returns

a callable wrapping the decorated function. The callable will be awaitable if the wrapped function was a coroutine function.