Skip to content

Commit

Permalink
Add sentry listener for the webapp
Browse files Browse the repository at this point in the history
  • Loading branch information
jschaff committed Dec 2, 2024
1 parent 1d2a4bc commit 8aa944a
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 2 deletions.
14 changes: 12 additions & 2 deletions biothings_annotator/application/cli/target.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@

from sanic import Sanic

from biothings_annotator.application.views import build_routes
from biothings_annotator.application.middleware import build_middleware
from biothings_annotator.application.exceptions import build_exception_handers
from biothings_annotator.application.listeners import build_listeners
from biothings_annotator.application.middleware import build_middleware
from biothings_annotator.application.views import build_routes

logging.basicConfig()
logger = logging.getLogger("sanic-application")
Expand Down Expand Up @@ -85,6 +86,15 @@ class Sanic(
logger.error("Unable to add middleware %s", middleware)
raise gen_exc

application_listeners = build_listeners()
for listener in application_listeners:
try:
application.register_listener(**listener)
except Exception as gen_exc:
logger.exception(gen_exc)
logger.error("Unable to add listener %s", listener)
raise gen_exc

exception_handlers = build_exception_handers()
for exception_handler in exception_handlers:
try:
Expand Down
3 changes: 3 additions & 0 deletions biothings_annotator/application/configuration/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
"REQUEST_MAX_SIZE": 100000000,
"CACHE_MAX_AGE": 604800
},
"sentry": {
"SENTRY_CLIENT_KEY": ""
},
"extension": {
"openapi": {
"OAS": true,
Expand Down
13 changes: 13 additions & 0 deletions biothings_annotator/application/listeners/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from typing import Dict, List

from .sentry import initialize_sentry


def build_listeners() -> List[Dict]:
"""
Basic method for aggregating all of listeners created
for the annotator service
"""
sentry_listener = {"listener": initialize_sentry, "event": "before_server_start", "priority": 0}
listener_collection = [sentry_listener]
return listener_collection
39 changes: 39 additions & 0 deletions biothings_annotator/application/listeners/sentry.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
"""
Listener for configuring the sentry support for the
sanic web-app associated with the annotator service
"""

import sanic

import sentry_sdk
from sentry_sdk.integrations.asyncio import AsyncioIntegration
from sentry_sdk.integrations.sanic import SanicIntegration


async def initialize_sentry(application_instance: sanic.Sanic) -> None:
"""
Listener for initializing our sentry logging
Set the sample rate for transactions
https://docs.sentry.io/platforms/python/configuration/options/#traces-sample-rate
Set the profiles sample rate for transactions
https://docs.sentry.io/platforms/python/profiling/
"""
# https://docs.sentry.io/platforms/python/integrations/asyncio/
async_integration = AsyncioIntegration()

# https://docs.sentry.io/platforms/python/integrations/sanic/
sanic_integration = SanicIntegration(
# Configure the Sanic integration so that we generate
# transactions for all HTTP status codes, including 404
unsampled_statuses=None,
)
sentry_configuration = application_instance.config.get("sentry", {})
sentry_key = sentry_configuration.get("SENTRY_CLIENT_KEY", "")
sentry_sdk.init(
dsn=sentry_key,
traces_sample_rate=0.20,
profiles_sample_rate=0.20,
integrations=[async_integration, sanic_integration],
)

0 comments on commit 8aa944a

Please sign in to comment.