Skip to content

Allow _DjangoMiddleware to have span creation overridden #3652

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

jourdanrodrigues
Copy link

Description

I had a need to dictate how the span creation would happen, specifically because the span I wanted to be the root was at the gunicorn worker level, not Django nor WSGI. Main reason was to include its request log in the trace.

Also, in our context, one span is enough to wrap gunicorn, WSGI and _DjangoMiddleware (for billing and practical purposes), so had WSGI and _DjangoMiddleware contribute with their attributes to the span created by gunicorn.

I had to override the entirety of _DjangoMiddleware.process_request in order to reuse the existing span and to avoid end_on_exit that's currently hardcoded to true.

This _DjangoMiddleware._get_span will allow achieving this behavior without such wide override.

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

  • Tests at instrumentation/opentelemetry-instrumentation-django/tests/test_middleware.py

Does This PR Require a Core Repo Change?

  • Yes. - Link to PR:
  • No.

Checklist:

See contributing.md for styleguide, changelog guidelines, and more.

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

@jourdanrodrigues jourdanrodrigues requested a review from a team as a code owner July 25, 2025 15:03
@jourdanrodrigues jourdanrodrigues changed the title Allow _DjangoMiddleware to have more override entrypoints Allow _DjangoMiddleware to have span creation overridden Jul 25, 2025
@tammy-baylis-swi
Copy link
Contributor

Thanks @jourdanrodrigues for contributing! Would it be possible to share example spans from before and after this change?

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