Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
80 commits
Select commit Hold shift + click to select a range
946decc
ref: Remove flag storage from StreamedSpan
sentrivana Mar 5, 2026
f3ee55c
ref: Tweak StreamedSpan interface
sentrivana Mar 5, 2026
47ed910
Add missing logger
sentrivana Mar 5, 2026
5023c76
fixes
sentrivana Mar 5, 2026
6445447
ref: Add active to StreamedSpan
sentrivana Mar 5, 2026
47e6211
Add property
sentrivana Mar 5, 2026
1e7b694
ref: Add no-op streaming span class
sentrivana Mar 5, 2026
80bfe5a
Remove redundant stuff
sentrivana Mar 5, 2026
1f0ffc1
Merge branch 'master' into ivana/span-first-4-add-noop-span
sentrivana Mar 5, 2026
d773428
ref: Add experimental streaming API
sentrivana Mar 5, 2026
647fa79
reformat
sentrivana Mar 5, 2026
49bdbe6
Add a __repr__
sentrivana Mar 5, 2026
cdd8bd6
Merge branch 'master' into ivana/span-first-5-add-start-span-api
sentrivana Mar 5, 2026
54f81af
ref: Add new_trace, continue_trace to span first
sentrivana Mar 5, 2026
941863e
ref: Add streaming trace decorator
sentrivana Mar 5, 2026
4b14e8d
Remove redundant code
sentrivana Mar 5, 2026
474f8e6
simplify
sentrivana Mar 5, 2026
9996e29
Merge branch 'ivana/span-first-5-add-start-span-api' into ivana/span-…
sentrivana Mar 5, 2026
e20d4fd
Merge branch 'ivana/span-first-6-add-continue-and-new-trace' into iva…
sentrivana Mar 5, 2026
f2738ff
reorder imports
sentrivana Mar 5, 2026
7874a54
ref: Per-bucket limits, fix envelope chunking
sentrivana Mar 5, 2026
63a9396
.
sentrivana Mar 5, 2026
c974d3e
add dummy __enter__, __exit__
sentrivana Mar 5, 2026
5d8c238
Merge branch 'ivana/span-first-7-add-trace-decorator' into ivana/span…
sentrivana Mar 5, 2026
831adae
type hint
sentrivana Mar 5, 2026
656ef2e
Merge branch 'ivana/span-first-7-add-trace-decorator' into ivana/span…
sentrivana Mar 5, 2026
1dcf176
remove unused import
sentrivana Mar 5, 2026
0a7eae8
ref: Allow to start and finish StreamedSpans
sentrivana Mar 5, 2026
6888c56
Add end, finish to noop spans
sentrivana Mar 6, 2026
09e5cce
fixes
sentrivana Mar 6, 2026
ae2fd52
.
sentrivana Mar 6, 2026
f223574
Correctly detect user-set parent_span=None
sentrivana Mar 6, 2026
05a4157
Merge branch 'master' into ivana/span-first-5-add-start-span-api
sentrivana Mar 6, 2026
9e8e60e
mypy
sentrivana Mar 6, 2026
777a246
Merge branch 'ivana/span-first-5-add-start-span-api' into ivana/span-…
sentrivana Mar 6, 2026
9b1e2f3
Merge branch 'ivana/span-first-6-add-continue-and-new-trace' into iva…
sentrivana Mar 6, 2026
e589c53
Merge branch 'ivana/span-first-7-add-trace-decorator' into ivana/span…
sentrivana Mar 6, 2026
1487ea8
Merge branch 'ivana/span-first-8-bucket-based-limits-in-batcher' into…
sentrivana Mar 6, 2026
1006e7b
remove unused imports
sentrivana Mar 6, 2026
6c16dbf
Merge branch 'ivana/span-first-7-add-trace-decorator' into ivana/span…
sentrivana Mar 6, 2026
cb37a07
Merge branch 'ivana/span-first-8-bucket-based-limits-in-batcher' into…
sentrivana Mar 6, 2026
ad6e7cc
move where finished is set
sentrivana Mar 6, 2026
ba29f0c
remove finished
sentrivana Mar 6, 2026
d6a42b2
end_timestamp improvements
sentrivana Mar 6, 2026
5e20ad3
.
sentrivana Mar 6, 2026
c70fae4
fix
sentrivana Mar 6, 2026
b995770
simplify
sentrivana Mar 6, 2026
0235053
Merge branch 'master' into ivana/span-first-9-start-end
sentrivana Mar 9, 2026
60217e1
ref: Add warnings to span streaming APIs
sentrivana Mar 9, 2026
b673a09
Merge branch 'master' into ivana/span-first-9-start-end
sentrivana Mar 9, 2026
d6fa965
.
sentrivana Mar 9, 2026
3602f86
.
sentrivana Mar 9, 2026
9f59eb0
fix
sentrivana Mar 9, 2026
bd8e1c9
Merge branch 'ivana/span-first-9-start-end' into ivana/span-first-10-…
sentrivana Mar 9, 2026
9b3df81
Merge branch 'master' into ivana/span-first-10-random-improvements
sentrivana Mar 9, 2026
8614d52
Merge branch 'master' into ivana/span-first-9-start-end
sentrivana Mar 9, 2026
cdee8bc
Merge branch 'ivana/span-first-9-start-end' into ivana/span-first-10-…
sentrivana Mar 9, 2026
72f0968
move
sentrivana Mar 9, 2026
dab1970
add a guard
sentrivana Mar 9, 2026
7daa720
.
sentrivana Mar 9, 2026
b59f3cd
move warnings
sentrivana Mar 9, 2026
2f0dc01
.
sentrivana Mar 9, 2026
dc81637
Merge branch 'ivana/span-first-9-start-end' into ivana/span-first-10-…
sentrivana Mar 9, 2026
bc9f765
Merge branch 'master' into ivana/span-first-9-start-end
sentrivana Mar 9, 2026
45372c1
Merge branch 'ivana/span-first-9-start-end' into ivana/span-first-10-…
sentrivana Mar 9, 2026
c5fcb3e
ref: Add sampling to span first
sentrivana Mar 9, 2026
51342fb
add sample_rate, sample_rate to spans
sentrivana Mar 9, 2026
336b643
order
sentrivana Mar 9, 2026
aba1b50
make private
sentrivana Mar 9, 2026
09b88f0
dont redefine slots
sentrivana Mar 9, 2026
2ac24d3
redundant slots
sentrivana Mar 9, 2026
a9b33a9
Merge branch 'ivana/span-first-9-start-end' into ivana/span-first-10-…
sentrivana Mar 9, 2026
4ba4351
Merge branch 'ivana/span-first-10-random-improvements' into ivana/spa…
sentrivana Mar 9, 2026
5d8c5f3
Merge branch 'master' into ivana/span-first-11-sampling
sentrivana Mar 10, 2026
73e33ea
.
sentrivana Mar 10, 2026
bd9e0a3
add finished to noop span
sentrivana Mar 10, 2026
918609c
.
sentrivana Mar 10, 2026
e850994
improvements
sentrivana Mar 10, 2026
f816c0a
.
sentrivana Mar 10, 2026
62deebf
Add [Tracing] prefix
sentrivana Mar 11, 2026
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
29 changes: 28 additions & 1 deletion sentry_sdk/scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
Baggage,
has_tracing_enabled,
has_span_streaming_enabled,
_make_sampling_decision,
normalize_incoming_data,
PropagationContext,
)
Expand Down Expand Up @@ -1199,6 +1200,21 @@
if parent_span is None:
propagation_context = self.get_active_propagation_context()

sampled, sample_rate, sample_rand, outcome = _make_sampling_decision(
name,
attributes,
self,
)

if sample_rate is not None:
self._update_sample_rate(sample_rate)

if sampled is False:
return NoOpStreamedSpan(
scope=self,
unsampled_reason=outcome,
)

Check warning on line 1216 in sentry_sdk/scope.py

View workflow job for this annotation

GitHub Actions / warden: find-bugs

NoOpStreamedSpan ignores `active` parameter, incorrectly setting itself as scope's active span

When `sampled is False`, `NoOpStreamedSpan` is created with `scope=self` (line 1214) but without passing the `active` parameter. The `NoOpStreamedSpan._start()` method unconditionally sets itself as the scope's active span if a scope is provided, ignoring whether `active=False` was requested by the caller. This causes incorrect span parenting behavior: spans started after a NoOpStreamedSpan with `active=False` will incorrectly become its children instead of being parented to the previous active span.

return StreamedSpan(
name=name,
attributes=attributes,
Expand All @@ -1209,12 +1225,14 @@
parent_span_id=propagation_context.parent_span_id,
parent_sampled=propagation_context.parent_sampled,
baggage=propagation_context.baggage,
sample_rand=sample_rand,
sample_rate=sample_rate,
)

# This is a child span; take propagation context from the parent span
with new_scope():
if isinstance(parent_span, NoOpStreamedSpan):
return NoOpStreamedSpan()
return NoOpStreamedSpan(unsampled_reason=parent_span._unsampled_reason)

return StreamedSpan(
name=name,
Expand All @@ -1227,6 +1245,15 @@
parent_sampled=parent_span.sampled,
)

def _update_sample_rate(self, sample_rate: float) -> None:
# If we had to adjust the sample rate when setting the sampling decision
# for a span, it needs to be updated in the propagation context too
propagation_context = self.get_active_propagation_context()
baggage = propagation_context.baggage

if baggage is not None:
baggage.sentry_items["sample_rate"] = str(sample_rate)

Check warning on line 1255 in sentry_sdk/scope.py

View check run for this annotation

@sentry/warden / warden: code-review

Baggage mutability check missing in _update_sample_rate

The `_update_sample_rate` method modifies `baggage.sentry_items["sample_rate"]` without checking if the baggage is mutable. According to the `Baggage` class documentation (lines 608-611 in tracing_utils.py), callers must verify `baggage.mutable` is `True` before mutating the object. When a baggage is parsed from an incoming header that already contains sentry items, `mutable` is set to `False`. This means in continued traces, the sample rate update would modify an immutable baggage, violating the documented invariant.

def continue_trace(
self,
environ_or_headers: "Dict[str, Any]",
Expand Down
47 changes: 37 additions & 10 deletions sentry_sdk/traces.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,8 @@
"_scope",
"_previous_span_on_scope",
"_baggage",
"_sample_rand",
"_sample_rate",
)

def __init__(
Expand All @@ -238,6 +240,8 @@
parent_span_id: "Optional[str]" = None,
parent_sampled: "Optional[bool]" = None,
baggage: "Optional[Baggage]" = None,
sample_rate: "Optional[float]" = None,
sample_rand: "Optional[float]" = None,
):
self._name: str = name
self._active: bool = active
Expand All @@ -254,6 +258,8 @@
self._parent_span_id = parent_span_id
self._parent_sampled = parent_sampled
self._baggage = baggage
self._sample_rand = sample_rand
self._sample_rate = sample_rate

self._start_timestamp = datetime.now(timezone.utc)
self._timestamp: "Optional[datetime]" = None
Expand Down Expand Up @@ -350,7 +356,9 @@
if isinstance(end_timestamp, datetime):
self._timestamp = end_timestamp
else:
logger.debug("Failed to set end_timestamp. Using current time instead.")
logger.debug(
"[Tracing] Failed to set end_timestamp. Using current time instead."
)

if self._timestamp is None:
try:
Expand Down Expand Up @@ -395,7 +403,7 @@

if status not in {e.value for e in SpanStatus}:
logger.debug(
f'Unsupported span status {status}. Expected one of: "ok", "error"'
f'[Tracing] Unsupported span status {status}. Expected one of: "ok", "error"'
)
return

Expand Down Expand Up @@ -441,13 +449,20 @@


class NoOpStreamedSpan(StreamedSpan):
__slots__ = ()
__slots__ = (
"_finished",
"_unsampled_reason",
)

def __init__(
self,
unsampled_reason: "Optional[str]" = None,
scope: "Optional[sentry_sdk.Scope]" = None,
) -> None:
self._scope = scope # type: ignore[assignment]
self._unsampled_reason = unsampled_reason

self._finished = False

self._start()

Expand All @@ -470,17 +485,29 @@
self._scope.span = self # type: ignore
self._previous_span_on_scope = old_span

def _end(self, end_timestamp: "Optional[Union[float, datetime]]" = None) -> None:
if self._scope is None:
if self._finished:
return

if not hasattr(self, "_previous_span_on_scope"):
return
if self._unsampled_reason is not None:
client = sentry_sdk.get_client()
if client.is_active() and client.transport:
logger.debug(
f"[Tracing] Discarding span because sampled=False (reason: {self._unsampled_reason})"
)
client.transport.record_lost_event(
reason=self._unsampled_reason,
data_category="span",
quantity=1,
)

if self._scope and hasattr(self, "_previous_span_on_scope"):
with capture_internal_exceptions():
old_span = self._previous_span_on_scope
del self._previous_span_on_scope
self._scope.span = old_span

with capture_internal_exceptions():
old_span = self._previous_span_on_scope
del self._previous_span_on_scope
self._scope.span = old_span
self._finished = True

Check warning on line 510 in sentry_sdk/traces.py

View check run for this annotation

@sentry/warden / warden: code-review

NoOpStreamedSpan lacks test coverage for new sampling/client report behavior

The `NoOpStreamedSpan` class has been extended with sampling-related functionality (`unsampled_reason` parameter, client report recording in `_end()`), but there are no tests covering this new behavior. The existing `tests/tracing/test_noop_span.py` tests `NoOpSpan` but not `NoOpStreamedSpan`. Per the review checklist, every PR should have appropriate test coverage for functional changes.

def end(self, end_timestamp: "Optional[Union[float, datetime]]" = None) -> None:
self._end()
Expand Down
86 changes: 86 additions & 0 deletions sentry_sdk/tracing_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
to_string,
try_convert,
is_sentry_url,
is_valid_sample_rate,
_is_external_source,
_is_in_project_root,
_module_in_list,
Expand All @@ -41,6 +42,8 @@

from types import FrameType

from sentry_sdk._types import Attributes


SENTRY_TRACE_REGEX = re.compile(
"^[ \t]*" # whitespace
Expand Down Expand Up @@ -1379,6 +1382,89 @@ def add_sentry_baggage_to_headers(
)


def _make_sampling_decision(
name: str,
attributes: "Optional[Attributes]",
scope: "sentry_sdk.Scope",
) -> "tuple[bool, Optional[float], Optional[float], Optional[str]]":
"""
Decide whether a span should be sampled.

Returns a tuple with:
1. the sampling decision
2. the effective sample rate
3. the sample rand
4. the reason for not sampling the span, if unsampled
"""
client = sentry_sdk.get_client()

if not has_tracing_enabled(client.options):
return False, None, None, None

propagation_context = scope.get_active_propagation_context()

sample_rand = None
if propagation_context.baggage is not None:
sample_rand = propagation_context.baggage._sample_rand()
if sample_rand is None:
sample_rand = _generate_sample_rand(propagation_context.trace_id)

# If there's a traces_sampler, use that; otherwise use traces_sample_rate
traces_sampler_defined = callable(client.options.get("traces_sampler"))
if traces_sampler_defined:
sampling_context = {
"name": name,
"trace_id": propagation_context.trace_id,
"parent_span_id": propagation_context.parent_span_id,
"parent_sampled": propagation_context.parent_sampled,
"attributes": dict(attributes) if attributes else {},
}

sample_rate = client.options["traces_sampler"](sampling_context)
else:
if propagation_context.parent_sampled is not None:
sample_rate = propagation_context.parent_sampled
else:
sample_rate = client.options["traces_sample_rate"]

# Validate whether the sample_rate we got is actually valid. Since
# traces_sampler is user-provided, it could return anything.
if not is_valid_sample_rate(sample_rate, source="Tracing"):
logger.warning(f"[Tracing] Discarding {name} because of invalid sample rate.")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bigger refactor that would happen outside of this PR (if we do decide to do this), but I wonder if we should consider leveraging the extra property within the logger in order to both hold dynamic values like name, but also to include a structured enum reason discard_reason=TraceDiscardReasons.INVALID_SAMPLE_RATE.

If a user is trying to determine if/why spans were dropped, this would help with that effort.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think more info in log messages is always nice, so we can def do something like this in the future.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Made PY-2133 to track this, might be a future maintenance day exploration for me 👀

return False, None, None, "sample_rate"

sample_rate = float(sample_rate)
if not sample_rate:
if traces_sampler_defined:
reason = "traces_sampler returned 0 or False"
else:
reason = "traces_sample_rate is set to 0"

logger.debug(f"[Tracing] Discarding {name} because {reason}")
return False, 0.0, None, "sample_rate"

# Adjust sample rate if we're under backpressure
if client.monitor:
sample_rate /= 2**client.monitor.downsample_factor

if not sample_rate:
logger.debug(f"[Tracing] Discarding {name} because backpressure")
return False, 0.0, None, "backpressure"

sampled = sample_rand < sample_rate

if sampled:
logger.debug(f"[Tracing] Starting {name}")
outcome = None
else:
logger.debug(
f"[Tracing] Discarding {name} because it's not included in the random sample (sampling rate = {sample_rate})"
)
outcome = "sample_rate"

return sampled, sample_rate, sample_rand, outcome


# Circular imports
from sentry_sdk.tracing import (
BAGGAGE_HEADER_NAME,
Expand Down
Loading