Package: aioprometheus
aioprometheusis a Prometheus Python client library for asyncio-based
applications. It provides metrics collection and serving capabilities for
use with Prometheus and compatible monitoring systems. It supports exporting
metrics into text and pushing metrics to a gateway.The ASGI middleware inaioprometheuscan be used in FastAPI/Starlette and
Quart applications.aioprometheuscan also be used in other kinds of asyncio
applications too.The project documentation can be found onReadTheDocs.Install$pipinstallaioprometheusThe ASGI middleware does not have any external dependencies but the Starlette
and Quart convenience functions that handle metrics requests do.If you plan on using the ASGI middleware in a Starlette / FastAPI application
then you can install the extra dependencies alongsideaioprometheusby adding
extras to the install.$pipinstallaioprometheus[starlette]If you plan on using the ASGI middleware in a Quart application then you can
install the extra dependencies alongsideaioprometheusby adding extras
to the install.$pipinstallaioprometheus[quart]A Prometheus Push Gateway client and a HTTP service are included, but their
dependencies are not installed by default. You can install them alongsideaioprometheusby adding extras to the install.$pipinstallaioprometheus[aiohttp]Multiple optional dependencies can be listed at once, such as:$pipinstallaioprometheus[aiohttp,starlette,quart]UsageThere are two basic steps involved in using aioprometheus; the first is to
instrument your software by creating metrics to monitor events and the second
is to expose the metrics to a collector.Creating a new metric is easy. First, import the appropriate metric from
aioprometheus. In the example below it’s a Counter metric. Next, instantiate
the metric with a name and a help string. Finally, update the metric when an
event occurs. In this case the counter is incremented.fromaioprometheusimportCounterevents_counter=Counter("events_counter","Total number of events.",)events_counter.inc({"kind":"event A"})By default, metrics get registered into the default collector registry which
is available ataioprometheus.REGISTRY.A number of convenience decorator functions are included in aioprometheus that
can assist with automatically updating metrics. Theexamplesdirectory
contains various decorators examples.Once your software is instrumented with various metrics you’ll want to
expose them to Prometheus or a compatible metrics collector. There are
multiple strategies available for this and the right choice depends on the
kind of thing being instrumented.If you are instrumenting a Starlette, FastAPI or Quart application then the
easiest option for adding Prometheus metrics is to use the ASGI Middleware
provided byaioprometheus.The ASGI middleware provides a default set of metrics that include counters
for total requests received, total responses sent, exceptions raised and
response status codes for route handlers.The example below shows how to use the aioprometheus ASGI middleware in a
FastAPI application. FastAPI is built upon Starlette so using the middleware
in Starlette would be the same.fromfastapiimportFastAPI,Request,ResponsefromaioprometheusimportCounter,MetricsMiddlewarefromaioprometheus.asgi.starletteimportmetricsapp=FastAPI()# Any custom application metrics are automatically included in the exposed# metrics. It is a good idea to attach the metrics to 'app.state' so they# can easily be accessed in the route handler - as metrics are often# created in a different module than where they are used.app.state.users_events_counter=Counter("events","Number of events.")app.add_middleware(MetricsMiddleware)app.add_route("/metrics",metrics)@app.get("/")asyncdefroot(request:Request):returnResponse("FastAPI Middleware Example")@app.get("/users/{user_id}")asyncdefget_user(request:Request,user_id:str,):request.app.state.users_events_counter.inc({"path":request.scope["path"]})returnResponse(f"{user_id}")if__name__=="__main__":importuvicornuvicorn.run(app)Other examples in theexamples/frameworksdirectory show how aioprometheus
can be used within various web application frameworks.The next example shows how to use the Service HTTP endpoint to provide a
dedicated metrics endpoint for other applications such as long running
distributed system processes.The Service object requires optional extras to be installed so make sure you
install aioprometheus with the ‘aiohttp’ extras.$pipinstallaioprometheus[aiohttp]"""
This example demonstrates how the ``aioprometheus.Service`` can be used to
expose metrics on a HTTP endpoint.

.. code-block:: console

    (env) $ python simple-service-example.py
    Serving prometheus metrics on: http://127.0.0.1:8000/metrics

You can open the URL in a browser or use the ``curl`` command line tool to
fetch metrics manually to verify they can be retrieved by Prometheus server.

"""importasyncioimportsocketfromaioprometheusimportCounterfromaioprometheus.serviceimportServiceasyncdefmain():service=Service()events_counter=Counter("events","Number of events.",const_labels={"host":socket.gethostname()})awaitservice.start(addr="127.0.0.1",port=8000)print(f"Serving prometheus metrics on:{service.metrics_url}")# Now start another coroutine to periodically update a metric to# simulate the application making some progress.asyncdefupdater(c:Counter):whileTrue:c.inc({"kind":"timer_expiry"})awaitasyncio.sleep(1.0)awaitupdater(events_counter)# Finally stop serverawaitservice.stop()if__name__=="__main__":try:asyncio.run(main())exceptKeyboardInterrupt:passA counter metric is used to track the number of while loop iterations executed
by the ‘updater’ coroutine. The Service is started and then a coroutine is
started to periodically update the metric to simulate progress.The Service can be configured to bind to a user defined network interface and
port.When the Service receives a request for metrics it forms a response by
rendering the contents of its registry into the appropriate format. By default
the Service uses the default collector registry, which isaioprometheus.REGISTRY. The Service can be configured to use a different
registry by passing one in as an argument to the Service constructor.Licenseaioprometheusis released under the MIT license.aioprometheusoriginates from the (now deprecated)prometheus pythonpackage which
was released under the MIT license.aioprometheuscontinues to use the MIT
license and contains a copy of the original MIT license from theprometheus-pythonproject as instructed by the original license.