apistar_prometheus
Prometheus metrics for API Star apps.
Requirements
- API Star 0.4+
- prometheus_client 0.0.20+
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.