From 0dbdee2d791b396d5bbe6a46933152f5a9346ae2 Mon Sep 17 00:00:00 2001 From: Bryan Newbold Date: Wed, 21 Oct 2020 16:38:59 -0700 Subject: set up sentry exception handling --- Makefile | 2 +- fatcat_scholar/config.py | 4 +++- fatcat_scholar/web.py | 29 ++++++++++++++++++++++++++--- settings.toml | 2 +- 4 files changed, 31 insertions(+), 6 deletions(-) diff --git a/Makefile b/Makefile index a5c16b4..18e36d1 100644 --- a/Makefile +++ b/Makefile @@ -37,7 +37,7 @@ dev: ## Run web service locally, with reloading .PHONY: dev-qa dev-qa: ## Run web service locally, with reloading, but point search queries to QA search index - ENV_FOR_DYNACONF=qa pipenv run uvicorn fatcat_scholar.web:app --reload --port 9819 + ENV_FOR_DYNACONF=qa DYNACONF_SCHOLAR_ENV=dev pipenv run uvicorn fatcat_scholar.web:app --reload --port 9819 .PHONY: run run: ## Run web service under gunicorn diff --git a/fatcat_scholar/config.py b/fatcat_scholar/config.py index 9a9d529..c66a3e5 100644 --- a/fatcat_scholar/config.py +++ b/fatcat_scholar/config.py @@ -1,4 +1,6 @@ +import subprocess from dynaconf import Dynaconf - settings = Dynaconf(settings_file="settings.toml", environments=True,) + +GIT_REVISION = subprocess.check_output(["git", "describe", "--always"]).strip().decode('utf-8') diff --git a/fatcat_scholar/web.py b/fatcat_scholar/web.py index 86bfb70..c50e02c 100644 --- a/fatcat_scholar/web.py +++ b/fatcat_scholar/web.py @@ -4,23 +4,39 @@ This contains the FastAPI web application and RESTful API. So far there are few endpoints, so we just put them all here! """ +import sys +import logging +from typing import Optional, Any + import babel.support from fastapi import FastAPI, APIRouter, Request, Depends, Response from fastapi.staticfiles import StaticFiles from fastapi.responses import PlainTextResponse -from typing import Optional, Any +import sentry_sdk +from sentry_sdk.integrations.asgi import SentryAsgiMiddleware -from fatcat_scholar.config import settings +from fatcat_scholar.config import settings, GIT_REVISION from fatcat_scholar.hacks import Jinja2Templates from fatcat_scholar.search import do_fulltext_search, FulltextQuery, FulltextHits -# print(f"dynaconf settings: {settings.as_dict()}", file=sys.stderr) + +logger = logging.getLogger() I18N_LANG_TRANSLATIONS = ["de", "zh", "ru", "ar", "fr", "es", "nb"] I18N_LANG_OPTIONS = I18N_LANG_TRANSLATIONS + [ settings.I18N_LANG_DEFAULT, ] +# note the middleware also installed at the end of this file +if settings.SENTRY_DSN: + logger.info("Sentry integration enabled") + sentry_sdk.init( + dsn=settings.SENTRY_DSN, + environment=settings.SCHOLAR_ENV, + max_breadcrumbs=10, + release=GIT_REVISION, + ) + class LangPrefix: """ @@ -39,6 +55,7 @@ class LangPrefix: self.prefix = f"/{lang_option}" self.code = lang_option break + sentry_sdk.set_tag("locale", self.code) class ContentNegotiation: @@ -169,9 +186,12 @@ async def web_search( try: hits = do_fulltext_search(query) except ValueError as e: + sentry_sdk.set_level("warning") + sentry_sdk.capture_exception(e) search_error = dict(type="query", message=str(e)) status_code = 400 except IOError as e: + sentry_sdk.capture_exception(e) search_error = dict(type="backend", message=str(e)) status_code = 500 @@ -231,3 +251,6 @@ async def robots_txt(response_class: Any = PlainTextResponse) -> Any: return PlainTextResponse(ROBOTS_ALLOW) else: return PlainTextResponse(ROBOTS_DISALLOW) + +# add sentry middleware +app = SentryAsgiMiddleware(app) diff --git a/settings.toml b/settings.toml index aaab1bd..a16969b 100644 --- a/settings.toml +++ b/settings.toml @@ -1,7 +1,6 @@ [default] SCHOLAR_ENV = "default" -SCHOLAR_DOMAIN = "fatcat.wiki" SCHOLAR_ISSUEDB_PATH = "data/issue_db.sqlite" I18N_LANG_DEFAULT = "en" FATCAT_ELASTICSEARCH_BACKEND = "https://search.fatcat.wiki" @@ -13,6 +12,7 @@ SANDCRAWLER_S3_API = "wbgrp-svc169.us.archive.org:8333" KAFKA_BROKERS = [] FATCAT_API_HOST = "https://api.fatcat.wiki/v0" INDEX_WORKER_BATCH_SIZE = 50 +SENTRY_DSN = "" [test] SCHOLAR_ENV = "test" -- cgit v1.2.3