Custom ASGI worker metrics with OpenTelemetry
Is it possible to create custom open telemetry metrics to calculate the average time a request stays queued in ASGI before execution and also the average queue size per worker? Or if it is not possible by worker just in general in the ASGI server. I tried searching on the internet but I didn't found any example of this use case. ChatGPT suugested me to create a middleware and do it like this:
import time
from starlette.middleware.base import BaseHTTPMiddleware
from opentelemetry import metrics
# Create a Meter
meter = metrics.get_meter_provider().get_meter("asgi.queue")
# Define a histogram for queue time
queue_time_histogram = meter.create_histogram(
name="asgi.queue_time",
description="Time requests spend in the ASGI queue",
unit="ms",
)
class QueueTimeMiddleware(BaseHTTPMiddleware):
async def dispatch(self, request, call_next):
request.state.queue_start_time = time.perf_counter() # Request received
response = await call_next(request)
queue_time = time.perf_counter() - request.state.queue_start_time
queue_time_histogram.record(queue_time * 1000) # Convert to milliseconds
return response
However I am little suspicious about this snippet and don't know if this is exactly what we want. Does anyone have any advice on this case?