Skip to content

Commit 3fcbf65

Browse files
committed
tests
1 parent cefb27c commit 3fcbf65

File tree

2 files changed

+26
-29
lines changed

2 files changed

+26
-29
lines changed

sentry_sdk/integrations/opentelemetry/sampler.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -141,24 +141,29 @@ def should_sample(
141141
# Check if there is a traces_sampler
142142
# Traces_sampler is responsible to check parent sampled to have full transactions.
143143
has_traces_sampler = callable(client.options.get("traces_sampler"))
144+
custom_parent_sampled = attributes.get(
145+
SentrySpanAttribute.CUSTOM_PARENT_SAMPLED
146+
)
147+
if custom_parent_sampled is not None:
148+
parent_sampled = custom_parent_sampled
149+
else:
150+
parent_sampled = get_parent_sampled(parent_span_context, trace_id)
151+
print("custom_parent_sampled", custom_parent_sampled)
152+
print("get parent sampled", get_parent_sampled(parent_span_context, trace_id))
153+
144154
if is_root_span and has_traces_sampler:
145155
sampling_context = {
146156
"transaction_context": {
147157
"name": name,
148158
"op": attributes.get(SentrySpanAttribute.OP),
149159
},
150-
"parent_sampled": get_parent_sampled(parent_span_context, trace_id),
160+
"parent_sampled": parent_sampled,
151161
}
152162
sampling_context.update(attributes)
153163
sample_rate = client.options["traces_sampler"](sampling_context)
154164

155165
else:
156166
# Check if there is a parent with a sampling decision
157-
parent_sampled = attributes.get(SentrySpanAttribute.CUSTOM_PARENT_SAMPLED)
158-
159-
if parent_sampled is None:
160-
parent_sampled = get_parent_sampled(parent_span_context, trace_id)
161-
162167
if parent_sampled is not None:
163168
sample_rate = parent_sampled
164169
else:

tests/tracing/test_sampling.py

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,20 @@
66

77
import sentry_sdk
88
from sentry_sdk import start_span, start_transaction, capture_exception
9-
from sentry_sdk.tracing import Transaction
109
from sentry_sdk.utils import logger
1110

1211

13-
def test_sampling_decided_only_for_transactions(sentry_init, capture_events):
12+
def test_sampling_decided_only_for_root_spans(sentry_init):
1413
sentry_init(traces_sample_rate=0.5)
1514

16-
with start_transaction(name="hi") as transaction:
17-
assert transaction.sampled is not None
15+
with start_span(name="outer1") as root_span1:
16+
assert root_span1.sampled is not None
1817

19-
with start_span() as span:
20-
assert span.sampled == transaction.sampled
18+
with start_span(name="inner") as span:
19+
assert span.sampled == root_span1.sampled
2120

22-
with start_span() as span:
23-
assert span.sampled is None
21+
with start_span(name="outer2") as root_span2:
22+
assert root_span2.sampled is not None
2423

2524

2625
@pytest.mark.parametrize("sampled", [True, False])
@@ -185,28 +184,21 @@ def test_inherits_parent_sampling_decision_when_traces_sampler_undefined(
185184
def test_passes_parent_sampling_decision_in_sampling_context(
186185
sentry_init, parent_sampling_decision
187186
):
188-
sentry_init(traces_sample_rate=1.0)
187+
def dummy_traces_sampler(sampling_context):
188+
assert sampling_context["parent_sampled"] is parent_sampling_decision
189+
return 1.0
190+
191+
sentry_init(traces_sample_rate=1.0, traces_sampler=dummy_traces_sampler)
189192

190193
sentry_trace_header = (
191194
"12312012123120121231201212312012-1121201211212012-{sampled}".format(
192195
sampled=int(parent_sampling_decision)
193196
)
194197
)
195198

196-
transaction = Transaction.continue_from_headers(
197-
headers={"sentry-trace": sentry_trace_header}, name="dogpark"
198-
)
199-
spy = mock.Mock(wraps=transaction)
200-
start_transaction(transaction=spy)
201-
202-
# there's only one call (so index at 0) and kwargs are always last in a call
203-
# tuple (so index at -1)
204-
sampling_context = spy._set_initial_sampling_decision.mock_calls[0][-1][
205-
"sampling_context"
206-
]
207-
assert "parent_sampled" in sampling_context
208-
# because we passed in a spy, attribute access requires unwrapping
209-
assert sampling_context["parent_sampled"]._mock_wraps is parent_sampling_decision
199+
with sentry_sdk.continue_trace({"sentry-trace": sentry_trace_header}):
200+
with sentry_sdk.start_span(name="dogpark"):
201+
pass
210202

211203

212204
def test_passes_attributes_from_start_span_to_traces_sampler(

0 commit comments

Comments
 (0)