Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update Sentry #100

Merged
merged 4 commits into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions udata_hydra/config_default.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions udata_hydra/logger.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import logging
import os
from typing import Union

import coloredlogs
import sentry_sdk
from sentry_sdk.integrations.aiohttp import AioHttpIntegration
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}
Expand All @@ -14,14 +17,26 @@
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,
integrations=[
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")
Expand Down
17 changes: 17 additions & 0 deletions udata_hydra/utils/app_version.py
Original file line number Diff line number Diff line change
@@ -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