|
| 1 | +""" |
| 2 | +
|
| 3 | + UNDER DEVELOPMENT for issue #784 |
| 4 | +
|
| 5 | + Based on https://github.com/amitsaha/aiohttp-prometheus |
| 6 | +
|
| 7 | + Clients: |
| 8 | + - https://github.com/prometheus/client_python |
| 9 | + - TODO: see https://github.com/claws/aioprometheus |
| 10 | +""" |
| 11 | + |
| 12 | +import prometheus_client |
| 13 | +from aiohttp import web |
| 14 | +from prometheus_client import Counter, Gauge, Histogram, CONTENT_TYPE_LATEST |
| 15 | +import time |
| 16 | + |
| 17 | + |
| 18 | +# https://prometheus.io/docs/concepts/metric_types/#counter |
| 19 | + |
| 20 | +def middleware_factory(app_name): |
| 21 | + @web.middleware |
| 22 | + async def middleware_handler(request, handler): |
| 23 | + try: |
| 24 | + request['start_time'] = time.time() |
| 25 | + request.app['REQUEST_IN_PROGRESS'].labels( |
| 26 | + app_name, request.path, request.method).inc() |
| 27 | + |
| 28 | + resp = await handler(request) |
| 29 | + |
| 30 | + except web.HTTPException as ee: |
| 31 | + # Captures raised reponses (success/failures accounted with resp.status) |
| 32 | + resp = ee |
| 33 | + raise |
| 34 | + finally: |
| 35 | + # metrics on the same request |
| 36 | + resp_time = time.time() - request['start_time'] |
| 37 | + request.app['REQUEST_LATENCY'].labels( |
| 38 | + app_name, request.path).observe(resp_time) |
| 39 | + |
| 40 | + request.app['REQUEST_IN_PROGRESS'].labels( |
| 41 | + app_name, request.path, request.method).dec() |
| 42 | + |
| 43 | + request.app['REQUEST_COUNT'].labels( |
| 44 | + app_name, request.method, request.path, resp.status).inc() |
| 45 | + |
| 46 | + return resp |
| 47 | + return middleware_handler |
| 48 | + |
| 49 | +async def metrics(_request): |
| 50 | + # TODO: NOT async! |
| 51 | + # prometheus_client access to a singleton registry! |
| 52 | + resp = web.Response(body=prometheus_client.generate_latest()) |
| 53 | + resp.content_type = CONTENT_TYPE_LATEST |
| 54 | + return resp |
| 55 | + |
| 56 | + |
| 57 | +def setup_monitoring(app, app_name): |
| 58 | + |
| 59 | + # NOTE: prometheus_client registers metrics in globals |
| 60 | + # tests might fail when fixtures get re-created |
| 61 | + |
| 62 | + # Total number of requests processed |
| 63 | + app['REQUEST_COUNT'] = Counter( |
| 64 | + 'http_requests_total', 'Total Request Count', |
| 65 | + ['app_name', 'method', 'endpoint', 'http_status'] |
| 66 | + ) |
| 67 | + |
| 68 | + # Latency of a request in seconds |
| 69 | + app['REQUEST_LATENCY'] = Histogram( |
| 70 | + 'http_request_latency_seconds', 'Request latency', |
| 71 | + ['app_name', 'endpoint'] |
| 72 | + ) |
| 73 | + |
| 74 | + # Number of requests in progress |
| 75 | + app['REQUEST_IN_PROGRESS']=Gauge( |
| 76 | + 'http_requests_in_progress_total', 'Requests in progress', |
| 77 | + ['app_name', 'endpoint', 'method'] |
| 78 | + ) |
| 79 | + |
| 80 | + app.middlewares.insert(0, middleware_factory(app_name)) |
| 81 | + |
| 82 | + # FIXME: this in the front-end has to be protected! |
| 83 | + app.router.add_get("/metrics", metrics) |
0 commit comments