[X] I had searched in the issues and found no similar issues.
Apache SkyWalking Component
Python Agent (apache/skywalking-python)
What happened
I want to return the skywalking trace id at the error handlers in FastAPI.
I print the trace id at the beginning of the request and in the error handler, and I got two different trace ids.(The first one is correct, which can be search in the skywalking UI)
For example, I got the log with trace id 8f954eae8f4011ee9a7682b792d8f7f3:
I try to add a http middleware to FastAPI and use request.state.traceid to save the trace id, but it doesn't work, I still got different trace ids.
Finally, I find a way to solve it partly: at every entry point and get the state at the error handler. The method works, but it's verbose, which means that I have add all my entry point function with same code: a request parameter and the state save process. So I want to know is there any other method to do it elegantly.
As async process, there is no black magic. You have to do manually, unless this async is inside the framework, the plugin system could work. But your cases are not.
Search before asking
Apache SkyWalking Component
Python Agent (apache/skywalking-python)
What happened
I want to return the skywalking trace id at the error handlers in FastAPI.
I print the trace id at the beginning of the request and in the error handler, and I got two different trace ids.(The first one is correct, which can be search in the skywalking UI)
For example, I got the log with trace id
8f954eae8f4011ee9a7682b792d8f7f3
:And the response of the request with trace id
8f98f8108f4011ee9a7682b792d8f7f3
:What you expected to happen
I expect the two trace ids should be same. By the way, I get trace id from context:
How to reproduce
Run the demo app service(
sw-python run uvicorn app:app --reload
) and call the demo api:Anything else
I try to add a
http
middleware to FastAPI and userequest.state.traceid
to save the trace id, but it doesn't work, I still got different trace ids.Finally, I find a way to solve it partly: at every entry point and get the state at the error handler. The method works, but it's verbose, which means that I have add all my entry point function with same code: a
request
parameter and the state save process. So I want to know is there any other method to do it elegantly.The environments(partly)
Are you willing to submit a pull request to fix on your own?
Code of Conduct