Skip to content

Force creation of a new trace in continue_trace with empty headers #4682

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions MIGRATION_GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,20 @@ Looking to upgrade from Sentry SDK 2.x to 3.x? Here's a comprehensive list of wh
- 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
- `sentry_sdk.start_span` now only takes keyword arguments.
- `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`.
- `sentry_sdk.continue_trace` no longer returns a `Transaction` and is now a context manager.
- `sentry_sdk.continue_trace` no longer returns a `Transaction` and is now a context manager.

- Use it to continue an upstream trace with the `sentry-trace` and `baggage` headers.

```python
headers = {"sentry-trace": "{trace_id}-{span_id}-{sampled_flag}", "baggage": "{baggage header}"}
with sentry_sdk.continue_trace(headers):
with sentry_sdk.start_span(name="continued span in trace"):
pass
```

- If the headers are empty, a new trace will be started.
- If you want to force creation of a new trace, use the `sentry_sdk.new_trace` context manager.

- 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`.
- The `Span()` constructor does not accept a `hub` parameter anymore.
- `Span.finish()` does not accept a `hub` parameter anymore.
Expand Down Expand Up @@ -227,7 +240,6 @@ Looking to upgrade from Sentry SDK 2.x to 3.x? Here's a comprehensive list of wh
### Deprecated

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


Expand Down
4 changes: 3 additions & 1 deletion sentry_sdk/opentelemetry/scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ def continue_trace(

span_context = self._incoming_otel_span_context()
if span_context is None:
yield
# force a new trace since no incoming stuff
with use_span(INVALID_SPAN):
yield
else:
with use_span(NonRecordingSpan(span_context)):
yield
Expand Down
19 changes: 19 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,25 @@ def test_continue_trace(sentry_init):
}


def test_continue_trace_without_headers_starts_new_trace(sentry_init, capture_events):
sentry_init(traces_sample_rate=1.0)
events = capture_events()

with start_span(name="parent"):
with start_span(name="child"):
with continue_trace({}):
with start_span(name="parent2"):
with start_span(name="child2"):
pass

assert len(events) == 2
(tx1, tx2) = events
assert tx1["transaction"] == "parent2"
assert tx1["spans"][0]["description"] == "child2"
assert tx2["transaction"] == "parent"
assert tx2["spans"][0]["description"] == "child"


def test_new_trace(sentry_init, capture_events):
sentry_init(traces_sample_rate=1.0)
events = capture_events()
Expand Down