Skip to content

Commit 375794e

Browse files
authored
Track Errors with Sentry (#311)
* add sentry-sdk * add sentry middleware
1 parent 1eee459 commit 375794e

File tree

6 files changed

+74
-53
lines changed

6 files changed

+74
-53
lines changed

Pipfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ pydantic = {extras = ["dotenv"],version = "*"}
3333
python-dateutil = "*"
3434
requests = "*"
3535
scout-apm = "*"
36+
sentry-sdk = "*"
3637
uvicorn = "*"
3738

3839
[requires]

Pipfile.lock

Lines changed: 52 additions & 45 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/config.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ class _Settings(BaseSettings):
1313
local_redis_url: AnyUrl = None
1414
# Scout APM
1515
scout_name: str = None
16+
# Sentry
17+
sentry_dsn: str = None
1618

1719

1820
@functools.lru_cache()

app/main.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44
import logging
55

66
import pydantic
7+
import sentry_sdk
78
import uvicorn
89
from fastapi import FastAPI, Request, Response
910
from fastapi.middleware.cors import CORSMiddleware
1011
from fastapi.middleware.gzip import GZipMiddleware
1112
from fastapi.responses import JSONResponse
1213
from scout_apm.async_.starlette import ScoutMiddleware
14+
from sentry_sdk.integrations.asgi import SentryAsgiMiddleware
1315

1416
from .config import get_settings
1517
from .data import data_source
@@ -23,6 +25,9 @@
2325

2426
SETTINGS = get_settings()
2527

28+
if SETTINGS.sentry_dsn: # pragma: no cover
29+
sentry_sdk.init(dsn=SETTINGS.sentry_dsn)
30+
2631
APP = FastAPI(
2732
title="Coronavirus Tracker",
2833
description=(
@@ -47,6 +52,11 @@
4752
else:
4853
LOGGER.debug("No SCOUT_NAME config")
4954

55+
# Sentry Error Tracking
56+
if SETTINGS.sentry_dsn: # pragma: no cover
57+
LOGGER.info("Adding Sentry middleware")
58+
APP.add_middleware(SentryAsgiMiddleware)
59+
5060
# Enable CORS.
5161
APP.add_middleware(
5262
CORSMiddleware, allow_credentials=True, allow_origins=["*"], allow_methods=["*"], allow_headers=["*"],

requirements-dev.txt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
-i https://pypi.org/simple
22
appdirs==1.4.3
3-
astroid==2.4.0
3+
astroid==2.4.1
44
async-asgi-testclient==1.4.4
55
async-generator==1.10
66
asyncmock==0.4.2
@@ -13,8 +13,8 @@ click==7.1.2
1313
coverage==5.1
1414
coveralls==2.0.0
1515
docopt==0.6.2
16-
gitdb==4.0.4
17-
gitpython==3.1.1
16+
gitdb==4.0.5
17+
gitpython==3.1.2
1818
idna==2.9
1919
importlib-metadata==1.6.0 ; python_version < '3.8'
2020
invoke==1.4.1
@@ -29,17 +29,17 @@ pathspec==0.8.0
2929
pbr==5.4.5
3030
pluggy==0.13.1
3131
py==1.8.1
32-
pylint==2.5.0
32+
pylint==2.5.2
3333
pyparsing==2.4.7
34-
pytest-asyncio==0.11.0
34+
pytest-asyncio==0.12.0
3535
pytest-cov==2.8.1
36-
pytest==5.4.1
36+
pytest==5.4.2
3737
pyyaml==5.3.1
38-
regex==2020.4.4
38+
regex==2020.5.7
3939
requests==2.23.0
4040
responses==0.10.14
4141
six==1.14.0
42-
smmap==3.0.2
42+
smmap==3.0.4
4343
stevedore==1.32.0
4444
toml==0.10.0
4545
typed-ast==1.4.1

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ python-dateutil==2.8.1
3030
python-dotenv==0.13.0
3131
requests==2.23.0
3232
scout-apm==2.14.1
33+
sentry-sdk==0.14.3
3334
six==1.14.0
3435
starlette==0.13.2
3536
urllib3[secure]==1.25.9 ; python_version >= '3.5'

0 commit comments

Comments
 (0)