Skip to content

Commit 1af631e

Browse files
committed
chore(trace): add helper to get the top_level span of a service
1 parent 2cd1ce4 commit 1af631e

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

ddtrace/_trace/span.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ class Span(object):
128128
"_context",
129129
"_parent_context",
130130
"_local_root_value",
131+
"_service_entry_span_value",
131132
"_parent",
132133
"_ignored_exceptions",
133134
"_on_finish_callbacks",
@@ -221,6 +222,7 @@ def __init__(
221222
self._parent: Optional["Span"] = None
222223
self._ignored_exceptions: Optional[List[Type[Exception]]] = None
223224
self._local_root_value: Optional["Span"] = None # None means this is the root span.
225+
self._service_entry_span_value: Optional["Span"] = None # None means this is the service entry span.
224226
self._store: Optional[Dict[str, Any]] = None
225227

226228
def _update_tags_from_context(self) -> None:
@@ -725,6 +727,18 @@ def _local_root(self, value: "Span") -> None:
725727
def _local_root(self) -> None:
726728
del self._local_root_value
727729

730+
@property
731+
def _service_entry_span(self) -> "Span":
732+
return self._service_entry_span_value or self
733+
734+
@_service_entry_span.setter
735+
def _service_entry_span(self, span: "Span") -> None:
736+
self._service_entry_span_value = None if span is self else span
737+
738+
@_service_entry_span.deleter
739+
def _service_entry_span(self) -> None:
740+
del self._service_entry_span_value
741+
728742
def link_span(self, context: Context, attributes: Optional[Dict[str, Any]] = None) -> None:
729743
"""Defines a causal relationship between two spans"""
730744
if not context.trace_id or not context.span_id:

ddtrace/_trace/tracer.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,8 @@ def _start_span(
553553
if parent:
554554
span._parent = parent
555555
span._local_root = parent._local_root
556+
if span._parent.service == service:
557+
span._service_entry_span = parent._service_entry_span
556558

557559
for k, v in _get_metas_to_propagate(context):
558560
# We do not want to propagate AppSec propagation headers

0 commit comments

Comments
 (0)