Skip to content

Commit

Permalink
Add sentry-sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
mejroslav committed Aug 11, 2023
1 parent 1de49bc commit d46047e
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
39 changes: 39 additions & 0 deletions asab/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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:
"""
Expand Down
7 changes: 7 additions & 0 deletions asab/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}

}
Expand Down

0 comments on commit d46047e

Please sign in to comment.