From d46047eac29508e4b415fd26abc7f9339781b4cf Mon Sep 17 00:00:00 2001 From: mejroslav Date: Fri, 11 Aug 2023 13:42:28 +0200 Subject: [PATCH] Add sentry-sdk --- asab/application.py | 39 +++++++++++++++++++++++++++++++++++++++ asab/config.py | 7 +++++++ 2 files changed, 46 insertions(+) diff --git a/asab/application.py b/asab/application.py index 20c911127..c13379962 100644 --- a/asab/application.py +++ b/asab/application.py @@ -190,6 +190,10 @@ def handler(type): # Every 10 minutes listen for housekeeping self.PubSub.subscribe("Application.tick/600!", self._on_housekeeping_tick) + # Sentry.io + + if asab.Config.getboolean("sentry", "enabled", False): + self._initialize_sentry() def create_argument_parser( self, @@ -799,6 +803,41 @@ def _on_housekeeping_tick(self, message_type): "missed_housekeeping_events": self.HousekeepingMissedEvents }) + # Sentry.io + + def _initialize_sentry(self): + + try: + import sentry_sdk + import sentry_sdk.integrations.aiohttp + import sentry_sdk.integrations.asyncio + import sentry_sdk.integrations.logging + except ModuleNotFoundError: + L.critical("Package for Sentry SDK not found. Install it with: pip install sentry-sdk") + sys.exit(1) + + self.Dsn = asab.Config.get("sentry", "dsn") + self.Environment = asab.Config.get("sentry", "environment") + self.TracesSampleRate = asab.Config.getfloat("sentry", "traces_sample_rate") + assert 0 <= self.TracesSampleRate <= 1.0, "Traces sample rate must be between 0 and 1." + + _ = sentry_sdk.integrations.logging.LoggingIntegration( + level=logging.INFO, # Capture info and above as breadcrumbs + event_level=logging.WARNING, # Send errors as events + ) + + sentry_sdk.init( + dsn=self.Dsn, + integrations=[ + sentry_sdk.integrations.aiohttp.AioHttpIntegration(), + sentry_sdk.integrations.asyncio.AsyncioIntegration(), + sentry_sdk.integrations.logging.LoggingIntegration(), + ], + traces_sample_rate=self.TracesSampleRate, + environment=self.Environment + ) + + L.info("Sentry SDK initialized!") def _housekeeping_id(dt: datetime.datetime) -> int: """ diff --git a/asab/config.py b/asab/config.py index 45efef720..ce9e51e43 100644 --- a/asab/config.py +++ b/asab/config.py @@ -106,6 +106,13 @@ class ConfigParser(configparser.ConfigParser): "at": "03:00", "limit": "05:00", "run_at_startup": "no", + }, + + "sentry": { + "enabled": "false", + "dsn": "", + "environment": "develop", + "traces_sample_rate": 1.0, } }