How to log operation id and status for each route?
See original GitHub issueFirst Check
- I added a very descriptive title to this issue.
- I used the GitHub search to find a similar issue and didn’t find it.
- I searched the FastAPI documentation, with the integrated search.
- I already searched in Google “How to X in FastAPI” and didn’t find any information.
- I already read and followed all the tutorial in the docs and didn’t find an answer.
- I already checked if it is not related to FastAPI but to Pydantic.
- I already checked if it is not related to FastAPI but to Swagger UI.
- I already checked if it is not related to FastAPI but to ReDoc.
Commit to Help
- I commit to help with one of those options 👆
Example Code
@app.middleware("http")
async def log(request, call_next):
response = call_next(request)
if response.status_code >= 300:
return response
operation_id = get_operation_id_somehow() # is this possible?
print("LOG", operation_id, get_access_token(request))
return response
Description
I want to log the Operation ID, as well as the access token (from the request) for all requests that did not error, is this at all possible right now?
Given that I want to do this for all endpoints, using a middleware seems reasonable, but they have no way of accessing the route information (including the operation id). I can get the other bits of information from the requests/response no problem.
I also considered using a dependency, but even dependencies seem unable to have access to the operation id.
Operating System
macOS
Operating System Details
No response
FastAPI Version
0.65.2
Python Version
Python 3.9.1
Additional Context
No response
Issue Analytics
- State:
- Created 2 years ago
- Comments:8 (1 by maintainers)
Top Results From Across the Web
Operations management - Azure Data Explorer | Microsoft Learn
Up to one record indicates the terminal state of Completed or Failed . This mode is used when the command doesn't indicate the...
Read more >How to get the Operation ID in a HTTPTrigger Azure Function ...
You can use Activity.Current.RootId to get Operation Id inside a HTTP Trigger in portal. Code: #r "Newtonsoft.Json" using System.
Read more >Path Operation Advanced Configuration - FastAPI
You can set the OpenAPI operationId to be used in your path operation with the parameter operation_id . You would have to make...
Read more >Paths and Operations - Swagger
All paths are relative to the API server URL. The full request URL is constructed as <server-url>/path . Global servers can also be...
Read more >Command Line Interface — Schemathesis 3.17.5 documentation
By default, Schemathesis runs tests for all operations, ... --endpoint / -E . Operation path; ... The response status is not defined in...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I’m currently working on the same situation… this seems to work so far:
However, you need to inject this class into the router before adding routes to it because the (internal?)
add_route
method uses theroute_class
constructor to create the routes, hence it cannot be overriden afterwards…An example on how to do this:
I’ve ended up using the solution suggested in https://github.com/tiangolo/fastapi/issues/3877#issuecomment-916162837