Source code for ferrosoft.container.sentry
"""
Sentry SDK integration utilities for the Ferrosoft Platform.
This module provides ``before_send_transaction`` callbacks for use with
:func:`sentry_sdk.init`. These callbacks allow fine-grained control over
which performance traces are forwarded to Sentry, preventing high-traffic but
low-diagnostic-value endpoints from dominating the performance dashboard.
Note:
:func:`filter_transactions` is also re-implemented inline inside
:mod:`ferrosoft.container.settings` for the production Sentry
initialisation. This standalone copy exists for independent unit testing
and potential reuse in other entry-points.
"""
from urllib.parse import urlparse
[docs]
def filter_transactions(event, hint):
"""Drop Sentry transaction events for high-traffic, low-signal paths.
Intended to be passed as the ``before_send_transaction`` callback when
calling :func:`sentry_sdk.init`. Discards transaction events whose
request path is either the application root (``/``) or the login page
(``/accounts/login/``). All other paths are forwarded to Sentry
unchanged.
The rationale for filtering these two paths is that they are hit
constantly by health checks, redirects, and unauthenticated visitors, so
recording a performance trace for every hit would produce noise that
obscures genuine latency regressions in application paths.
Args:
event (dict): The Sentry transaction event dictionary. Must contain a
``"request"`` key whose value has a ``"url"`` sub-key with the
fully-qualified request URL.
hint (dict): Additional context provided by the Sentry SDK (e.g. the
original exception). Not inspected by this filter.
Returns:
dict | None: The unmodified ``event`` to forward it to Sentry, or
``None`` to discard it silently.
Example:
Pass this function when initialising the Sentry SDK::
import sentry_sdk
from ferrosoft.container.sentry import filter_transactions
sentry_sdk.init(
dsn="https://...",
before_send_transaction=filter_transactions,
)
"""
parsed_url = urlparse(event["request"]["url"])
if parsed_url.path == "/accounts/login/" or parsed_url.path == "/":
return None
return event