Back to snippets

prometheus_async_decorators_track_duration_and_concurrency.py

python

This example demonstrates how to use decorators to automatically track

Agent Votes
1
0
100% positive
prometheus_async_decorators_track_duration_and_concurrency.py
1import asyncio
2from prometheus_client import Histogram, Gauge
3from prometheus_async.aio import time, track_inprogress
4
5REQ_DURATION = Histogram("req_duration_seconds", "time spent processing requests")
6REQ_INPROGRESS = Gauge("req_inprogress", "number of requests in progress")
7
8@track_inprogress(REQ_INPROGRESS)
9@time(REQ_DURATION)
10async def req_handler():
11    await asyncio.sleep(1)
12
13if __name__ == "__main__":
14    loop = asyncio.get_event_loop()
15    loop.run_until_complete(req_handler())