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

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
32 changes: 30 additions & 2 deletions sentry_sdk/tracing.py
Expand Up @@ -695,8 +695,31 @@ 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, like so:

with sentry_sdk.start_transaction(transaction):
# code here
"""
)
Comment on lines +713 to +721
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Log messages should not be multi-line because it makes it hard or impossible to grep through the logs (or do some other line based processing)

Please change the log message to be one line.


super().__enter__()

if self._profile is not None:
Expand Down Expand Up @@ -747,9 +770,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
59 changes: 59 additions & 0 deletions tests/tracing/test_misc.py
Expand Up @@ -362,3 +362,62 @@ 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"
)


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, like so:

with sentry_sdk.start_transaction(transaction):
# code here
"""
Comment on lines +417 to +422
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
The transaction will not be sent to Sentry. To fix, start the transaction by
passing it to sentry_sdk.start_transaction, like so:
with sentry_sdk.start_transaction(transaction):
# code here
"""
The transaction will not be sent to Sentry. To fix, start the transaction by
passing it to sentry_sdk.start_transaction, like so:
with sentry_sdk.start_transaction(transaction):
# code here
"""

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This spacing is intentional; it makes the message appear nicely in the logs, like so:

 [sentry] 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, like so:

                   with sentry_sdk.start_transaction(transaction):
                       # code here            

)