Skip to content

opentelemetry-instrumentation-django: fix spans ending prematurely for StreamingHttpResponse#4723

Open
SurbhiD404 wants to merge 2 commits into
open-telemetry:mainfrom
SurbhiD404:fix/streaming-response-span-timing
Open

opentelemetry-instrumentation-django: fix spans ending prematurely for StreamingHttpResponse#4723
SurbhiD404 wants to merge 2 commits into
open-telemetry:mainfrom
SurbhiD404:fix/streaming-response-span-timing

Conversation

@SurbhiD404

@SurbhiD404 SurbhiD404 commented Jun 20, 2026

Copy link
Copy Markdown

Description

Fixes #4681

The Django instrumentation middleware ends the request span synchronously inside process_response(), right after get_response() returns. For a normal HttpResponse this is correct, since the body is fully rendered by that point.

For StreamingHttpResponse, however, get_response() returns before the stream is actually consumed. Django (and WSGI servers) only iterate response.streaming_content afterward, once the response is being sent to the client. As a result, the span's end_time is set before the streamed content has finished sending, producing inaccurate span durations for streaming endpoints.

For streaming responses (response.streaming), span completion is now deferred until response.close() is called, which occurs after the stream has been consumed. Existing behavior for non-streaming responses and exception paths is preserved.

Scope: this change covers the synchronous (WSGI) path only. Async/ASGI behavior remains unchanged.

Type of change

  • Bug fix (non-breaking change which fixes an issue)

How Has This Been Tested?

Added TestMiddlewareStreamingResponse in tests/test_middleware.py.

The test records the order of:

  • streaming content consumption
  • span completion (Span.end)

and verifies that the span is not completed before the streaming response is consumed.

Tested locally with:

tox -e py313-test-instrumentation-django-3

Result:

78 passed

Does This PR Require a Core Repo Change?

  • No.

Checklist:

  • Followed the style guidelines of this project
  • Changelogs have been updated
  • Unit tests have been added
  • Documentation has been updated

…r StreamingHttpResponse

Signed-off-by: Surbhi Dharvan <dharvansurbhi@gmail.com>
@linux-foundation-easycla

linux-foundation-easycla Bot commented Jun 20, 2026

Copy link
Copy Markdown

CLA Signed
The committers listed above are authorized under a signed CLA.

  • ✅ login: SurbhiD404 / name: Surbhi Dharvan (b98cc22)

@SurbhiD404 SurbhiD404 requested a review from a team as a code owner June 20, 2026 17:19
Signed-off-by: Surbhi Dharvan <dharvansurbhi@gmail.com>
@SurbhiD404 SurbhiD404 force-pushed the fix/streaming-response-span-timing branch from c20040d to 442796a Compare June 20, 2026 17:23
@SurbhiD404

Copy link
Copy Markdown
Author

Hi @xrmx,

When you get a chance, I'd appreciate a review of this PR. It fixes premature span completion for Django StreamingHttpResponse and includes a regression test and changelog fragment.

Happy to address any feedback. Thanks!

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

Labels

None yet

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

Django Instrumentation: Spans close prematurely for StreamingHttpResponse

1 participant