-
Notifications
You must be signed in to change notification settings - Fork 516
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Django StreamingHttpResponse View not working as expected #3736
Comments
Do we have a way to set the trace_id? But it seems that we don't have a set_trace_id thing at transaction/event level, right? I know that we can do a Span(trace_id=myid), but passing this to ALL calls downstream is bad. 😅 |
Hey @marcoaleixo thanks for raising this, I will have a look! |
Indeed! I have now reproduced this with this demo application: The first span |
This is a bug. I guess it has something to do with how Django handles streaming views and what we monkey patch in Django. Sentry is probably not patching the functions in Django that are handling the streaming responses. |
hey @antonpirker - thanks for taking a look but I have a quick question: how do you prioritize those fixes? Just to understand if I will need to use another observability tool until you fix this. |
Hey! I will have some time today and tomorrow to look at this. I will keep you posted on the progress |
Small update: The problem is that |
Fixes spans in streaming responses when using WSGI based frameworks. Only close the transaction once the response was consumed. This way all the spans created during creation of the response will be recorded with the transaction: - The transaction stays open until all the streaming blocks are sent to the client. (because of this I had to update the tests, to make sure the tests, consume the response, because the Werkzeug test client (used by Flask and Django and our Strawberry tests) will not close the WSGI response) - A maximum runtime of 5 minutes for transactions is enforced. (like Javascript does it) - When using a generator to generate the streaming response, it uses the correct scopes to have correct parent-child relationship of spans created in the generator. People having Sentry in a streaming application will: - See an increase in their transaction duration to up to 5 minutes - Get the correct span tree for streaming responses generated by a generator Fixes #3736
Reopening since we ultimately reverted the change |
Is the solution of this bug complicated? Do you think that's too risky to deploy the reverted PR? is the performance the only thing that made you revert it? |
Hey @marcoaleixo, it's unfortunately more tricky than we realized and there likely won't be a proper fix until after the holidays. The PR was reverted for performance reasons, it's spawning an additional thread per request. It's hard to tell where the cutoff is when this starts being a problem since it depends on your setup etc. -- it could work fine for you. As long as you have the option to monitor it for some time I think you could go for it. |
Just sharing that we deployed the branch on Monday and no major impact on our latency/CPU usage. |
Awesome, thanks for sharing @marcoaleixo! As @sentrivana mentioned, we hope to continue working on a proper fix after the holidays. |
How do you use Sentry?
Sentry Saas (sentry.io)
Version
2.18.0
Steps to Reproduce
Here the span.trace_id will be for instance "1234"
In this function, the trace_id will be totally different.
I think that this is related the StreamingHttpResponse, because the Sentry is working fine for all my other views.
Feel free to ask more details to help in the investigation.
FROM python:3.9.18-slim-bullseye
Django==4.1
djangorestframework==3.14
I'm executing my server with python manage.py runserver 0.0.0.0:8000
In production we use something like gunicorn api.wsgi -b 0.0.0.0:8081
Expected Result
The trace_id should stay the same in StreamingHttpResponse views and underlying function calls.
Actual Result
The trace_id is different between the view and underlying function calls.
The text was updated successfully, but these errors were encountered: