claws / aioprometheus

A Prometheus Python client library for asyncio-based applications
174 stars 21 forks source link

Migrating from synchronous prometheus #86

Closed sshleifer closed 1 year ago

sshleifer commented 1 year ago

I'm migrating a web app from synchronous flask to asynchronous Quart + hypercorn.

We previously had the following line in our flask_app.py

app.wsgi_app = DispatcherMiddleware(app.wsgi_app, {"/metrics": make_wsgi_app()})

which let another service scoop up prometheus metrics by calling /metrics.

It seems like the correct alternative is from your docs

from quart import request

from aioprometheus import REGISTRY, Counter, render
@app.route("/metrics")
async def handle_metrics():
    content, http_headers = render(REGISTRY, request.headers.getlist("accept"))
    return content, http_headers

My second question is, do I need to migrate a bunch of metrics like below to aiprometheus.Summary? and if so what should labelnames=["processing_type"] be converted to? const_labels={'processing_type': 'processing_type'}?

from prometheus_client import Histogram, Summary
CONTEXT_TOKENS_CACHED = Summary(
    "context_tokens_cached",
    "Number of context tokens successfully retrieved from context",
    labelnames=["processing_type"],
)
CONTEXT_CACHED = Summary(
    "context_cached",
    "Whether context hit any cache",
    labelnames=["processing_type"],
)
claws commented 1 year ago

If I understand correctly you want to expose Prometheus metrics from your Quart application? I'd recommend using the ASGI middleware approach as I think that simplifies it a lot. Take a look at the quart middleware example.

As for the second question, you'd need to declare the metrics using the aioprometheus API. In your examples I think this means you would replace labelnames with const_labels which aioprometheus uses for labels that should always be included with all instances of the metric. If you don't explicitly specify a registry then the metric is automatically added to the default registry.