diff --git a/octomachinery/github/api/app_client.py b/octomachinery/github/api/app_client.py index 48e974f..e5b2c37 100644 --- a/octomachinery/github/api/app_client.py +++ b/octomachinery/github/api/app_client.py @@ -9,6 +9,7 @@ from aiohttp.client import ClientSession from aiohttp.client_exceptions import ClientConnectorError import attr +import sentry_sdk # pylint: disable=relative-beyond-top-level from ...routing import WEBHOOK_EVENTS_ROUTER @@ -50,6 +51,14 @@ class GitHubApp: converter=frozenset, ) + def __attrs_post_init__(self) -> None: # pylint: disable=no-self-use + """Initialize the Sentry SDK library.""" + # NOTE: Under the hood, it will set up the DSN from `SENTRY_DSN` + # NOTE: env var. We don't need to care about it not existing as + # NOTE: Sentry SDK helpers don't fail loudly and if not + # NOTE: configured, it'll be ignored. + sentry_sdk.init() + async def dispatch_event(self, github_event: GitHubEvent) -> Iterable[Any]: """Dispatch ``github_event`` into the embedded routers.""" return await github_event.dispatch_via( diff --git a/octomachinery/routing/webhooks_dispatcher.py b/octomachinery/routing/webhooks_dispatcher.py index 63550c0..28a6570 100644 --- a/octomachinery/routing/webhooks_dispatcher.py +++ b/octomachinery/routing/webhooks_dispatcher.py @@ -6,6 +6,7 @@ import logging from anyio import get_cancelled_exc_class, sleep as async_sleep +import sentry_sdk # pylint: disable=relative-beyond-top-level,import-error from ..runtime.context import RUNTIME_CONTEXT @@ -88,6 +89,12 @@ async def route_github_event( except get_cancelled_exc_class(): raise except Exception as exc: # pylint: disable=broad-except + # NOTE: It's probably better to wrap each event handler with + # NOTE: try/except and call `capture_exception()` there instead. + # NOTE: We'll also need to figure out the magic of associating + # NOTE: breadcrumbs with event handlers. + sentry_sdk.capture_exception(exc) + # NOTE: Framework-wise, these exceptions are meaningless because they # NOTE: can be anything random that the webhook author (octomachinery # NOTE: end-user) forgot to handle. There's nothing we can do about diff --git a/setup.cfg b/setup.cfg index 2dae20e..fdf7aef 100644 --- a/setup.cfg +++ b/setup.cfg @@ -95,6 +95,7 @@ install_requires = gidgethub pyjwt[crypto] pyyaml + sentry_sdk setuptools_scm [options.packages.find]