Skip to content

Fix transaction discarded debug messages #3002

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

Merged
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
9 changes: 7 additions & 2 deletions sentry_sdk/tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -747,9 +747,14 @@ def finish(self, hub=None, end_timestamp=None):
# We have no active client and therefore nowhere to send this transaction.
return None

# This is a de facto proxy for checking if sampled = False
if self._span_recorder is None:
logger.debug("Discarding transaction because sampled = False")
# Explicit check against False needed because self.sampled might be None
if self.sampled is False:
logger.debug("Discarding transaction because sampled = False")
else:
logger.debug(
"Discarding transaction because it was not started with sentry_sdk.start_transaction"
)

# This is not entirely accurate because discards here are not
# exclusively based on sample rate but also traces sampler, but
Expand Down
39 changes: 39 additions & 0 deletions tests/tracing/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,3 +362,42 @@ def test_start_transaction_updates_scope_name_source(sentry_init):
with start_transaction(name="foobar", source="route"):
assert scope._transaction == "foobar"
assert scope._transaction_info == {"source": "route"}


@pytest.mark.parametrize("sampled", (True, None))
def test_transaction_dropped_debug_not_started(sentry_init, sampled):
sentry_init(enable_tracing=True)

tx = Transaction(sampled=sampled)

with mock.patch("sentry_sdk.tracing.logger") as mock_logger:
with tx:
pass

mock_logger.debug.assert_any_call(
"Discarding transaction because it was not started with sentry_sdk.start_transaction"
)

with pytest.raises(AssertionError):
# We should NOT see the "sampled = False" message here
mock_logger.debug.assert_any_call(
"Discarding transaction because sampled = False"
)


def test_transaction_dropeed_sampled_false(sentry_init):
sentry_init(enable_tracing=True)

tx = Transaction(sampled=False)

with mock.patch("sentry_sdk.tracing.logger") as mock_logger:
with sentry_sdk.start_transaction(tx):
pass

mock_logger.debug.assert_any_call("Discarding transaction because sampled = False")

with pytest.raises(AssertionError):
# We should not see the "not started" message here
mock_logger.debug.assert_any_call(
"Discarding transaction because it was not started with sentry_sdk.start_transaction"
)