Skip to content

refactor: align Django middleware with OTel-native span export pattern#64

Merged
jy-tan merged 3 commits intomainfrom
fix-duplicate-root-spans
Feb 7, 2026
Merged

refactor: align Django middleware with OTel-native span export pattern#64
jy-tan merged 3 commits intomainfrom
fix-duplicate-root-spans

Conversation

@jy-tan
Copy link
Contributor

@jy-tan jy-tan commented Feb 7, 2026

Summary

The Django middleware was the only framework instrumentation that manually built CleanSpanData and called the deprecated sdk.collect_span() instead of using the standard OTel span attribute pattern. This caused a dual-write bug where both the middleware and TdSpanProcessor.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 constructing CleanSpanData and calling sdk.collect_span(). The existing span.end() call in the finally block now triggers TdSpanProcessor.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: ignore comments flagged by the ty static type checker.

Context

The original Django middleware predated the OTel-native approach and was never updated. A previous fix added an EXPORTED_BY_INSTRUMENTATION flag to prevent duplicate spans, but that was a band-aid. This refactor removes the root cause - the net diff against main shows zero changes to td_attributes.py, td_span_processor.py, or test_td_span_processor.py because the flag was added and then removed within the same branch.

Test plan

  • All 9 Django e2e tests pass (full record/replay cycle)
  • All 20 TdSpanProcessor unit tests pass

Copy link

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 4 files

@jy-tan jy-tan changed the title fix: eliminate duplicate root spans in Django middleware refactor: align Django middleware with OTel-native span export pattern Feb 7, 2026
Copy link

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@jy-tan jy-tan merged commit 29ad4fd into main Feb 7, 2026
22 checks passed
@jy-tan jy-tan deleted the fix-duplicate-root-spans branch February 7, 2026 00:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants