Skip to content

Commit 53005b9

Browse files
Force creation of a new trace in continue_trace with empty headers (#4682)
Co-authored-by: Ivana Kellyer <[email protected]>
1 parent 954eaa5 commit 53005b9

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

MIGRATION_GUIDE.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,20 @@ Looking to upgrade from Sentry SDK 2.x to 3.x? Here's a comprehensive list of wh
2424
- The default of `traces_sample_rate` changed to `0`. Meaning: Incoming traces will be continued by default. For example, if your frontend sends a `sentry-trace/baggage` headers pair, your SDK will create Spans and send them to Sentry. (The default used to be `None` meaning by default no Spans where created, no matter what headers the frontend sent to your project.) See also: https://docs.sentry.io/platforms/python/configuration/options/#traces_sample_rate
2525
- `sentry_sdk.start_span` now only takes keyword arguments.
2626
- `sentry_sdk.start_transaction`/`sentry_sdk.start_span` no longer takes the following arguments: `span`, `parent_sampled`, `trace_id`, `span_id` or `parent_span_id`.
27-
- `sentry_sdk.continue_trace` no longer returns a `Transaction` and is now a context manager.
27+
- `sentry_sdk.continue_trace` no longer returns a `Transaction` and is now a context manager.
28+
29+
- Use it to continue an upstream trace with the `sentry-trace` and `baggage` headers.
30+
31+
```python
32+
headers = {"sentry-trace": "{trace_id}-{span_id}-{sampled_flag}", "baggage": "{baggage header}"}
33+
with sentry_sdk.continue_trace(headers):
34+
with sentry_sdk.start_span(name="continued span in trace"):
35+
pass
36+
```
37+
38+
- If the headers are empty, a new trace will be started.
39+
- If you want to force creation of a new trace, use the `sentry_sdk.new_trace` context manager.
40+
2841
- You can no longer change the sampled status of a span with `span.sampled = False` after starting it. The sampling decision needs to be either be made in the `traces_sampler`, or you need to pass an explicit `sampled` parameter to `start_span`.
2942
- The `Span()` constructor does not accept a `hub` parameter anymore.
3043
- `Span.finish()` does not accept a `hub` parameter anymore.
@@ -227,7 +240,6 @@ Looking to upgrade from Sentry SDK 2.x to 3.x? Here's a comprehensive list of wh
227240
### Deprecated
228241

229242
- `sentry_sdk.start_transaction()` is deprecated. Use `sentry_sdk.start_span()` instead.
230-
- If you want to force creation of a new trace, use the `sentry_sdk.new_trace()` context manager.
231243
- `Span.set_data()` is deprecated. Use `Span.set_attribute()` instead.
232244

233245

sentry_sdk/opentelemetry/scope.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,9 @@ def continue_trace(
8585

8686
span_context = self._incoming_otel_span_context()
8787
if span_context is None:
88-
yield
88+
# force a new trace since no incoming stuff
89+
with use_span(INVALID_SPAN):
90+
yield
8991
else:
9092
with use_span(NonRecordingSpan(span_context)):
9193
yield

tests/test_api.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,25 @@ def test_continue_trace(sentry_init):
9797
}
9898

9999

100+
def test_continue_trace_without_headers_starts_new_trace(sentry_init, capture_events):
101+
sentry_init(traces_sample_rate=1.0)
102+
events = capture_events()
103+
104+
with start_span(name="parent"):
105+
with start_span(name="child"):
106+
with continue_trace({}):
107+
with start_span(name="parent2"):
108+
with start_span(name="child2"):
109+
pass
110+
111+
assert len(events) == 2
112+
(tx1, tx2) = events
113+
assert tx1["transaction"] == "parent2"
114+
assert tx1["spans"][0]["description"] == "child2"
115+
assert tx2["transaction"] == "parent"
116+
assert tx2["spans"][0]["description"] == "child"
117+
118+
100119
def test_new_trace(sentry_init, capture_events):
101120
sentry_init(traces_sample_rate=1.0)
102121
events = capture_events()

0 commit comments

Comments
 (0)