Skip to content

Django StreamingHttpResponse View not working as expected #3736

Closed

Description

@marcoaleixo

How do you use Sentry?

Sentry Saas (sentry.io)

Version

2.18.0

Steps to Reproduce

  1. Have a view like
class StreamingView(APIView):

    def post(self, request, *args, **kwargs):

        span = sentry_sdk.start_span(name=f"Log: {record.msg}", op="log.info", sampled=False)
        print(span)
        span.set_data("value", record.msg)
        span.set_data("filename", f"{record.pathname} at line {record.lineno}")
        span.finish()

        generator_streaming = dummy_function.execute(parameters=parameters)
        
        return StreamingHttpResponse(generator_streaming, content_type='text/event-stream')

Here the span.trace_id will be for instance "1234"

  1. Inside the dummy_function
def execute():
        span = sentry_sdk.start_span(name=f"Log: {record.msg}", op="log.info", sampled=False)
        print(span)
        span.set_data("value", record.msg)
        span.set_data("filename", f"{record.pathname} at line {record.lineno}")
        span.finish()

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.

Activity

  1. moved this to Waiting for: Product Owner in GitHub Issues with 👀 3on Nov 5, 2024
  2. changed the title [-]Django StreamingHttpResponse not working as expected[/-] [+]Django StreamingHttpResponse View not working as expected[/+] on Nov 5, 2024
  3. marcoaleixo commented on Nov 5, 2024

    @marcoaleixo
    Author

    Do we have a way to set the trace_id?
    Maybe an easy fix is to get the trace_id in the view level and pass as parameter to my function and do a transaction.set_trace_id(trace_id=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. 😅

  4. antonpirker commented on Nov 7, 2024

    @antonpirker
    Contributor

    Hey @marcoaleixo thanks for raising this, I will have a look!

  5. moved this from Waiting for: Product Owner to No status in GitHub Issues with 👀 3on Nov 7, 2024
  6. antonpirker commented on Nov 7, 2024

    @antonpirker
    Contributor

    Indeed!

    I have now reproduced this with this demo application:
    https://github.com/antonpirker/testing-sentry/blob/main/test-django-rest-framework-streaming/mysite/api/views.py

    The first span "Streaming View" is present in the trace, but the second span "Stream Response Generator" is missing (because it has the wrong trace id)
    Image

  7. antonpirker commented on Nov 7, 2024

    @antonpirker
    Contributor

    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.

  8. marcoaleixo commented on Nov 12, 2024

    @marcoaleixo
    Author

    hey @antonpirker - thanks for taking a look but I have a quick question: how do you prioritize those fixes?
    This bug is creating a hole in the observability of one of my main endpoints :/

    Just to understand if I will need to use another observability tool until you fix this.
    I do have some bandwidth to work on this fix but not sure if I have enough context to work on it.

  9. moved this to Waiting for: Product Owner in GitHub Issues with 👀 3on Nov 12, 2024
  10. 24 remaining items

  11. moved this from Waiting for: Product Owner to No status in GitHub Issues with 👀 3on Dec 30, 2024
  12. BSathvik commented on Jan 22, 2025

    @BSathvik

    Hey y'all! we're running into the same issue, would you be able to provide a guesstimate for the fix? Thanks!

  13. moved this to Waiting for: Product Owner in GitHub Issues with 👀 3on Jan 22, 2025
  14. antonpirker commented on Jan 23, 2025

    @antonpirker
    Contributor

    Hey @BSathvik to be honest, I can not give any time estimate for a fix for this.

    Right now we are working on the next major of the SDK. This new version will be able to use OpenTelemetry under the hood. This will give us a lot of power and maybe this issue will resolve itself. And if it does not, we will update our Django integration to bring it more in line with OpenTelementry and in the course of this we will also address this issue.

  15. moved this from Waiting for: Product Owner to No status in GitHub Issues with 👀 3on Jan 23, 2025
  16. antonpirker commented on Mar 6, 2025

    @antonpirker
    Contributor

    As this feature will be provided in the upcoming major version of the SDK from the OpenTelementry integration, I am closing this issue.

  17. nickodell commented on May 27, 2025

    @nickodell

    Hello, I have developed a package to provide a work-around for this issue. It is based on the code in the linked PR. It provides a custom integration that keeps the transaction open until gunicorn calls .close() on the StreamingHTTPResponse. It is also compatible with non-streaming responses.

    It is available here.

    https://github.com/nickodell/shr_span_recorder

  18. nickodell commented on Jun 3, 2025

    @nickodell

    @stephanie-anderson By the way, I recently tried sentry-sdk==3.0.0rc1 to see if it solves this bug, and it doesn't appear to.

    Was this release intended to solve this bug? Should I open a different issue if I'm still having this problem? Or is the fix in an upcoming release? If so, could we have clarification on which release the fix is targeted for?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions