Skip to content
Open
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
10 changes: 7 additions & 3 deletions ddtrace/_trace/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,10 @@ def activate(self, ctx: Optional[ActiveTrace]) -> None:
def active(self) -> Optional[ActiveTrace]:
"""Returns the active span or context for the current execution."""
item = _DD_CONTEXTVAR.get()
if isinstance(item, Span):

# PERF: type(item) is Span is about the same perf as isinstance(item, Span)
# when item is a Span, but slower when item is a Context
if type(item) is Span:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't expect subclasses of Span to ever appear?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only one I see in our codebase is:

tests/utils.py:745:class TestSpan(Span):

Not sure when/why we would subclass Span

return self._update_active(item)
return item

Expand All @@ -82,8 +85,9 @@ def _update_active(self, span: Span) -> Optional[ActiveTrace]:
If no parent exists and the context is reactivatable, that context is restored.
"""
new_active: Optional[Span] = span
# PERF: Avoid calling `Span.finished` more than once per span. This is a computed property.
while new_active and new_active.finished:
# PERF: Avoid checking if the span is finished more than once per span.
# PERF: By-pass Span.finished which is a computed property to avoid the function call overhead
while new_active and new_active.duration_ns is not None:
if new_active._parent is None and new_active._parent_context and new_active._parent_context._reactivate:
self.activate(new_active._parent_context)
return new_active._parent_context
Expand Down
Loading