Skip to content

Commit e8c1813

Browse files
authored
Remove sampled setter and fix sanic behavior with an event processor (#3779)
1 parent 0891703 commit e8c1813

File tree

4 files changed

+26
-17
lines changed

4 files changed

+26
-17
lines changed

MIGRATION_GUIDE.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Looking to upgrade from Sentry SDK 2.x to 3.x? Here's a comprehensive list of wh
1111
- The SDK now supports Python 3.7 and higher.
1212
- `sentry_sdk.start_span` now only takes keyword arguments.
1313
- `sentry_sdk.start_transaction`/`sentry_sdk.start_span` no longer takes the following arguments: `span`, `parent_sampled`, `trace_id`, `span_id` or `parent_span_id`.
14+
- You can no longer change the sampled status of a span with `span.sampled = False` after starting it.
1415
- The `Span()` constructor does not accept a `hub` parameter anymore.
1516
- `Span.finish()` does not accept a `hub` parameter anymore.
1617
- The `Profile()` constructor does not accept a `hub` parameter anymore.

sentry_sdk/integrations/sanic.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,11 @@ async def _context_enter(request):
187187
return
188188

189189
weak_request = weakref.ref(request)
190-
request.ctx._sentry_scope = sentry_sdk.isolation_scope()
191-
scope = request.ctx._sentry_scope.__enter__()
190+
request.ctx._sentry_scope_manager = sentry_sdk.isolation_scope()
191+
scope = request.ctx._sentry_scope_manager.__enter__()
192+
request.ctx._sentry_scope = scope
193+
194+
scope.set_transaction_name(request.path, TRANSACTION_SOURCE_URL)
192195
scope.clear_breadcrumbs()
193196
scope.add_event_processor(_make_request_processor(weak_request))
194197

@@ -197,7 +200,7 @@ async def _context_enter(request):
197200
dict(request.headers)
198201
)
199202
request.ctx._sentry_continue_trace.__enter__()
200-
request.ctx._sentry_transaction = sentry_sdk.start_transaction(
203+
request.ctx._sentry_transaction = sentry_sdk.start_span(
201204
op=OP.HTTP_SERVER,
202205
# Unless the request results in a 404 error, the name and source will get overwritten in _set_transaction
203206
name=request.path,
@@ -220,14 +223,20 @@ async def _context_exit(request, response=None):
220223
# happens while trying to end the transaction, we still attempt to exit the scope.
221224
with capture_internal_exceptions():
222225
request.ctx._sentry_transaction.set_http_status(response_status)
223-
request.ctx._sentry_transaction.sampled &= (
226+
227+
if (
224228
isinstance(integration, SanicIntegration)
225-
and response_status not in integration._unsampled_statuses
226-
)
229+
and response_status in integration._unsampled_statuses
230+
):
231+
# drop the event in an event processor
232+
request.ctx._sentry_scope.add_event_processor(
233+
lambda _event, _hint: None
234+
)
235+
227236
request.ctx._sentry_transaction.__exit__(None, None, None)
228237
request.ctx._sentry_continue_trace.__exit__(None, None, None)
229238

230-
request.ctx._sentry_scope.__exit__(None, None, None)
239+
request.ctx._sentry_scope_manager.__exit__(None, None, None)
231240

232241

233242
async def _set_transaction(request, route, **_):

sentry_sdk/tracing.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1231,7 +1231,9 @@ def __init__(
12311231
skip_span = False
12321232
if only_if_parent:
12331233
parent_span_context = get_current_span().get_span_context()
1234-
skip_span = not parent_span_context.is_valid or parent_span_context.is_remote
1234+
skip_span = (
1235+
not parent_span_context.is_valid or parent_span_context.is_remote
1236+
)
12351237

12361238
if skip_span:
12371239
self._otel_span = INVALID_SPAN
@@ -1401,11 +1403,6 @@ def sampled(self):
14011403
# type: () -> Optional[bool]
14021404
return self._otel_span.get_span_context().trace_flags.sampled
14031405

1404-
@sampled.setter
1405-
def sampled(self, value):
1406-
# type: (Optional[bool]) -> None
1407-
pass
1408-
14091406
@property
14101407
def op(self):
14111408
# type: () -> Optional[str]

tests/integrations/sanic/test_sanic.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -346,8 +346,9 @@ def __init__(
346346
expected_status,
347347
expected_transaction_name,
348348
expected_source=None,
349+
has_transaction_event=True,
349350
):
350-
# type: (Iterable[Optional[Container[int]]], str, int, Optional[str], Optional[str]) -> None
351+
# type: (Iterable[Optional[Container[int]]], str, int, Optional[str], Optional[str], bool) -> None
351352
"""
352353
expected_transaction_name of None indicates we expect to not receive a transaction
353354
"""
@@ -356,6 +357,7 @@ def __init__(
356357
self.expected_status = expected_status
357358
self.expected_transaction_name = expected_transaction_name
358359
self.expected_source = expected_source
360+
self.has_transaction_event = has_transaction_event
359361

360362

361363
@pytest.mark.skipif(
@@ -386,6 +388,7 @@ def __init__(
386388
url="/404",
387389
expected_status=404,
388390
expected_transaction_name=None,
391+
has_transaction_event=False,
389392
),
390393
TransactionTestConfig(
391394
# With no ignored HTTP statuses, we should get transactions for 404 errors
@@ -401,6 +404,7 @@ def __init__(
401404
url="/message",
402405
expected_status=200,
403406
expected_transaction_name=None,
407+
has_transaction_event=False,
404408
),
405409
],
406410
)
@@ -430,9 +434,7 @@ def test_transactions(test_config, sentry_init, app, capture_events):
430434
(transaction_event, *_) = [*transaction_events, None]
431435

432436
# We should have no transaction event if and only if we expect no transactions
433-
assert (transaction_event is None) == (
434-
test_config.expected_transaction_name is None
435-
)
437+
assert bool(transaction_event) == test_config.has_transaction_event
436438

437439
# If a transaction was expected, ensure it is correct
438440
assert (

0 commit comments

Comments
 (0)