refactor: align Django middleware with OTel-native span export pattern#64
Merged
refactor: align Django middleware with OTel-native span export pattern#64
Conversation
sohankshirsagar
approved these changes
Feb 7, 2026
There was a problem hiding this comment.
1 issue found across 4 files (changes from recent commits).
Prompt for AI agents (all issues)
Check if these issues are valid — if so, understand the root cause of each and fix them.
<file name="drift/core/tracing/td_span_processor.py">
<violation number="1">
P1: The code removes the deduplication logic described in the PR summary. The summary claims to add/use `EXPORTED_BY_INSTRUMENTATION` to prevent duplicate spans, but the diff removes this check, and the attribute is missing from `td_attributes.py`. This contradicts the stated goal and implementation strategy. If the intention is to switch to a single-write pattern (as implied by the `middleware.py` content), the PR description should be updated to reflect this, as the current description is misleading.</violation>
</file>
Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.
sohankshirsagar
approved these changes
Feb 7, 2026
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.
Summary
The Django middleware was the only framework instrumentation that manually built
CleanSpanDataand called the deprecatedsdk.collect_span()instead of using the standard OTel span attribute pattern. This caused a dual-write bug where both the middleware andTdSpanProcessor.on_end()would produce a root span per request (one with full HTTP data and one empty). This refactor eliminates that divergence by aligning Django with the same single-write pattern used by Flask/WSGI and FastAPI.Changes
drift/instrumentation/django/middleware.py: Refactored_capture_span()and_capture_error_span()to set OTel span attributes (INPUT_VALUE,OUTPUT_VALUE,INPUT_SCHEMA_MERGES,OUTPUT_SCHEMA_MERGES,TRANSFORM_METADATA,NAME, status) instead of manually constructingCleanSpanDataand callingsdk.collect_span(). The existingspan.end()call in thefinallyblock now triggersTdSpanProcessor.on_end()to handle conversion and export - identical to the Flask and FastAPI code paths. Removed unused imports (CleanSpanData,Duration,Timestamp,SpanStatus,StatusCode).drift/instrumentation/psycopg/instrumentation.py: Removed three redundant# type: ignorecomments flagged by thetystatic type checker.Context
The original Django middleware predated the OTel-native approach and was never updated. A previous fix added an
EXPORTED_BY_INSTRUMENTATIONflag to prevent duplicate spans, but that was a band-aid. This refactor removes the root cause - the net diff againstmainshows zero changes totd_attributes.py,td_span_processor.py, ortest_td_span_processor.pybecause the flag was added and then removed within the same branch.Test plan