|
6 | 6 |
|
7 | 7 | import sentry_sdk |
8 | 8 | from sentry_sdk import start_span, start_transaction, capture_exception |
9 | | -from sentry_sdk.tracing import Transaction |
10 | 9 | from sentry_sdk.utils import logger |
11 | 10 |
|
12 | 11 |
|
13 | | -def test_sampling_decided_only_for_transactions(sentry_init, capture_events): |
| 12 | +def test_sampling_decided_only_for_root_spans(sentry_init): |
14 | 13 | sentry_init(traces_sample_rate=0.5) |
15 | 14 |
|
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 |
18 | 17 |
|
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 |
21 | 20 |
|
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 |
24 | 23 |
|
25 | 24 |
|
26 | 25 | @pytest.mark.parametrize("sampled", [True, False]) |
@@ -185,28 +184,21 @@ def test_inherits_parent_sampling_decision_when_traces_sampler_undefined( |
185 | 184 | def test_passes_parent_sampling_decision_in_sampling_context( |
186 | 185 | sentry_init, parent_sampling_decision |
187 | 186 | ): |
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) |
189 | 192 |
|
190 | 193 | sentry_trace_header = ( |
191 | 194 | "12312012123120121231201212312012-1121201211212012-{sampled}".format( |
192 | 195 | sampled=int(parent_sampling_decision) |
193 | 196 | ) |
194 | 197 | ) |
195 | 198 |
|
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 |
210 | 202 |
|
211 | 203 |
|
212 | 204 | def test_passes_attributes_from_start_span_to_traces_sampler( |
|
0 commit comments