Skip to content

Commit ae0ad1d

Browse files
committed
feat: add sentry support to skope api backend
only enabled in prod
1 parent 8547b70 commit ae0ad1d

4 files changed

Lines changed: 36 additions & 4 deletions

File tree

timeseries/app/config.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class Settings(BaseSettings):
3232
max_processing_time = 15000 # in milliseconds
3333
default_max_cells = 500000 # max number of cells to extract from data cubes
3434
store: Store
35+
sentry_dsn = "https://79be8c27ac14446e8af51b6d5f3dc90f@sentry.comses.net/5"
3536

3637
@classmethod
3738
def create(cls):
@@ -40,6 +41,10 @@ def create(cls):
4041
dictConfig(yaml.safe_load(f))
4142
return instance
4243

44+
@property
45+
def is_production(self):
46+
return self.environment == "prod"
47+
4348
@property
4449
def logging_config_file(self):
4550
return f"deploy/logging/{self.environment}.yml"

timeseries/app/main.py

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,53 @@
33
from fastapi.responses import JSONResponse
44
from starlette.status import HTTP_504_GATEWAY_TIMEOUT, HTTP_422_UNPROCESSABLE_ENTITY
55

6-
from app.config import get_settings, Settings
6+
from app.config import get_settings
77
from app.exceptions import TimeseriesValidationError, TimeseriesTimeoutError
88
from app.routers.v1 import api as v1_api
99
from app.routers.v2 import api as v2_api
1010

11+
import sentry_sdk
12+
from sentry_sdk.integrations.asgi import SentryAsgiMiddleware
13+
14+
import logging
15+
1116

1217
app = FastAPI(title="SKOPE API Services")
1318

19+
settings = get_settings()
20+
21+
logger = logging.getLogger(__name__)
22+
23+
24+
if settings.is_production:
25+
sentry_sdk.init(dsn=settings.sentry_dsn)
26+
try:
27+
app.add_middleware(SentryAsgiMiddleware)
28+
except Exception:
29+
logger.error("Unable to initialize Sentry middleware")
30+
pass
1431

1532
# Since the whole API is public there is no danger in allowing all cross origin requests for now
1633
app.add_middleware(
1734
CORSMiddleware,
18-
allow_origins=get_settings().allowed_origins,
35+
allow_origins=settings.allowed_origins,
1936
allow_methods=["*"],
2037
allow_headers=["*"],
2138
)
2239

2340

2441
@app.get("/settings")
25-
async def info(settings: Settings = Depends(get_settings)):
42+
async def info():
2643
info_dict = dict(settings.__dict__)
2744
info_dict.update(logfile=settings.logging_config_file)
2845
return info_dict
2946

3047

48+
@app.get("/test-sentry")
49+
async def test_sentry():
50+
raise Exception("Testing sentry integration")
51+
52+
3153
@app.exception_handler(TimeseriesTimeoutError)
3254
async def timeseries_timeout_error_handler(
3355
request: Request, exc: TimeseriesTimeoutError

timeseries/app/schemas/geometry.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,11 @@ class Config:
109109
class BaseSkopePolygonModel(SkopeGeometry):
110110
@staticmethod
111111
def _make_band_range_groups(
112-
*, width: int, height: int, band_range: BandRange, max_size=settings.default_max_cells
112+
*,
113+
width: int,
114+
height: int,
115+
band_range: BandRange,
116+
max_size=settings.default_max_cells,
113117
):
114118
n_cells_per_band = width * height # 25
115119
n_cells_per_full_chunk = max_size - max_size % n_cells_per_band

timeseries/deploy/requirements/base.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ python-dateutil==2.8.2
88
PyYAML==6.0
99
rasterio==1.2.10
1010
scipy==1.8.0
11+
sentry-sdk==1.5.11
1112
Shapely==1.7.1
1213
uvicorn==0.17.6

0 commit comments

Comments
 (0)