Exporting prometheus metrics of sync vs async python apps

Viewed 318

I have a minimal async python server based on aiohttp.

It is very straightforward, just a websocket endpoint exposed as in

@routes.get('/my_endpoint')
async def my_func(request):
    ws = web.WebSocketResponse()
    await ws.prepare(request)
    return ws

I want to expose as prometheus metrics the request rate (and potentially the error rate).

After performing a brief investigation on the topic, I realised that it seems like there is a distinction between approaching prometheus metrics exposure when it comes to sync vs async apps.

For my case, where I want a simple request count/rate, is there a reason not to just use the plain' old prometheus python client (e.g by simply decorating my_func?)

Would the request count actually fail in such a case?

1 Answers

The following is based on my understanding on asyncio and the way the official prometheus client describes how it exposes metrics. aiohttp is to be used on top of asyncio. Now, asyncio is running something called an "event loop" which runs inside a single thread (usually the main thread)

You can look at it as an entity that decides to suspend or execute functions that were assigned to run in the loop. In your case my_func. For prometheus_client to expose your metrics you will probably need to run it in a different thread

Metrics are usually exposed over HTTP, to be read by the Prometheus server. The easiest way to do this is via start_http_server, which will start a HTTP server in a daemon thread on the given port

This is outside "the control of the event loop" which might lead to performance issues and to unexpected behavior as a result. So the request count might not fail, but if for some reason its doing some blocking task (I/O) it will block the main thread as well. If you'd use the async approach and run it as part of the event loop your blocking task can be awaited and give back the control to the main thread. There are open source projects that support prometheus in async functions such as aioprometheus and prometheus-async.

Related