Skip to content

Commit 937964c

Browse files
committed
Add sample_rate property and fix test_monitor
1 parent aa9c5ca commit 937964c

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

sentry_sdk/tracing.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1403,6 +1403,19 @@ def sampled(self):
14031403
# type: () -> Optional[bool]
14041404
return self._otel_span.get_span_context().trace_flags.sampled
14051405

1406+
@property
1407+
def sample_rate(self):
1408+
# type: () -> Optional[float]
1409+
from sentry_sdk.integrations.opentelemetry.consts import (
1410+
TRACESTATE_SAMPLE_RATE_KEY,
1411+
)
1412+
1413+
sample_rate = self._otel_span.get_span_context().trace_state.get(
1414+
TRACESTATE_SAMPLE_RATE_KEY
1415+
)
1416+
sample_rate = cast("Optional[str]", sample_rate)
1417+
return float(sample_rate) if sample_rate is not None else None
1418+
14061419
@property
14071420
def op(self):
14081421
# type: () -> Optional[str]

tests/test_monitor.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def test_monitor_unhealthy(sentry_init):
5555
assert monitor.downsample_factor == (i + 1 if i < 10 else 10)
5656

5757

58-
def test_transaction_uses_downsample_rate(
58+
def test_root_span_uses_downsample_rate(
5959
sentry_init, capture_envelopes, capture_record_lost_event_calls, monkeypatch
6060
):
6161
sentry_init(
@@ -78,16 +78,14 @@ def test_transaction_uses_downsample_rate(
7878
assert monitor.is_healthy() is False
7979
assert monitor.downsample_factor == 1
8080

81-
with sentry_sdk.start_transaction(name="foobar") as transaction:
81+
with sentry_sdk.start_span(name="foobar") as root_span:
8282
with sentry_sdk.start_span(name="foospan"):
8383
with sentry_sdk.start_span(name="foospan2"):
8484
with sentry_sdk.start_span(name="foospan3"):
8585
...
8686

87-
assert transaction.sampled is False
88-
assert (
89-
transaction.sample_rate == 0.5
90-
) # TODO: this fails until we put the sample_rate in the POTelSpan
87+
assert root_span.sampled is False
88+
assert root_span.sample_rate == 0.5
9189

9290
assert len(envelopes) == 0
9391

@@ -104,7 +102,7 @@ def test_transaction_uses_downsample_rate(
104102
"span",
105103
None,
106104
1,
107-
), # Only one span (the transaction itself) is counted, since we did not record any spans in the first place.
105+
), # Only one span (the root span itself) is counted, since we did not record any spans in the first place.
108106
]
109107
)
110108

0 commit comments

Comments
 (0)