/asgi-prometheus

Support Prometheus metrics for ASGI applications

Primary LanguagePythonMIT LicenseMIT

ASGI-Prometheus

asgi-prometheus -- Support Prometheus metrics for ASGI applications (Asyncio / Trio, / Curio)

Tests Status PYPI Version Python Versions
  • python >= 3.9

asgi-prometheus should be installed using pip:

pip install asgi-prometheus

Common ASGI applications:

from asgi_prometheus import PrometheusMiddleware


async def my_app(scope, receive, send):
    """Read session and get the current user data from it or from request query."""
    await send({"type": "http.response.start", "status": status, "headers": headers})
    await send({"type": "http.response.body", "body": b"Hello World!"})

app = PrometheusMiddleware(my_app, metrics_url="/metrics", group_paths=['/'])

# http GET / -> OK
# http GET /metrics -> [Prometheus metrics]

As ASGI-Tools Internal middleware

from asgi_tools import App
from asgi_prometheus import PrometheusMiddleware

app = App()
app.middleware(PrometheusMiddleware.setup(group_paths=['/views', '/api']))

@app.route('/')
async def index(request):
    return 'Hello World!'

# http GET / -> OK
# http GET /prometheus -> [Prometheus metrics (default URL)]
from asgi_sessions import PrometheusMiddleware

app = PrometheusMiddleware(

     # Your ASGI application
     app,

     # Metrics URL for Prometheus (set empty string to disable)
     metrics_url='/prometheus',

     # List of path's prefixes to group. A path which starts from the prefix will be grouped.
     # For example: group_paths=['/api/users'], "/api/users/1", "/api/users/2" will be grouped into "/api/users*"
     group_paths=[],

)

If you have any suggestions, bug reports or annoyances please report them to the issue tracker at https://github.com/klen/asgi-prometheus/issues

Development of the project happens at: https://github.com/klen/asgi-prometheus

Licensed under a MIT license.