"""Sentry SDK helper methods.""" from typing import Optional from sentry_sdk.types import Event, Hint from pdp import config def sentry_ignore_loggers( event: Event, hint: Hint, ) -> Optional[Event]: """Ignore error logs for modules listed in pdp.config.IGNORED_SENTRY_LOGGERS. Use this method as the `before_send` hook in sentry_sdk.init. import sentry_sdk sentry_sdk.init( # ... before_send=sentry_ignore_loggers ) """ if not config.IGNORED_SENTRY_LOGGERS: # return immediately if the config is empty return event if event and event.get("logger"): # FUTURE WORK: # Use a prefix tree if IGNORED_SENTRY_LOGGERS grows too large # instead of looping over entries. for ignored_name in config.IGNORED_SENTRY_LOGGERS: if event["logger"].startswith(ignored_name): return None return event