Skip to content

Commit

Permalink
Merge branch 'feature/sentry' of github.com:TeskaLabs/asab into featu…
Browse files Browse the repository at this point in the history
…re/sentry
  • Loading branch information
mejroslav committed Aug 11, 2023
2 parents 1de49bc + 3f260a3 commit 42f42e8
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"):
self._initialize_sentry()


def create_argument_parser(
self,
Expand Down Expand Up @@ -800,6 +804,41 @@ def _on_housekeeping_tick(self, message_type):
})


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:
"""
Create a unique ID for each date. Utility function for housekeeping.
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": "",
"traces_sample_rate": 1.0,
"environment": "develop",
}

}
Expand Down

0 comments on commit 42f42e8

Please sign in to comment.