diff --git a/.env b/.env index 76984e8e..cc54679b 100644 --- a/.env +++ b/.env @@ -39,3 +39,6 @@ MEM_LIMIT=4294967296 # on dev connect to the same network as off-server COMMON_NET_NAME=po_default OPENFOODFACTS_API_URL=http://world.openfoodfacts.localhost + +# Sentry DNS for bug tracking, used only in staging and production +SENTRY_DNS= \ No newline at end of file diff --git a/.github/workflows/container-deploy.yml b/.github/workflows/container-deploy.yml index ba138bf4..fe206e36 100644 --- a/.github/workflows/container-deploy.yml +++ b/.github/workflows/container-deploy.yml @@ -124,6 +124,7 @@ jobs: # This secret is to be generated using htpasswd, see .env file # use simple quotes to avoid interpolation of $apr1$ ! echo 'NGINX_BASIC_AUTH_USER_PASSWD=${{ secrets.NGINX_BASIC_AUTH_USER_PASSWD }}' >> .env + echo "SENTRY_DNS=${{ secrets.SENTRY_DSN }}" >> .env - name: Create Docker volumes uses: appleboy/ssh-action@master diff --git a/app/api.py b/app/api.py index b5504c7d..2182cfa1 100644 --- a/app/api.py +++ b/app/api.py @@ -3,10 +3,10 @@ from elasticsearch_dsl import Search from fastapi import FastAPI, HTTPException, Query -from app.config import CONFIG +from app.config import CONFIG, settings from app.postprocessing import load_result_processor from app.query import build_search_query -from app.utils import connection, get_logger +from app.utils import connection, get_logger, init_sentry logger = get_logger() @@ -22,6 +22,7 @@ "url": "https://www.gnu.org/licenses/agpl-3.0.en.html", }, ) +init_sentry(settings.sentry_dns) connection.get_connection() diff --git a/app/config.py b/app/config.py index 2babdf09..c7a84879 100644 --- a/app/config.py +++ b/app/config.py @@ -11,6 +11,7 @@ class Settings(BaseSettings): redis_host: str = "localhost" # TODO: this should be in the config below openfoodfacts_base_url: str = "https://world.openfoodfacts.org" + sentry_dns: str | None = None settings = Settings() diff --git a/app/import_queue/queue_manager.py b/app/import_queue/queue_manager.py index 443b1eca..bbc3fa54 100644 --- a/app/import_queue/queue_manager.py +++ b/app/import_queue/queue_manager.py @@ -5,6 +5,7 @@ from app.import_queue.redis_client import RedisClient from app.indexing import DocumentProcessor from app.utils import get_logger +from app.utils.log import init_sentry logger = get_logger(__name__) @@ -76,6 +77,8 @@ def run_queue_safe(config: Config): if __name__ == "__main__": # Create root logger get_logger() + # Initialize sentry for bug tracking + init_sentry(settings.sentry_dns) # create elasticsearch connection from app.utils import connection diff --git a/app/utils/__init__.py b/app/utils/__init__.py index 11d57991..629df035 100644 --- a/app/utils/__init__.py +++ b/app/utils/__init__.py @@ -1,6 +1,6 @@ import importlib -from .log import get_logger +from .log import get_logger, init_sentry def load_class_object_from_string(qualifier: str) -> type: diff --git a/app/utils/log.py b/app/utils/log.py index 4e58b237..a2dbd68a 100644 --- a/app/utils/log.py +++ b/app/utils/log.py @@ -1,5 +1,9 @@ import logging +import sentry_sdk +from sentry_sdk.integrations import Integration +from sentry_sdk.integrations.logging import LoggingIntegration + def get_logger(name=None, level: int = logging.INFO) -> logging.Logger: logger = logging.getLogger(name) @@ -27,3 +31,18 @@ def configure_root_logger( handler.setLevel(level) logger.addHandler(handler) return logger + + +def init_sentry(sentry_dsn: str | None, integrations: list[Integration] | None = None): + if sentry_dsn: + integrations = integrations or [] + integrations.append( + LoggingIntegration( + level=logging.INFO, # Capture info and above as breadcrumbs + event_level=logging.WARNING, # Send warning and errors as events + ) + ) + sentry_sdk.init( # type:ignore # mypy say it's abstract + sentry_dsn, + integrations=integrations, + ) diff --git a/docker-compose.yml b/docker-compose.yml index 60852d58..137653d4 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -41,6 +41,7 @@ x-api-common: &api-common - ELASTICSEARCH_URL=http://es01:9200 - REDIS_HOST=redis - OPENFOODFACTS_API_URL + - SENTRY_DNS # make it easier to run scripts - PYTHONPATH=/code networks: diff --git a/requirements.txt b/requirements.txt index c7434172..dc5b3004 100644 --- a/requirements.txt +++ b/requirements.txt @@ -8,3 +8,4 @@ cachetools==5.3.1 typer==0.9.0 luqum==0.13.0 pydantic-settings==2.0.3 +sentry-sdk[fastapi]==1.31.0 \ No newline at end of file