Skip to content

Commit 5879bb7

Browse files
committed
perf(co): retrieve context from spans
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 cbd2a7d commit 5879bb7

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
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_context(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_context(span.context)):
274274
# Create a snapshot
275275
snapshot = Snapshot(
276276
probe=ExitSpanProbe.from_frame(frame),

ddtrace/debugging/_session.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ def get_probe_count(self, probe_id: str) -> int:
7878
def from_trace(cls) -> t.List["Session"]:
7979
return SessionManager.get_sessions_for_trace()
8080

81+
@classmethod
82+
def from_context(cls, context) -> t.List["Session"]:
83+
return SessionManager.get_sessions_for_context(context)
84+
8185
@classmethod
8286
def lookup(cls, ident: SessionId) -> t.Optional["Session"]:
8387
return SessionManager.lookup_session(ident)
@@ -119,6 +123,13 @@ def get_sessions_for_trace(cls) -> t.List[Session]:
119123

120124
return list(cls._sessions_trace_map.get(context, {}).values())
121125

126+
@classmethod
127+
def get_sessions_for_context(cls, context) -> t.List[Session]:
128+
if context is None:
129+
return []
130+
131+
return list(cls._sessions_trace_map.get(context, {}).values())
132+
122133
@classmethod
123134
def lookup_session(cls, ident: SessionId) -> t.Optional[Session]:
124135
context = tracer.current_trace_context()

0 commit comments

Comments
 (0)