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

Warn when transaction entered without calling start_transaction #3003

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
19 changes: 19 additions & 0 deletions sentry_sdk/tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -714,8 +714,27 @@ def __repr__(self):
)
)

def _possibly_started(self):
# type: () -> bool
"""Returns whether the transaction might have been started.

If this returns False, we know that the transaction was not started
with sentry_sdk.start_transaction, and therefore the transaction will
be discarded.
"""

# We must explicitly check self.sampled is False since self.sampled can be None
return self._span_recorder is not None or self.sampled is False
antonpirker marked this conversation as resolved.
Show resolved Hide resolved

def __enter__(self):
# type: () -> Transaction
if not self._possibly_started():
logger.warning(
"Transaction was entered without being started with sentry_sdk.start_transaction."
"The transaction will not be sent to Sentry. To fix, start the transaction by"
"passing it to sentry_sdk.start_transaction."
)

super().__enter__()

if self._profile is not None:
Expand Down
16 changes: 16 additions & 0 deletions tests/tracing/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,3 +401,19 @@ def test_transaction_dropeed_sampled_false(sentry_init):
mock_logger.debug.assert_any_call(
"Discarding transaction because it was not started with sentry_sdk.start_transaction"
)


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

tx = Transaction()

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

mock_logger.warning.assert_any_call(
"Transaction was entered without being started with sentry_sdk.start_transaction."
"The transaction will not be sent to Sentry. To fix, start the transaction by"
"passing it to sentry_sdk.start_transaction."
)
Loading