Skip to content
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

Fix spans for streaming responses in WSGI based frameworks #3798

Merged
merged 54 commits into from
Nov 25, 2024

Conversation

antonpirker
Copy link
Member

@antonpirker antonpirker commented Nov 18, 2024

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

Copy link

codecov bot commented Nov 18, 2024

Codecov Report

Attention: Patch coverage is 90.62500% with 6 lines in your changes missing coverage. Please review.

Project coverage is 79.94%. Comparing base (a7c2d70) to head (8574c06).
Report is 1 commits behind head on master.

✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
sentry_sdk/integrations/wsgi.py 92.59% 4 Missing ⚠️
sentry_sdk/tracing_utils.py 80.00% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #3798      +/-   ##
==========================================
+ Coverage   79.89%   79.94%   +0.05%     
==========================================
  Files         137      137              
  Lines       15385    15421      +36     
  Branches     2611     2614       +3     
==========================================
+ Hits        12292    12329      +37     
- Misses       2222     2223       +1     
+ Partials      871      869       -2     
Files with missing lines Coverage Δ
sentry_sdk/tracing_utils.py 83.92% <80.00%> (-0.11%) ⬇️
sentry_sdk/integrations/wsgi.py 85.96% <92.59%> (+1.13%) ⬆️

... and 5 files with indirect coverage changes

@antonpirker antonpirker changed the title Fix spans for streaming responses Fix spans for streaming responses in WSGI based frameworks Nov 20, 2024
@antonpirker antonpirker self-assigned this Nov 21, 2024
@antonpirker antonpirker force-pushed the antonpirker/fix-streaming-response branch from 32c40ed to c612326 Compare November 21, 2024 14:09
@antonpirker antonpirker force-pushed the antonpirker/fix-streaming-response branch from 15eb914 to 30d18fa Compare November 22, 2024 11:41
@antonpirker antonpirker marked this pull request as ready for review November 22, 2024 12:19
Copy link
Contributor

@sentrivana sentrivana left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left a couple of comments. As long as the tests for the various integrations that use the WSGI middleware are happy, I think this looks good.

sentry_sdk/integrations/wsgi.py Outdated Show resolved Hide resolved
sentry_sdk/tracing_utils.py Outdated Show resolved Hide resolved
sentry_sdk/integrations/wsgi.py Show resolved Hide resolved
@antonpirker antonpirker merged commit da20623 into master Nov 25, 2024
136 checks passed
@antonpirker antonpirker deleted the antonpirker/fix-streaming-response branch November 25, 2024 09:04
antonpirker added a commit that referenced this pull request Dec 2, 2024
antonpirker added a commit that referenced this pull request Dec 2, 2024
…3798)" (#3836)

This reverts commit da20623. (PR #3798)

Having a timer thread on each request is too much overhead on high volume servers.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Django StreamingHttpResponse View not working as expected
2 participants