Skip to content

Commit 157d123

Browse files
committed
ref: Add new_trace, continue_trace to span first
1 parent 49bdbe6 commit 157d123

File tree

1 file changed

+42
-1
lines changed

1 file changed

+42
-1
lines changed

sentry_sdk/traces.py

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from sentry_sdk.utils import format_attribute, logger
1515

1616
if TYPE_CHECKING:
17-
from typing import Optional, Union
17+
from typing import Any, Optional, Union
1818
from sentry_sdk._types import Attributes, AttributeValue
1919

2020

@@ -93,6 +93,9 @@ def start_span(
9393
span.end()
9494
```
9595
96+
To continue a trace from another service, call
97+
`sentry_sdk.traces.continue_trace()` prior to creating a top-level span.
98+
9699
:param name: The name to identify this span by.
97100
:type name: str
98101
@@ -119,6 +122,44 @@ def start_span(
119122
)
120123

121124

125+
def continue_trace(incoming: "dict[str, Any]") -> None:
126+
"""
127+
Continue a trace from headers or environment variables.
128+
129+
This function sets the propagation context on the scope. Any span started
130+
in the updated scope will belong under the trace extracted from the
131+
provided propagation headers or environment variables.
132+
133+
continue_trace() doesn't start any spans on its own. Use the start_span()
134+
API for that.
135+
"""
136+
# This is set both on the isolation and the current scope for compatibility
137+
# reasons. Conceptually, it belongs on the isolation scope, and it also
138+
# used to be set there in non-span-first mode. But in span first mode, we
139+
# start spans on the current scope, regardless of type, like JS does, so we
140+
# need to set the propagation context there.
141+
sentry_sdk.get_isolation_scope().generate_propagation_context(
142+
incoming,
143+
)
144+
return sentry_sdk.get_current_scope().generate_propagation_context(
145+
incoming,
146+
)
147+
148+
149+
def new_trace() -> None:
150+
"""
151+
Resets the propagation context, forcing a new trace.
152+
153+
This function sets the propagation context on the scope. Any span started
154+
in the updated scope will start its own trace.
155+
156+
new_trace() doesn't start any spans on its own. Use the start_span() API
157+
for that.
158+
"""
159+
sentry_sdk.get_isolation_scope().set_new_propagation_context()
160+
sentry_sdk.get_current_scope().set_new_propagation_context()
161+
162+
122163
class StreamedSpan:
123164
"""
124165
A span holds timing information of a block of code.

0 commit comments

Comments
 (0)