Skip to content

Commit

Permalink
feat: add sentry support to skope api backend
Browse files Browse the repository at this point in the history
only enabled in prod
  • Loading branch information
alee committed May 5, 2022
1 parent 8547b70 commit ae0ad1d
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 4 deletions.
5 changes: 5 additions & 0 deletions timeseries/app/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class Settings(BaseSettings):
max_processing_time = 15000 # in milliseconds
default_max_cells = 500000 # max number of cells to extract from data cubes
store: Store
sentry_dsn = "https://79be8c27ac14446e8af51b6d5f3dc90f@sentry.comses.net/5"

@classmethod
def create(cls):
Expand All @@ -40,6 +41,10 @@ def create(cls):
dictConfig(yaml.safe_load(f))
return instance

@property
def is_production(self):
return self.environment == "prod"

@property
def logging_config_file(self):
return f"deploy/logging/{self.environment}.yml"
Expand Down
28 changes: 25 additions & 3 deletions timeseries/app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,53 @@
from fastapi.responses import JSONResponse
from starlette.status import HTTP_504_GATEWAY_TIMEOUT, HTTP_422_UNPROCESSABLE_ENTITY

from app.config import get_settings, Settings
from app.config import get_settings
from app.exceptions import TimeseriesValidationError, TimeseriesTimeoutError
from app.routers.v1 import api as v1_api
from app.routers.v2 import api as v2_api

import sentry_sdk
from sentry_sdk.integrations.asgi import SentryAsgiMiddleware

import logging


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

settings = get_settings()

logger = logging.getLogger(__name__)


if settings.is_production:
sentry_sdk.init(dsn=settings.sentry_dsn)
try:
app.add_middleware(SentryAsgiMiddleware)
except Exception:
logger.error("Unable to initialize Sentry middleware")
pass

# Since the whole API is public there is no danger in allowing all cross origin requests for now
app.add_middleware(
CORSMiddleware,
allow_origins=get_settings().allowed_origins,
allow_origins=settings.allowed_origins,
allow_methods=["*"],
allow_headers=["*"],
)


@app.get("/settings")
async def info(settings: Settings = Depends(get_settings)):
async def info():
info_dict = dict(settings.__dict__)
info_dict.update(logfile=settings.logging_config_file)
return info_dict


@app.get("/test-sentry")
async def test_sentry():
raise Exception("Testing sentry integration")


@app.exception_handler(TimeseriesTimeoutError)
async def timeseries_timeout_error_handler(
request: Request, exc: TimeseriesTimeoutError
Expand Down
6 changes: 5 additions & 1 deletion timeseries/app/schemas/geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,11 @@ class Config:
class BaseSkopePolygonModel(SkopeGeometry):
@staticmethod
def _make_band_range_groups(
*, width: int, height: int, band_range: BandRange, max_size=settings.default_max_cells
*,
width: int,
height: int,
band_range: BandRange,
max_size=settings.default_max_cells,
):
n_cells_per_band = width * height # 25
n_cells_per_full_chunk = max_size - max_size % n_cells_per_band
Expand Down
1 change: 1 addition & 0 deletions timeseries/deploy/requirements/base.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ python-dateutil==2.8.2
PyYAML==6.0
rasterio==1.2.10
scipy==1.8.0
sentry-sdk==1.5.11
Shapely==1.7.1
uvicorn==0.17.6

0 comments on commit ae0ad1d

Please sign in to comment.