Repository navigation
opentelemetry-instrumentation-django: add code attributes to spans - #4840
Conversation
Assisted-by: Claude Code
Assisted-by: Claude Code
|
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. |
Pull request dashboard statusMerged · refreshed 2026-11-24 10:13 UTC Status above doesn't look right?
|
|
Thanks for contributing @manduinca ! I commented on the linked issue and will copy here for visibility:
|
This comment has been minimized.
This comment has been minimized.
…-attributes # Conflicts: # instrumentation/opentelemetry-instrumentation-django/src/opentelemetry/instrumentation/django/middleware/otel_middleware.py
|
Thanks @tammy-baylis-swi. The logging path answers a different question than this PR. The On upkeep, the change is one helper plus a single call in |
|
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:
@manduinca Any chance you can take a look at the java instrumentations report for any prior art? |
|
Thanks for taking it to the SIG. On prior art: yes, there's a fair amount of it. It's consumed through 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. Opt-in is fine by me. The Django instrumentation already has an |
|
@xrmx coming back to this one. If For transparency: this PR and my comments here, including this one, were drafted with an AI coding assistant (Claude Code). |
This comment has been minimized.
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
tammy-baylis-swi
left a comment
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
🟡 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
This comment has been minimized.
This comment has been minimized.
|
@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? |
|
/dashboard route:maintainers |
|
@tammy-baylis-swi, |
There was a problem hiding this comment.
🟡 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)

Description
Adds opt-in
code.function.name,code.file.pathandcode.line.numberattributes for the resolved Django view. SetOTEL_PYTHON_DJANGO_CODE_ATTRIBUTES=trueto 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
INTERNALspan 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
How Has This Been Tested?
Does This PR Require a Core Repo Change?
Checklist