Skip to content

opentelemetry-instrumentation-django: add code attributes to spans - #4840

Merged
xrmx merged 9 commits into
open-telemetry:mainfrom
manduinca:feature/django-code-attributes
Nov 24, 2026
Merged

xrmx merged 9 commits into
open-telemetry:mainfrom
manduinca:feature/django-code-attributes

Conversation

@manduinca

@manduinca manduinca commented Jul 19, 2026 •

Copy link
Copy Markdown
Contributor

Description

Adds opt-in code.function.name, code.file.path and code.line.number attributes for the resolved Django view. Set OTEL_PYTHON_DJANGO_CODE_ATTRIBUTES=true to enable them; the default is off, with no view introspection when disabled or when the span is not recording.

Attributes are added to the existing Django request span, including its INTERNAL span when an outer instrumentation already handles the request. No additional spans or metric attributes are created. Class-based views identify their dispatch method; decorated functions and partials are unwrapped. Extraction is best effort and file-path disclosure is documented.

Fixes #1074

Type of change

  • New feature (non-breaking change which adds functionality)

How Has This Been Tested?

  • Python 3.12: all four Django tox variants pass (66 passed / 26 skipped on Django 2.x; 92 passed on each newer variant).
  • Tests cover opt-in and default-off behavior, non-recording and nested spans, function/class/decorated/partial views, and ASGI.
  • Ruff 0.16.1 check and format.

Does This PR Require a Core Repo Change?

  • No.

Checklist

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

@manduinca
manduinca requested a review from a team as a code owner July 19, 2026 07:38
@linux-foundation-easycla

linux-foundation-easycla Bot commented Jul 19, 2026 •

Copy link
Copy Markdown

CLA Signed
The committers listed above are authorized under a signed CLA.

  • ✅ login: manduinca / name: Jean Pierre Mandujano G. (84dc9ca, 926941a)

Assisted-by: Claude Code
@github-actions

github-actions Bot commented Aug 3, 2026

Copy link
Copy Markdown

This PR has been automatically marked as stale because it has not had any activity for 14 days. It will be closed if no further activity occurs within 14 days of this comment.
If you're still working on this, please add a comment or push new commits.

@github-actions github-actions Bot added the Stale label Aug 3, 2026
@opentelemetry-pr-dashboard

opentelemetry-pr-dashboard Bot commented Aug 14, 2026 •

Copy link
Copy Markdown

Pull request dashboard status

Merged · refreshed 2026-11-24 10:13 UTC

Status above doesn't look right?
  • Anything look wrong? Report it with what you expected; it helps us improve the dashboard.

@tammy-baylis-swi

Copy link
Copy Markdown
Contributor

Thanks for contributing @manduinca ! I commented on the linked issue and will copy here for visibility:

This is an older issue so I just wanted to gauge current use cases.

Currently the LoggingInstrumenter writes code.file.path etc (semconv) which can be opted into with manual instrumentation or OTEL_PYTHON_LOG_AUTO_INSTRUMENTATION=true. Is this enough for current users, with consideration for codebase upkeep?

@opentelemetry-pr-dashboard

This comment has been minimized.

…-attributes

# Conflicts:
#	instrumentation/opentelemetry-instrumentation-django/src/opentelemetry/instrumentation/django/middleware/otel_middleware.py
@manduinca

Copy link
Copy Markdown
Contributor Author

Thanks @tammy-baylis-swi. The logging path answers a different question than this PR.

The code.* attributes on log records tell you where a log line was emitted, and they only exist if the app opted into log auto-instrumentation and something actually logged during the request. If the view delegates to a helper and the helper logs, they point at the helper. This PR puts the attributes on the server span, so they identify the view that handled the request even when nothing was logged, which is the common case: you take a slow or failed span in the trace view and jump straight to the handler, or aggregate latency and errors per handler in the backend. http.route doesn't fully cover that since routes and views are not 1:1 in Django (one CBV or view function can be wired to several routes).

On upkeep, the change is one helper plus a single call in process_view behind span.is_recording(), uses the stable semconv constants, and is tested across the four supported Django versions. If the concern is default span size, I can gate it behind an opt-in env var like the logging one.

@xrmx

xrmx commented Aug 31, 2026

Copy link
Copy Markdown
Contributor

I understand the issue but I'm not sure the http semconv suggests to use code attributes. Let me ask to the semconv people.

@xrmx

xrmx commented Sep 16, 2026

Copy link
Copy Markdown
Contributor

I understand the issue but I'm not sure the http semconv suggests to use code attributes. Let me ask to the semconv people.

I was able to bring this to the semantic conventions SIG, got some feedback:

  • could make sense as opt-in, conformance tests may flag this as violation
  • Some java instrumentations may create a span at controller level (as in MVC) and store these code attributes there
  • Ok if first span does this, not ok for followup spans as it would like a poor version of profiling

@manduinca Any chance you can take a look at the java instrumentations report for any prior art?

@manduinca

Copy link
Copy Markdown
Contributor Author

Thanks for taking it to the SIG.

On prior art: yes, there's a fair amount of it. CodeAttributesExtractor lives in instrumentation-api-incubator under semconv/code/, and it sets code.namespace and code.function as locally-declared keys plus code.function.name imported from io.opentelemetry.semconv.CodeAttributes — so Java is already emitting the stable attribute this PR uses, alongside the deprecated pair.

It's consumed through CodeAttributesGetter by roughly a dozen instrumentations: jaxrs (1.0 and common), jaxws, grails, finatra, jfinal, mybatis, apache-elasticjob, jdbc's DataSourceCodeAttributesGetter, methods, and the @WithSpan annotation ones.

That also covers the controller point: jaxrs, grails and finatra are exactly that case, and they put the attributes on the controller span, which is nested under the HTTP server span.

On the first-span concern, this PR already satisfies it, though by a different route than Java. process_view doesn't create a span — it writes onto the server span process_request already put in request.META. One request means one set of code.* attributes, on the root span, with no extra span involved. There's no path here that decorates followup spans.

Opt-in is fine by me. The Django instrumentation already has an environment_variables.py with OTEL_PYTHON_DJANGO_INSTRUMENT, so I'd add OTEL_PYTHON_DJANGO_CODE_ATTRIBUTES there, defaulting to off, and gate the process_view block on it. Say if you'd rather have a different name or a package-wide variable instead of a Django-specific one, and I'll push that.

@manduinca

Copy link
Copy Markdown
Contributor Author

@xrmx coming back to this one. If OTEL_PYTHON_DJANGO_CODE_ATTRIBUTES, off by default, works as the opt-in, I'll put the process_view block behind it and add tests for both settings. If you or the SIG prefer another name or approach, I'll follow that instead.

For transparency: this PR and my comments here, including this one, were drafted with an AI coding assistant (Claude Code).

@opentelemetry-pr-dashboard

This comment has been minimized.

Default view code attributes to off and avoid introspection for disabled or non-recording spans. Cover nested request spans and identify callable methods for class-based views.

Assisted-by: OpenAI Codex
@manduinca

Copy link
Copy Markdown
Contributor Author

@xrmx I went ahead with it: the process_view block is now behind OTEL_PYTHON_DJANGO_CODE_ATTRIBUTES, off by default (2b5f7fc). The attributes only go on the request span Django already creates, no extra spans, and the tests cover both settings. Could you take another look?

@tammy-baylis-swi tammy-baylis-swi left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for discussion and the scope of this makes sense to me.

Approving but please could you also sync main in and do tox -e lint-instrumentation-django to fix some formatting. That might fix the changelog and botocore ci/cd failures.

@github-project-automation github-project-automation Bot moved this to Approved PRs in Python PR digest Nov 2, 2026
@xrmx
xrmx requested a balanced review from Copilot November 6, 2026 15:25
Comment thread .changelog/4840.added Resolved

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

A decorated functools.partial is not fully unwrapped, causing incorrect view function, file, and line metadata; normalization and a regression test are needed.

1 open issue
What changed in this PR

Adds opt-in source-code attributes to existing Django request spans, helping users trace requests back to resolved views without creating additional spans.

Changes:

  • Adds environment-controlled collection of view function names, file paths, and line numbers.
  • Covers function, class-based, decorated, partial, and async views, plus disabled and non-recording behavior.
  • Documents configuration and potential source-path disclosure.
File Description
instrumentation/​opentelemetry-instrumentation-django/​tests/​views.py Adds view fixtures for attribute tests.
instrumentation/​opentelemetry-instrumentation-django/​tests/​test_middleware.py Tests view attributes, opt-in behavior, and span handling.
instrumentation/​opentelemetry-instrumentation-django/​tests/​test_middleware_asgi.py Tests async view attributes and disabled collection.
instrumentation/​opentelemetry-instrumentation-django/​src/​opentelemetry/​instrumentation/​django/​middleware/​otel_middleware.py Extracts view metadata and attaches it to recording request spans.
instrumentation/​opentelemetry-instrumentation-django/​src/​opentelemetry/​instrumentation/​django/​environment_variables.py Defines the code-attributes configuration variable.
instrumentation/​opentelemetry-instrumentation-django/​src/​opentelemetry/​instrumentation/​django/​__init__.py Reads configuration and documents the feature.
.changelog/​4840.added Records the opt-in feature.

Review effort: Balanced

Assisted-by: AI directed by me
@opentelemetry-pr-dashboard

This comment has been minimized.

@manduinca

Copy link
Copy Markdown
Contributor Author

@tammy-baylis-swi thanks!

I merged main on November 3; tox -e lint-instrumentation-django passes locally. The latest update also fixes the decorated-partial case and applies the changelog suggestion; the targeted Django tests pass locally.

The test workflow from main exceeds GitHub’s 500 KB limit, so it cannot start. Three other workflows need approval. Could you take another look?

@tammy-baylis-swi

Copy link
Copy Markdown
Contributor

/dashboard route:maintainers

@opentelemetry-pr-dashboard

Copy link
Copy Markdown

@tammy-baylis-swi, /dashboard route:maintainers is not a recognized dashboard command. The only supported command is /dashboard route:reviewers, which the pull request author can use to move a pull request from waiting on the author to waiting on reviewers.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Decorated callable views can report the wrapper’s source location alongside the original method’s name; normalization must also unwrap call decorators, with a regression test covering this case.

1 open issue
1 resolved since last review

Review effort: Balanced

Assisted-by: Codex (GPT-6.2 Sol)
@xrmx
xrmx enabled auto-merge November 24, 2026 10:01
@xrmx
xrmx added this pull request to the merge queue Nov 24, 2026
Merged via the queue into open-telemetry:main with commit 79d3150 Nov 24, 2026
770 checks passed
@github-project-automation github-project-automation Bot moved this from Approved PRs to Done in Python PR digest Nov 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Add code information in span based on semantic conventions in Django instrumentation

5 participants