Skip to content

Commit f223574

Browse files
committed
Correctly detect user-set parent_span=None
1 parent 4b14e8d commit f223574

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

sentry_sdk/scope.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
normalize_incoming_data,
3434
PropagationContext,
3535
)
36-
from sentry_sdk.traces import StreamedSpan, NoOpStreamedSpan
36+
from sentry_sdk.traces import _DEFAULT_PARENT_SPAN, StreamedSpan, NoOpStreamedSpan
3737
from sentry_sdk.tracing import (
3838
BAGGAGE_HEADER_NAME,
3939
SENTRY_TRACE_HEADER_NAME,
@@ -1177,9 +1177,9 @@ def start_span(
11771177
def start_streamed_span(
11781178
self,
11791179
name: str,
1180-
attributes: "Optional[Attributes]" = None,
1181-
parent_span: "Optional[StreamedSpan]" = None,
1182-
active: bool = True,
1180+
attributes: "Optional[Attributes]",
1181+
parent_span: "Optional[StreamedSpan]",
1182+
active: bool,
11831183
) -> "StreamedSpan":
11841184
# TODO: rename to start_span once we drop the old API
11851185
if isinstance(parent_span, NoOpStreamedSpan):
@@ -1189,7 +1189,9 @@ def start_streamed_span(
11891189
"currently active span instead."
11901190
)
11911191

1192-
if parent_span is None or isinstance(parent_span, NoOpStreamedSpan):
1192+
if parent_span is _DEFAULT_PARENT_SPAN or isinstance(
1193+
parent_span, NoOpStreamedSpan
1194+
):
11931195
parent_span = self.span # type: ignore
11941196

11951197
# If no eligible parent_span was provided and there is no currently

sentry_sdk/traces.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,18 +59,24 @@ def __str__(self) -> str:
5959
}
6060

6161

62+
# Sentinel value for an unset parent_span to be able to distinguish it from
63+
# a None set by the user
64+
_DEFAULT_PARENT_SPAN = object()
65+
66+
6267
def start_span(
6368
name: str,
6469
attributes: "Optional[Attributes]" = None,
65-
parent_span: "Optional[StreamedSpan]" = None,
70+
parent_span: "Optional[StreamedSpan]" = _DEFAULT_PARENT_SPAN,
6671
active: bool = True,
6772
) -> "StreamedSpan":
6873
"""
6974
Start a span.
7075
7176
The span's parent, unless provided explicitly via the `parent_span` argument,
7277
will be the current active span, if any. If there is none, this span will
73-
become the root of a new span tree.
78+
become the root of a new span tree. If you explicitly want this span to be
79+
top-level without a parent, set `parent_span=None`.
7480
7581
`start_span()` can either be used as context manager or you can use the span
7682
object it returns and explicitly end it via `span.end()`. The following is
@@ -102,7 +108,8 @@ def start_span(
102108
103109
:param parent_span: A span instance that the new span should consider its
104110
parent. If not provided, the parent will be set to the currently active
105-
span, if any.
111+
span, if any. If set to `None`, this span will become a new root-level
112+
span.
106113
:type parent_span: "Optional[StreamedSpan]"
107114
108115
:param active: Controls whether spans started while this span is running

0 commit comments

Comments
 (0)