Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion ddtrace/debugging/_origin/span.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from time import monotonic_ns
from types import FrameType
from types import FunctionType
from types import MethodType
import typing as t
import uuid

Expand Down Expand Up @@ -210,8 +211,11 @@ class SpanCodeOriginProcessorEntry:
_lock = Lock()

@classmethod
def instrument_view(cls, f):
def instrument_view(cls, f: t.Union[FunctionType, MethodType]) -> None:
if isinstance(f, MethodType):
f = t.cast(FunctionType, f.__func__)
if not _isinstance(f, FunctionType):
log.warning("Cannot instrument view %r: not a function", f)
return

with cls._lock:
Expand Down
4 changes: 3 additions & 1 deletion ddtrace/debugging/_products/code_origin/span.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import enum
from functools import partial
from types import FunctionType
from types import MethodType
import typing as t

import ddtrace.internal.core as core
Expand Down Expand Up @@ -32,7 +34,7 @@ def start():
# We need to instrument the entrypoints on boot because this is the only
# time the tracer will notify us of entrypoints being registered.
@partial(core.on, "service_entrypoint.patch")
def _(f: t.Callable) -> None:
def _(f: t.Union[FunctionType, MethodType]) -> None:
from ddtrace.debugging._origin.span import SpanCodeOriginProcessorEntry

SpanCodeOriginProcessorEntry.instrument_view(f)
Expand Down
28 changes: 28 additions & 0 deletions tests/debugging/origin/test_span.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,31 @@ def entry_call():
assert _exit.get_tag("_dd.code_origin.type") is None
assert _exit.get_tag("_dd.code_origin.frames.0.file") is None
assert _exit.get_tag("_dd.code_origin.frames.0.line") is None

def test_span_origin_entry_method(self):
class App:
def entry_call(self):
pass

app = App()

core.dispatch("service_entrypoint.patch", (app.entry_call,))

with self.tracer.trace("entry"):
app.entry_call()
with self.tracer.trace("middle"):
with self.tracer.trace("exit", span_type=SpanTypes.HTTP):
pass

self.assert_span_count(3)
entry, *_ = self.get_spans()

# Check for the expected tags on the entry span
assert entry.get_tag("_dd.code_origin.type") == "entry"
assert entry.get_tag("_dd.code_origin.frames.0.file") == str(Path(__file__).resolve())
assert entry.get_tag("_dd.code_origin.frames.0.line") == str(App.entry_call.__code__.co_firstlineno)
assert entry.get_tag("_dd.code_origin.frames.0.type") == __name__
assert (
entry.get_tag("_dd.code_origin.frames.0.method")
== "SpanProbeTestCase.test_span_origin_entry_method.<locals>.App.entry_call"
)
Loading