Skip to content

Commit fb2d052

Browse files
fix: rm functions_for_code usage in code origins [backport 2.21] (#13568)
Backport a6520bc from #13539 to 2.21. Remove the call to `functions_for_code` from code origins. For Python3.11+ we'll use the fully qualified name stored on the Code Object: https://docs.python.org/3/reference/datamodel.html#codeobject.co_qualname For older version, we'll fall back to the function name. refs: DEBUG-3967 ## Checklist - [x] PR author has checked that all the criteria below are met - The PR description includes an overview of the change - The PR description articulates the motivation for the change - The change includes tests OR the PR description describes a testing strategy - The PR description notes risks associated with the change, if any - Newly-added code is easy to change - The change follows the [library release note guidelines](https://ddtrace.readthedocs.io/en/stable/releasenotes.html) - The change includes or references documentation updates if necessary - Backport labels are set (if [applicable](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting)) ## Reviewer Checklist - [x] Reviewer has checked that all the criteria below are met - Title is accurate - All changes are related to the pull request's stated goal - Avoids breaking [API](https://ddtrace.readthedocs.io/en/stable/versioning.html#interfaces) changes - Testing strategy adequately addresses listed risks - Newly-added code is easy to change - Release note makes sense to a user of the library - If necessary, author has acknowledged and discussed the performance implications of this PR as reported in the benchmarks PR comment - Backport labels are set in a manner that is consistent with the [release branch maintenance policy](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting) Co-authored-by: Tyler Finethy <[email protected]>
1 parent 64d6eb7 commit fb2d052

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

ddtrace/debugging/_origin/span.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
from ddtrace.internal import core
2929
from ddtrace.internal.packages import is_user_code
3030
from ddtrace.internal.safety import _isinstance
31-
from ddtrace.internal.utils.inspection import functions_for_code
3231
from ddtrace.internal.wrapping.context import WrappingContext
3332
from ddtrace.settings.code_origin import config as co_config
3433
from ddtrace.trace import Span
@@ -217,12 +216,17 @@ def on_span_start(self, span: Span) -> None:
217216

218217
span.set_tag_str(f"_dd.code_origin.frames.{n}.file", filename)
219218
span.set_tag_str(f"_dd.code_origin.frames.{n}.line", str(code.co_firstlineno))
219+
220+
# Get the module and function name from the frame and code object. In Python3.11+ qualname
221+
# is available, otherwise we'll fallback to the unqualified name.
220222
try:
221-
(f,) = functions_for_code(code)
222-
span.set_tag_str(f"_dd.code_origin.frames.{n}.type", f.__module__)
223-
span.set_tag_str(f"_dd.code_origin.frames.{n}.method", f.__qualname__)
224-
except ValueError:
225-
continue
223+
name = code.co_qualname # type: ignore[attr-defined]
224+
except AttributeError:
225+
name = code.co_name
226+
227+
mod = frame.f_globals.get("__name__")
228+
span.set_tag_str(f"_dd.code_origin.frames.{n}.type", mod) if mod else None
229+
span.set_tag_str(f"_dd.code_origin.frames.{n}.method", name) if name else None
226230

227231
# TODO[gab]: This will be enabled as part of the live debugger/distributed debugging
228232
# if ld_config.enabled:
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
fixes:
3+
- |
4+
dynamic instrumentation: Fixed an issue with a GC-based lookup for code origin resolution resulting in high
5+
performance overhead.

0 commit comments

Comments
 (0)