opentelemetry-instrumentation-django: fix spans ending prematurely for StreamingHttpResponse#4723
Open
SurbhiD404 wants to merge 2 commits into
Open
Conversation
…r StreamingHttpResponse Signed-off-by: Surbhi Dharvan <dharvansurbhi@gmail.com>
|
|
Signed-off-by: Surbhi Dharvan <dharvansurbhi@gmail.com>
c20040d to
442796a
Compare
Author
|
Hi @xrmx, When you get a chance, I'd appreciate a review of this PR. It fixes premature span completion for Django Happy to address any feedback. Thanks! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Fixes #4681
The Django instrumentation middleware ends the request span synchronously inside
process_response(), right afterget_response()returns. For a normalHttpResponsethis 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 iterateresponse.streaming_contentafterward, once the response is being sent to the client. As a result, the span'send_timeis 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 untilresponse.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
How Has This Been Tested?
Added
TestMiddlewareStreamingResponseintests/test_middleware.py.The test records the order of:
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-3Result:
78 passedDoes This PR Require a Core Repo Change?
Checklist: