feat: add sentry sdk

This commit is contained in:
jo 2023-02-26 23:24:29 +01:00 committed by Kyle Robbertze
parent a60d83311b
commit b7214b5d46
14 changed files with 86 additions and 12 deletions

View File

@ -76,7 +76,7 @@ repos:
- id: requirements.txt - id: requirements.txt
name: requirements.txt name: requirements.txt
description: Generate requirements.txt description: Generate requirements.txt
entry: tools/extract_requirements.py dev entry: tools/extract_requirements.py dev sentry
pass_filenames: false pass_filenames: false
language: script language: script
files: setup.py$ files: setup.py$

View File

@ -83,7 +83,7 @@ RUN --mount=type=cache,target=/root/.cache/pip \
COPY analyzer . COPY analyzer .
RUN --mount=type=cache,target=/root/.cache/pip \ RUN --mount=type=cache,target=/root/.cache/pip \
pip install --editable . pip install --editable .[sentry]
# Run # Run
USER ${UID}:${GID} USER ${UID}:${GID}
@ -122,7 +122,7 @@ RUN --mount=type=cache,target=/root/.cache/pip \
COPY playout . COPY playout .
RUN --mount=type=cache,target=/root/.cache/pip \ RUN --mount=type=cache,target=/root/.cache/pip \
pip install --editable . pip install --editable .[sentry]
# Run # Run
USER ${UID}:${GID} USER ${UID}:${GID}
@ -158,7 +158,7 @@ RUN --mount=type=cache,target=/root/.cache/pip \
COPY api . COPY api .
RUN --mount=type=cache,target=/root/.cache/pip \ RUN --mount=type=cache,target=/root/.cache/pip \
pip install --editable .[prod] pip install --editable .[prod,sentry]
# Run # Run
USER ${UID}:${GID} USER ${UID}:${GID}
@ -191,7 +191,7 @@ RUN --mount=type=cache,target=/root/.cache/pip \
COPY worker . COPY worker .
RUN --mount=type=cache,target=/root/.cache/pip \ RUN --mount=type=cache,target=/root/.cache/pip \
pip install --editable . pip install --editable .[sentry]
# Run # Run
USER ${UID}:${GID} USER ${UID}:${GID}

View File

@ -4,7 +4,7 @@ include ../tools/python.mk
PIP_INSTALL := \ PIP_INSTALL := \
--editable ../shared \ --editable ../shared \
--editable .[dev] --editable .[dev,sentry]
PYLINT_ARG := libretime_analyzer tests || true PYLINT_ARG := libretime_analyzer tests || true
MYPY_ARG := libretime_analyzer tests || true MYPY_ARG := libretime_analyzer tests || true
BANDIT_ARG := libretime_analyzer || true BANDIT_ARG := libretime_analyzer || true

View File

@ -1,3 +1,5 @@
import logging
import os
from pathlib import Path from pathlib import Path
from typing import Optional from typing import Optional
@ -6,10 +8,13 @@ from libretime_shared.cli import cli_config_options, cli_logging_options
from libretime_shared.config import DEFAULT_ENV_PREFIX from libretime_shared.config import DEFAULT_ENV_PREFIX
from libretime_shared.logging import setup_logger from libretime_shared.logging import setup_logger
from . import PACKAGE, VERSION
from .config import Config from .config import Config
from .message_listener import MessageListener from .message_listener import MessageListener
from .status_reporter import StatusReporter from .status_reporter import StatusReporter
logger = logging.getLogger(__name__)
VERSION = "1.0" VERSION = "1.0"
DEFAULT_RETRY_QUEUE_FILEPATH = Path("retry_queue") DEFAULT_RETRY_QUEUE_FILEPATH = Path("retry_queue")
@ -36,6 +41,16 @@ def cli(
setup_logger(log_level, log_filepath) setup_logger(log_level, log_filepath)
config = Config(config_filepath) config = Config(config_filepath)
if "SENTRY_DSN" in os.environ:
logger.info("installing sentry")
# pylint: disable=import-outside-toplevel
import sentry_sdk
sentry_sdk.init(
traces_sample_rate=1.0,
release=f"{PACKAGE}@{VERSION}",
)
# Start up the StatusReporter process # Start up the StatusReporter process
StatusReporter.start_thread(retry_queue_filepath) StatusReporter.start_thread(retry_queue_filepath)

View File

@ -30,6 +30,9 @@ setup(
"distro>=1.8.0,<1.9", "distro>=1.8.0,<1.9",
"types-requests>=2.25.1,<2.29", "types-requests>=2.25.1,<2.29",
], ],
"sentry": [
"sentry-sdk>=1.15.0,<1.16",
],
}, },
zip_safe=False, zip_safe=False,
) )

View File

@ -4,7 +4,7 @@ include ../tools/python.mk
PIP_INSTALL := \ PIP_INSTALL := \
--editable ../shared \ --editable ../shared \
--editable .[dev] --editable .[dev,sentry]
PYLINT_ARG := libretime_api PYLINT_ARG := libretime_api
MYPY_ARG := libretime_api MYPY_ARG := libretime_api
BANDIT_ARG := --exclude '*/tests/*' libretime_api || true BANDIT_ARG := --exclude '*/tests/*' libretime_api || true

View File

@ -1,6 +1,8 @@
from os import getenv from os import environ, getenv
from typing import Optional from typing import Optional
from .. import PACKAGE, VERSION
API_VERSION = "2.0.0" API_VERSION = "2.0.0"
# SECURITY WARNING: don't run with debug turned on in production! # SECURITY WARNING: don't run with debug turned on in production!
@ -178,3 +180,18 @@ SPECTACULAR_SETTINGS = {
"VERSION": API_VERSION, "VERSION": API_VERSION,
"ENUM_NAME_OVERRIDES": SPECTACULAR_ENUM_NAME_OVERRIDES, "ENUM_NAME_OVERRIDES": SPECTACULAR_ENUM_NAME_OVERRIDES,
} }
# Sentry
# https://docs.sentry.io/platforms/python/guides/django/
if "SENTRY_DSN" in environ:
# pylint: disable=import-outside-toplevel
import sentry_sdk
from sentry_sdk.integrations.django import DjangoIntegration
sentry_sdk.init(
traces_sample_rate=1.0,
release=f"{PACKAGE}@{VERSION}",
integrations=[
DjangoIntegration(),
],
)

View File

@ -44,5 +44,8 @@ setup(
"pytest-django>=4.5.2,<4.6", "pytest-django>=4.5.2,<4.6",
"requests-mock>=1.10.0,<1.11", "requests-mock>=1.10.0,<1.11",
], ],
"sentry": [
"sentry-sdk[django]>=1.15.0,<1.16",
],
}, },
) )

View File

@ -6,7 +6,7 @@ APP := playout
PIP_INSTALL := \ PIP_INSTALL := \
--editable ../api-client \ --editable ../api-client \
--editable ../shared \ --editable ../shared \
--editable .[dev] --editable .[dev,sentry]
PYLINT_ARG := libretime_playout tests PYLINT_ARG := libretime_playout tests
MYPY_ARG := libretime_playout tests || true MYPY_ARG := libretime_playout tests || true
BANDIT_ARG := libretime_playout || true BANDIT_ARG := libretime_playout || true

View File

@ -1,8 +1,8 @@
""" """
Python part of radio playout (pypo) Python part of radio playout (pypo)
""" """
import logging import logging
import os
import sys import sys
import time import time
from datetime import datetime from datetime import datetime
@ -18,6 +18,7 @@ from libretime_shared.cli import cli_config_options, cli_logging_options
from libretime_shared.config import DEFAULT_ENV_PREFIX from libretime_shared.config import DEFAULT_ENV_PREFIX
from libretime_shared.logging import setup_logger from libretime_shared.logging import setup_logger
from . import PACKAGE, VERSION
from .config import CACHE_DIR, RECORD_DIR, Config from .config import CACHE_DIR, RECORD_DIR, Config
from .history.stats import StatsCollectorThread from .history.stats import StatsCollectorThread
from .liquidsoap.client import LiquidsoapClient from .liquidsoap.client import LiquidsoapClient
@ -75,6 +76,16 @@ def cli(
setup_logger(log_level, log_filepath) setup_logger(log_level, log_filepath)
config = Config(config_filepath) config = Config(config_filepath)
if "SENTRY_DSN" in os.environ:
logger.info("installing sentry")
# pylint: disable=import-outside-toplevel
import sentry_sdk
sentry_sdk.init(
traces_sample_rate=1.0,
release=f"{PACKAGE}@{VERSION}",
)
try: try:
for dir_path in [CACHE_DIR, RECORD_DIR]: for dir_path in [CACHE_DIR, RECORD_DIR]:
dir_path.mkdir(exist_ok=True) dir_path.mkdir(exist_ok=True)

View File

@ -41,6 +41,9 @@ setup(
"types-python-dateutil>=2.8.1,<2.9", "types-python-dateutil>=2.8.1,<2.9",
"types-requests>=2.25.1,<2.29", "types-requests>=2.25.1,<2.29",
], ],
"sentry": [
"sentry-sdk>=1.15.0,<1.16",
],
}, },
zip_safe=False, zip_safe=False,
) )

View File

@ -4,7 +4,7 @@ include ../tools/python.mk
PIP_INSTALL := \ PIP_INSTALL := \
--editable ../shared \ --editable ../shared \
--editable .[dev] --editable .[dev,sentry]
PYLINT_ARG := libretime_worker PYLINT_ARG := libretime_worker
MYPY_ARG := libretime_worker || true MYPY_ARG := libretime_worker || true
BANDIT_ARG := libretime_worker BANDIT_ARG := libretime_worker

View File

@ -1,4 +1,5 @@
import json import json
import os
from email.message import EmailMessage from email.message import EmailMessage
from pathlib import Path from pathlib import Path
from tempfile import NamedTemporaryFile from tempfile import NamedTemporaryFile
@ -7,17 +8,35 @@ from urllib.parse import urlsplit
import mutagen import mutagen
import requests import requests
from celery import Celery from celery import Celery, signals
from celery.utils.log import get_task_logger from celery.utils.log import get_task_logger
from mutagen import MutagenError from mutagen import MutagenError
from requests import RequestException, Response from requests import RequestException, Response
from . import PACKAGE, VERSION
from .config import config from .config import config
worker = Celery() worker = Celery()
logger = get_task_logger(__name__) logger = get_task_logger(__name__)
@signals.worker_init.connect
def init_sentry(**_kwargs):
if "SENTRY_DSN" in os.environ:
logger.info("installing sentry")
# pylint: disable=import-outside-toplevel
import sentry_sdk
from sentry_sdk.integrations.celery import CeleryIntegration
sentry_sdk.init(
traces_sample_rate=1.0,
release=f"{PACKAGE}@{VERSION}",
integrations=[
CeleryIntegration(),
],
)
@worker.task(name="podcast-download", acks_late=True) @worker.task(name="podcast-download", acks_late=True)
def podcast_download( def podcast_download(
episode_id: int, episode_id: int,

View File

@ -25,6 +25,9 @@ setup(
"requests-mock>=1.10.0,<1.11", "requests-mock>=1.10.0,<1.11",
"types-requests>=2.25.1,<2.29", "types-requests>=2.25.1,<2.29",
], ],
"sentry": [
"sentry-sdk>=1.15.0,<1.16",
],
}, },
zip_safe=False, zip_safe=False,
) )