Skip to content

Commit 215c703

Browse files
ctrl-schaffjschaff
andauthored
Add sentry support (#66)
* Add sentry listener for the webapp * Add listener support for hooking into the application instance * Add various sentry dependencies --------- Co-authored-by: jschaff <jschaff@scripps.edu>
1 parent b7f465c commit 215c703

File tree

4 files changed

+59
-3
lines changed

4 files changed

+59
-3
lines changed

biothings_annotator/application/configuration/default.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
"REQUEST_MAX_SIZE": 100000000,
88
"CACHE_MAX_AGE": 604800
99
},
10+
"sentry": {
11+
"SENTRY_CLIENT_KEY": ""
12+
},
1013
"extension": {
1114
"cors": {
1215
"CORS_ALLOW_HEADERS": "*",
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from typing import Dict, List
2+
3+
from .sentry import initialize_sentry
4+
5+
6+
def build_listeners() -> List[Dict]:
7+
"""
8+
Basic method for aggregating all of listeners created
9+
for the annotator service
10+
"""
11+
sentry_listener = {"listener": initialize_sentry, "event": "before_server_start", "priority": 0}
12+
listener_collection = [sentry_listener]
13+
return listener_collection
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
"""
2+
Listener for configuring the sentry support for the
3+
sanic web-app associated with the annotator service
4+
"""
5+
6+
import sanic
7+
8+
import sentry_sdk
9+
from sentry_sdk.integrations.asyncio import AsyncioIntegration
10+
from sentry_sdk.integrations.sanic import SanicIntegration
11+
12+
13+
async def initialize_sentry(application_instance: sanic.Sanic) -> None:
14+
"""
15+
Listener for initializing our sentry logging
16+
17+
Set the sample rate for transactions
18+
https://docs.sentry.io/platforms/python/configuration/options/#traces-sample-rate
19+
20+
Set the profiles sample rate for transactions
21+
https://docs.sentry.io/platforms/python/profiling/
22+
"""
23+
# https://docs.sentry.io/platforms/python/integrations/asyncio/
24+
async_integration = AsyncioIntegration()
25+
26+
# https://docs.sentry.io/platforms/python/integrations/sanic/
27+
sanic_integration = SanicIntegration(
28+
# Configure the Sanic integration so that we generate
29+
# transactions for all HTTP status codes, including 404
30+
unsampled_statuses=None,
31+
)
32+
sentry_configuration = application_instance.config.get("sentry", {})
33+
sentry_key = sentry_configuration.get("SENTRY_CLIENT_KEY", "")
34+
sentry_sdk.init(
35+
dsn=sentry_key,
36+
traces_sample_rate=0.20,
37+
profiles_sample_rate=0.20,
38+
integrations=[async_integration, sanic_integration],
39+
)

pyproject.toml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ classifiers = [
4444
]
4545
dependencies = [
4646
"biothings_client >= 0.4.0",
47-
"sanic[ext] == 24.12.0"
47+
"sanic[ext] == 24.12.0",
48+
"sentry-sdk[sanic] >= 2.19.0",
4849
]
4950

5051

@@ -54,7 +55,7 @@ tests = [
5455
"pytest-asyncio >= 0.23.8",
5556
"pytest-randomly",
5657
"sanic-testing >= 24.6.0",
57-
"pytest-randomly"
58+
"pytest-randomly",
5859
]
5960

6061

@@ -64,7 +65,7 @@ tests = [
6465

6566
[tool.black]
6667
line-length = 120
67-
target-version = ['py38', 'py39', 'py310', 'py311', 'py312']
68+
target-version = ['py38', 'py39', 'py310', 'py311', 'py312', 'py313', 'py314']
6869
extend-exclude = '''
6970
# A regex preceded with ^/ will apply only to files and directories
7071
# in the root of the project.

0 commit comments

Comments
 (0)