Skip to content
This repository has been archived by the owner on Oct 3, 2018. It is now read-only.

Bogdanp/apistar_prometheus

Repository files navigation

apistar_prometheus

Build Status Test Coverage Maintainability PyPI version

Prometheus metrics for API Star apps.

Requirements

Installation

Use pipenv (or plain pip) to install the package:

pipenv install apistar_prometheus

To hook it into your app, add the Prometheus component to your app's components list, add a route for the expose_metrics handler and add before_request and after_request as the first and last hooks, respectively.

from apistar import App, Route
from apistar_prometheus import PrometheusComponent, PrometheusHooks, expose_metrics

routes = [
    # ...
    Route("/metrics", method="GET", handler=expose_metrics),
]

components = [
    PrometheusComponent(),
]

event_hooks = [
    PrometheusHooks(),
]

app = App(
    routes=routes,
    components=components,
    event_hooks=event_hooks,
)

Multiprocess mode

The default configuration assumes a single multi-threaded worker process. To expose metrics in a multiprocess configuration (eg. using gunicorn), set the prometheus_multiprocess_dir environment variable and use the expose_metrics_multiprocess handler.

from apistar_prometheus import PrometheusComponent, PrometheusHooks, expose_metrics_multiprocess

routes = [
    # ...,
    Route("/metrics", method="GET", handler=expose_metrics_multiprocess),
]

License

apistar_prometheus is licensed under Apache 2.0. Please see LICENSE for licensing details.