-
Notifications
You must be signed in to change notification settings - Fork 563
fix: Selective disabling option for N+1 warnings #4920
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
Mark expected N+1 loops | ||
====================== | ||
|
||
The SDK provides a small helper to mark transactions or spans where an N+1 loop is | ||
expected and acceptable. | ||
|
||
.. code-block:: python | ||
|
||
from sentry_sdk.performance import allow_n_plus_one | ||
|
||
with sentry_sdk.start_transaction(name="process_items"): | ||
with allow_n_plus_one("expected batch processing"): | ||
for item in items: | ||
process(item) | ||
|
||
Notes | ||
----- | ||
|
||
- This helper sets the tag ``sentry.n_plus_one.ignore`` (and optional | ||
``sentry.n_plus_one.reason``) on the current transaction and current span. | ||
- Server-side support is required for the N+1 detector to actually ignore | ||
transactions with this tag. The SDK only attaches the metadata. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
from contextlib import contextmanager | ||
|
||
# Import the helper that returns the current active span/transaction without | ||
# importing the top-level package to avoid circular imports. | ||
from sentry_sdk.tracing_utils import get_current_span | ||
|
||
|
||
@contextmanager | ||
def allow_n_plus_one(reason=None): | ||
"""Context manager to mark the current span and its root transaction as | ||
intentionally allowed N+1. | ||
|
||
This sets tags on the active span and its containing transaction so that | ||
server-side N+1 detectors (if updated to honor these tags) can ignore the | ||
transaction. This helper is best-effort and will not raise if there is no | ||
active span/transaction. | ||
|
||
Usage: | ||
with allow_n_plus_one("expected loop"): | ||
for x in queryset: | ||
... | ||
""" | ||
span = get_current_span() | ||
if span is not None: | ||
try: | ||
# Tag the active span | ||
span.set_tag("sentry.n_plus_one.ignore", True) | ||
if reason: | ||
span.set_tag("sentry.n_plus_one.reason", reason) | ||
|
||
# Also tag the containing transaction if available | ||
try: | ||
tx = span.containing_transaction | ||
except Exception: | ||
tx = None | ||
|
||
if tx is not None: | ||
try: | ||
tx.set_tag("sentry.n_plus_one.ignore", True) | ||
if reason: | ||
tx.set_tag("sentry.n_plus_one.reason", reason) | ||
except Exception: | ||
# best-effort: do not fail if transaction tagging fails | ||
pass | ||
except Exception: | ||
# best-effort: silence any unexpected errors | ||
pass | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bug: Circular Import and Incorrect DocumentationA circular import exists between |
||
|
||
try: | ||
yield | ||
finally: | ||
# keep tags; no cleanup required | ||
pass |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import sentry_sdk | ||
from sentry_sdk.performance import allow_n_plus_one | ||
|
||
|
||
def test_allow_n_plus_one_sets_tag(sentry_init): | ||
# Initialize SDK with test fixture | ||
sentry_init() | ||
|
||
with sentry_sdk.start_transaction(name="tx") as tx: | ||
with allow_n_plus_one("expected"): | ||
# no-op loop simulated | ||
pass | ||
|
||
# The tag should be set on the transaction and the active span | ||
assert tx._tags.get("sentry.n_plus_one.ignore") is True | ||
assert tx._tags.get("sentry.n_plus_one.reason") == "expected" | ||
|
||
# if a span was active, it should have been tagged as well; start a span | ||
# to verify tagging of the active span | ||
with sentry_sdk.start_span(op="db", name="q") as span: | ||
with allow_n_plus_one("inner"): | ||
pass | ||
|
||
assert span._tags.get("sentry.n_plus_one.ignore") is True | ||
assert span._tags.get("sentry.n_plus_one.reason") == "inner" |
Uh oh!
There was an error while loading. Please reload this page.