Skip to content

Commit

Permalink
feat: add sentry for bug tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
raphael0202 committed Sep 22, 2023
1 parent 82c2f99 commit 9529b52
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -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=
1 change: 1 addition & 0 deletions .github/workflows/container-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions app/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -22,6 +22,7 @@
"url": "https://www.gnu.org/licenses/agpl-3.0.en.html",
},
)
init_sentry(settings.sentry_dns)
connection.get_connection()


Expand Down
1 change: 1 addition & 0 deletions app/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
3 changes: 3 additions & 0 deletions app/import_queue/queue_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion app/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
19 changes: 19 additions & 0 deletions app/utils/log.py
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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,
)
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 9529b52

Please sign in to comment.