Skip to content
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

Add sample_rate property and fix test_monitor #3790

Merged
merged 1 commit into from
Nov 18, 2024
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
13 changes: 13 additions & 0 deletions sentry_sdk/tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1403,6 +1403,19 @@ def sampled(self):
# type: () -> Optional[bool]
return self._otel_span.get_span_context().trace_flags.sampled

@property
def sample_rate(self):
# type: () -> Optional[float]
from sentry_sdk.integrations.opentelemetry.consts import (
TRACESTATE_SAMPLE_RATE_KEY,
)

sample_rate = self._otel_span.get_span_context().trace_state.get(
TRACESTATE_SAMPLE_RATE_KEY
)
sample_rate = cast("Optional[str]", sample_rate)
return float(sample_rate) if sample_rate is not None else None

@property
def op(self):
# type: () -> Optional[str]
Expand Down
12 changes: 5 additions & 7 deletions tests/test_monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def test_monitor_unhealthy(sentry_init):
assert monitor.downsample_factor == (i + 1 if i < 10 else 10)


def test_transaction_uses_downsample_rate(
def test_root_span_uses_downsample_rate(
sentry_init, capture_envelopes, capture_record_lost_event_calls, monkeypatch
):
sentry_init(
Expand All @@ -78,16 +78,14 @@ def test_transaction_uses_downsample_rate(
assert monitor.is_healthy() is False
assert monitor.downsample_factor == 1

with sentry_sdk.start_transaction(name="foobar") as transaction:
with sentry_sdk.start_span(name="foobar") as root_span:
with sentry_sdk.start_span(name="foospan"):
with sentry_sdk.start_span(name="foospan2"):
with sentry_sdk.start_span(name="foospan3"):
...

assert transaction.sampled is False
assert (
transaction.sample_rate == 0.5
) # TODO: this fails until we put the sample_rate in the POTelSpan
assert root_span.sampled is False
assert root_span.sample_rate == 0.5

assert len(envelopes) == 0

Expand All @@ -104,7 +102,7 @@ def test_transaction_uses_downsample_rate(
"span",
None,
1,
), # Only one span (the transaction itself) is counted, since we did not record any spans in the first place.
), # Only one span (the root span itself) is counted, since we did not record any spans in the first place.
]
)

Expand Down
Loading