opentelemetry-instrumentation-django: add code attributes to spans#4840
Open
manduinca wants to merge 2 commits into
Open
opentelemetry-instrumentation-django: add code attributes to spans#4840manduinca wants to merge 2 commits into
manduinca wants to merge 2 commits into
Conversation
Assisted-by: Claude Code
Assisted-by: Claude Code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Adds
code.function.name,code.file.pathandcode.line.numberspan attributes to the Django instrumentation. The attributes are set inprocess_view, where the resolved view callable is available.The issue originally asked for
code.namespaceandcode.function, but those are deprecated now, so this uses the current stable code attributes fromopentelemetry.semconv.attributes.code_attributes.The extraction is best effort and never raises: it handles plain view functions, class based views,
functools.partialand 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
How Has This Been Tested?
test_middleware.py(function view, class based view, partial, decorated view) andtest_middleware_asgi.py(async view), asserting the exact attribute names and values against the view's own__code__metadatatox -e py312-test-instrumentation-django-{0,1,2,3}(Django 2.x through 5.2), 82 passed on eachtox -e lint-instrumentation-django, ruff 0.14.1 check and format, codespellNote on the async test: it needs
override_settings(ROOT_URLCONF=...)becausesettings.configure()fromsetUpClassis 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?
Checklist: