A follow-up to Counting OpenAI Tokens per Request in FastAPI .
I sent a document to evorag while the database was down.
OpenAI was called. The embedding was created. I paid for it.
Then the database write failed. The user got a 500.
My logs showed embedding.created . They did not show request.completed .
No status. No latency. No token count. The failed request was invisible.
That's the request I most needed to see.
Where it came from
In the last post, I built a logging middleware. This was its core:
metrics = start_metrics () started = time . perf_counter () response = await call_next ( request ) latency_ms = round (( time . perf_counter () - started ) * 1000 , 2 )
logger . info ( " request.completed " , ...)
Enter fullscreen mode
Exit fullscreen mode
I tested the happy path. I never tested a route that raises.
If the route raises, call_next raises. Every line below it is skipped. The log never runs.
So my request log had a blind spot. Successful requests were logged. Failed ones disappeared.…