refactor: align Django middleware with OTel-native span export pattern #64
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