diff --git a/CHANGELOG.md b/CHANGELOG.md index 8677767e..1241a45a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## Current (in progress) + +- Update `sentry-sdk` dependency, and update Sentry logic to be able to send environment, app version and profiling/performance info [#100](https://github.com/datagouv/hydra/pull/100) - Add linter and formatter with `pyproject.toml` config, add lint and formatting step in CI, add pre-commit hook to lint and format, update docs and lint and format the code [#99](https://github.com/datagouv/hydra/pull/99) - Use profiling option from csv-detective [#54](https://github.com/etalab/udata-hydra/pull/54) - Remove csv_analysis, integrate into checks [#52](https://github.com/etalab/udata-hydra/pull/52) diff --git a/poetry.lock b/poetry.lock index 4294063d..c8684b61 100755 --- a/poetry.lock +++ b/poetry.lock @@ -1583,13 +1583,13 @@ crt = ["botocore[crt] (>=1.20.29,<2.0a.0)"] [[package]] name = "sentry-sdk" -version = "1.45.0" +version = "2.10.0" description = "Python client for Sentry (https://sentry.io)" optional = false -python-versions = "*" +python-versions = ">=3.6" files = [ - {file = "sentry-sdk-1.45.0.tar.gz", hash = "sha256:509aa9678c0512344ca886281766c2e538682f8acfa50fd8d405f8c417ad0625"}, - {file = "sentry_sdk-1.45.0-py2.py3-none-any.whl", hash = "sha256:1ce29e30240cc289a027011103a8c83885b15ef2f316a60bcc7c5300afa144f1"}, + {file = "sentry_sdk-2.10.0-py2.py3-none-any.whl", hash = "sha256:87b3d413c87d8e7f816cc9334bff255a83d8b577db2b22042651c30c19c09190"}, + {file = "sentry_sdk-2.10.0.tar.gz", hash = "sha256:545fcc6e36c335faa6d6cda84669b6e17025f31efbf3b2211ec14efe008b75d1"}, ] [package.dependencies] diff --git a/pyproject.toml b/pyproject.toml index d2cfeb2e..c616ea5e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -27,7 +27,7 @@ minicli = "^0.5.0" progressist = "^0.1.0" python-magic = "^0.4.25 " redis = "^4.1.4" -sentry-sdk = "^1.11.1" +sentry-sdk = "^2.10.0" aiocontextvars = "^0.2.2" coloredlogs = "^15.0.1" rq = "^1.11.1" diff --git a/udata_hydra/config_default.toml b/udata_hydra/config_default.toml index ae049fab..87849f2a 100644 --- a/udata_hydra/config_default.toml +++ b/udata_hydra/config_default.toml @@ -6,6 +6,7 @@ DATABASE_URL_CSV = "postgres://postgres:postgres@localhost:5434/postgres" DATABASE_SCHEMA = "public" REDIS_URL = "redis://localhost:6379/0" SENTRY_DSN = "" +SENTRY_SAMPLE_RATE = 1.0 TESTING = false # max postgres pool size MAX_POOL_SIZE = 50 diff --git a/udata_hydra/logger.py b/udata_hydra/logger.py index 2ab04f8c..f4a33e3d 100644 --- a/udata_hydra/logger.py +++ b/udata_hydra/logger.py @@ -1,4 +1,6 @@ import logging +import os +from typing import Union import coloredlogs import sentry_sdk @@ -6,6 +8,7 @@ from sentry_sdk.integrations.rq import RqIntegration from udata_hydra import config +from udata_hydra.utils.app_version import get_app_version log = logging.getLogger("udata-hydra") context = {"inited": False} @@ -14,6 +17,10 @@ def setup_logging(): if context.get("inited"): return log + release = "hydra@unknown" + app_version: Union[str, None] = get_app_version() + if app_version: + release = f"hydra@{app_version}" if config.SENTRY_DSN: sentry_sdk.init( dsn=config.SENTRY_DSN, @@ -21,7 +28,15 @@ def setup_logging(): AioHttpIntegration(), RqIntegration(), ], + release=release, + environment=os.getenv("HYDRA_ENV", "unknown"), + # Set traces_sample_rate to 1.0 to capture 100% + # of transactions for performance monitoring. + # Sentry recommends adjusting this value in production. + traces_sample_rate=config.SENTRY_SAMPLE_RATE, + profiles_sample_rate=config.SENTRY_SAMPLE_RATE, ) + coloredlogs.install(level=config.LOG_LEVEL) # silence urllib3 a bit logging.getLogger("urllib3").setLevel("INFO") diff --git a/udata_hydra/utils/app_version.py b/udata_hydra/utils/app_version.py new file mode 100644 index 00000000..c221a9fd --- /dev/null +++ b/udata_hydra/utils/app_version.py @@ -0,0 +1,17 @@ +import logging +from typing import Union + +import toml + +log = logging.getLogger("udata-hydra") + + +def get_app_version() -> Union[str, None]: + """Get the app version from pyproject.toml""" + try: + pyproject: dict = toml.load("pyproject.toml") + app_version: str = pyproject["tool"]["poetry"]["version"] + return app_version + except Exception as e: + log.error(f"Error while getting app version: {e}") + return None