Skip to content

Commit 8a8427f

Browse files
sentrivanaericapisani
authored andcommitted
ref: Add new_trace, continue_trace to span first (6) (#5593)
### Description Add the new `sentry_sdk.traces.continue_trace` and `sentry_sdk.traces.new_trace` APIs. #### Issues <!-- * resolves: #1234 * resolves: LIN-1234 --> #### Reminders - Please add tests to validate your changes, and lint your code using `tox -e linters`. - Add GH Issue ID _&_ Linear ID (if applicable) - PR title should use [conventional commit](https://develop.sentry.dev/engineering-practices/commit-messages/#type) style (`feat:`, `fix:`, `ref:`, `meta:`) - For external contributors: [CONTRIBUTING.md](https://github.com/getsentry/sentry-python/blob/master/CONTRIBUTING.md), [Sentry SDK development docs](https://develop.sentry.dev/sdk/), [Discord community](https://discord.gg/Ww9hbqr)
1 parent c2ce8bc commit 8a8427f

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

@@ -99,6 +99,9 @@ def start_span(
9999
span.end()
100100
```
101101
102+
To continue a trace from another service, call
103+
`sentry_sdk.traces.continue_trace()` prior to creating a top-level span.
104+
102105
:param name: The name to identify this span by.
103106
:type name: str
104107
@@ -126,6 +129,44 @@ def start_span(
126129
)
127130

128131

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

0 commit comments

Comments
 (0)