Open mehdihasan opened 7 months ago
aioprometheus
seems to assume the app
that is stored in the state is a starlette
app, while this is not the case in Connexion.
CC: @claws
I think you can work around this by setting use_template_urls
to False though.
app = Connexion.App(__name__)
app.add_middleware(MetricsMiddleware, use_template_urls=False)
Thanks @RobbeSneyders for your reply. Your suggestion worked - the app is running but metrics is not working yet. I am getting some other errors regarding app/object state.
BTW, what metrics solution do you recommened to integrate with Connexion AsyncApp?
@mehdihasan the app/object state
error is coming from: https://github.com/claws/aioprometheus/blob/master/src/aioprometheus/asgi/starlette.py#L16
I got it working with something like this:
application = connexion.AsyncApp(__name__, specification_dir="../openapi/")
from starlette.requests import Request
from starlette.responses import Response
from aioprometheus import REGISTRY, render
async def metrics(request: Request) -> Response:
"""Render metrics into format specified by 'accept' header.
This function first attempts to retrieve the metrics Registry from
``request.app.state`` in case the app is using a custom registry instead
of the default registry. If this fails then the default registry is used.
"""
registry = (
request.app.state.registry
if hasattr(request, "app") and hasattr(request.app, "state") and hasattr(request.app.state, "registry")
else REGISTRY
)
content, http_headers = render(registry, request.headers.getlist("Accept"))
return Response(content=content, media_type=http_headers["Content-Type"])
from aioprometheus import Counter, MetricsMiddleware
application.add_middleware(MetricsMiddleware, use_template_urls=False, exclude_paths=())
application.add_url_rule("/metrics", "metrics", metrics)
Description
While upgrading connexion from 2.x to 3.x, we found the previous ConnexionPrometheusMetrics from prometheus_flask_exporter is not working. I have not found any way to integrate prometheus_flask_exporter with the new AsyncApp. After some search, we found aioprometheus probably the library to use. Now, we want to integrate aioprometheus to our application.
Expected behaviour
Publishing application metrics from /metrics endpoint.
Actual behaviour
500 Internal Server Error
Steps to reproduce
We have tried to add aioprometheus MetricsMiddleware in our app as following,
However it is failing with the following message:
Additional info:
Output of the commands:
python --version
Python 3.10.0pip show connexion | grep "^Version\:"
Version: 3.0.5