Skip to content

Commit c332ef9

Browse files
authored
perf(co): retrieve context from spans (#14929)
We retrieve the context object that we use to link sessions to traces directly from span objects to avoid triggering more expensive tracer logic by querying for the current running context from the tracer itself
1 parent 3fd2417 commit c332ef9

File tree

2 files changed

+10
-10
lines changed

2 files changed

+10
-10
lines changed

ddtrace/debugging/_origin/span.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ def _close_signal(self, retval=None, exc_info=(None, None, None)):
166166

167167
# Check if we have any level 2 debugging sessions running for the
168168
# current trace
169-
if any(s.level >= 2 for s in Session.from_trace()):
169+
if any(s.level >= 2 for s in Session.from_trace(root.context or span.context)):
170170
# Create a snapshot
171171
snapshot = Snapshot(
172172
probe=self.location.probe,
@@ -270,7 +270,7 @@ def on_span_start(self, span: Span) -> None:
270270

271271
# Check if we have any level 2 debugging sessions running for
272272
# the current trace
273-
if any(s.level >= 2 for s in Session.from_trace()):
273+
if any(s.level >= 2 for s in Session.from_trace(span.context)):
274274
# Create a snapshot
275275
snapshot = Snapshot(
276276
probe=ExitSpanProbe.from_frame(frame),

ddtrace/debugging/_session.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ def get_probe_count(self, probe_id: str) -> int:
7575
return self._counts.get(probe_id, 0)
7676

7777
@classmethod
78-
def from_trace(cls) -> t.List["Session"]:
79-
return SessionManager.get_sessions_for_trace()
78+
def from_trace(cls, trace_context: t.Optional[t.Any] = None) -> t.List["Session"]:
79+
return SessionManager.get_sessions_for_trace(trace_context)
8080

8181
@classmethod
8282
def lookup(cls, ident: SessionId) -> t.Optional["Session"]:
@@ -112,12 +112,12 @@ def unlink_session_from_trace(cls, session, trace_context: t.Optional[t.Any] = N
112112
cls._sessions_trace_map.get(context, {}).pop(session.ident, None)
113113

114114
@classmethod
115-
def get_sessions_for_trace(cls) -> t.List[Session]:
116-
context = tracer.current_trace_context()
117-
if context is None:
118-
return []
119-
120-
return list(cls._sessions_trace_map.get(context, {}).values())
115+
def get_sessions_for_trace(cls, trace_context: t.Optional[t.Any] = None) -> t.List[Session]:
116+
return (
117+
list(cls._sessions_trace_map.get(context, {}).values())
118+
if cls._sessions_trace_map and (context := (trace_context or tracer.current_trace_context())) is not None
119+
else []
120+
)
121121

122122
@classmethod
123123
def lookup_session(cls, ident: SessionId) -> t.Optional[Session]:

0 commit comments

Comments
 (0)