Skip to content

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

Open
manduinca wants to merge 2 commits into
open-telemetry:mainfrom
manduinca:feature/django-code-attributes
Open

opentelemetry-instrumentation-django: add code attributes to spans#4840
manduinca wants to merge 2 commits into
open-telemetry:mainfrom
manduinca:feature/django-code-attributes

Conversation

@manduinca

Copy link
Copy Markdown

Description

Adds code.function.name, code.file.path and code.line.number span attributes to the Django instrumentation. The attributes are set in process_view, where the resolved view callable is available.

The issue originally asked for code.namespace and code.function, but those are deprecated now, so this uses the current stable code attributes from opentelemetry.semconv.attributes.code_attributes.

The extraction is best effort and never raises: it handles plain view functions, class based views, functools.partial and decorated views. For class based views the line number is omitted on purpose, since classes have no __code__ and resolving the line would mean parsing source on every request.

Fixes #1074

Type of change

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

How Has This Been Tested?

  • New tests in test_middleware.py (function view, class based view, partial, decorated view) and test_middleware_asgi.py (async view), asserting the exact attribute names and values against the view's own __code__ metadata
  • tox -e py312-test-instrumentation-django-{0,1,2,3} (Django 2.x through 5.2), 82 passed on each
  • tox -e lint-instrumentation-django, ruff 0.14.1 check and format, codespell

Note on the async test: it needs override_settings(ROOT_URLCONF=...) because settings.configure() from setUpClass is not visible to the URL resolver when another test module configured settings first in the same process. The existing ASGI tests don't hit this because both urlconfs mirror the same routes.

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

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

1 participant