Skip to content

Commit 7f8182f

Browse files
anfimovdmKwaizermaccelf
authored
Add Sentry support (#153)
* Add Sentry support (#113) * Backport production changes (#119) * Catch timeout exception more carefully (#120) * Fix Sentry capturing message (#123) --------- Co-authored-by: Lev <94750963+Kwaizer@users.noreply.github.com> Co-authored-by: Maxim <mpetuhov@almalinux.org>
1 parent b8b9b56 commit 7f8182f

File tree

5 files changed

+35
-4
lines changed

5 files changed

+35
-4
lines changed

alts/scheduler/app.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import aiohttp
88
import logging
9+
import sentry_sdk
910
import signal
1011
import urllib.parse
1112
from threading import Event
@@ -40,6 +41,16 @@
4041
graceful_terminate_event = Event()
4142
http_bearer_scheme = HTTPBearer()
4243

44+
if CONFIG.sentry_dsn:
45+
sentry_sdk.init(
46+
dsn=CONFIG.sentry_dsn,
47+
traces_sample_rate=CONFIG.sentry_traces_sample_rate,
48+
profiles_sample_rate=CONFIG.sentry_profiles_sample_rate,
49+
environment=CONFIG.sentry_environment,
50+
ignore_errors=[
51+
ConnectionResetError
52+
],
53+
)
4354

4455
def get_celery_task_result(task_id: str, timeout: int = 1) -> tuple:
4556
"""

alts/shared/models.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,11 @@ def __init__(self, **data):
245245
):
246246
setattr(self, field_name, field)
247247

248+
# Sentry params
249+
sentry_dsn: str = ''
250+
sentry_traces_sample_rate: float = 0.2
251+
sentry_profiles_sample_rate: float = 0.2
252+
sentry_environment: str = 'dev'
248253
# Whether to setup Celery SSL
249254
use_ssl: bool = False
250255
# SSL configuration section

alts/worker/app.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import itertools
22
import logging
3+
import sentry_sdk
34

45
from celery import Celery
56
from kombu import Exchange, Queue
@@ -32,6 +33,17 @@
3233
celery_app.conf.task_default_exchange = 'default'
3334
celery_app.conf.task_default_routing_key = 'default'
3435

36+
if CONFIG.sentry_dsn:
37+
sentry_sdk.init(
38+
dsn=CONFIG.sentry_dsn,
39+
traces_sample_rate=CONFIG.sentry_traces_sample_rate,
40+
profiles_sample_rate=CONFIG.sentry_profiles_sample_rate,
41+
environment=CONFIG.sentry_environment,
42+
ignore_errors=[
43+
ConnectionResetError
44+
],
45+
)
46+
3547
if CONFIG.use_ssl:
3648
if not getattr(CONFIG, 'ssl_config'):
3749
raise ValueError('Empty SSL configuration section')

alts/worker/runners/base.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
import os
55
import random
66
import re
7-
import signal
87
import shutil
8+
import signal
99
import tempfile
1010
import time
1111
import traceback
@@ -18,15 +18,16 @@
1818
Dict,
1919
List,
2020
Optional,
21-
Union,
2221
Tuple,
2322
Type,
23+
Union,
2424
)
2525

2626
from billiard.exceptions import SoftTimeLimitExceeded
2727
from filelock import FileLock
2828
from mako.lookup import TemplateLookup
29-
from plumbum import local, ProcessExecutionError, ProcessTimedOut
29+
from plumbum import ProcessExecutionError, ProcessTimedOut, local
30+
from sentry_sdk import capture_message
3031

3132
from alts.shared.constants import COMMAND_TIMEOUT_EXIT_CODE
3233
from alts.shared.exceptions import (
@@ -599,6 +600,7 @@ def run_ansible_command(
599600
stderr = f'Timeout occurred when running ansible command: "{formulated_cmd}"'
600601
exit_code = COMMAND_TIMEOUT_EXIT_CODE
601602
exception_happened = True
603+
capture_message(stderr)
602604
except Exception as e:
603605
self._logger.error(
604606
'Unknown error happened during %s execution: %s',
@@ -759,7 +761,7 @@ def initial_provision(self, verbose=False):
759761
)
760762
if exit_code == COMMAND_TIMEOUT_EXIT_CODE:
761763
return 1, '', f'Provision has timed out: {out}\n{err}'
762-
elif exit_code != 0:
764+
if exit_code != 0:
763765
return 1, '', f'Provision exited abnormally: {out}\n{err}'
764766
return exit_code, out, err
765767

requirements/base.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@ pulpcore-client==3.61.0
1212
pydantic==2.9.2
1313
pyone==6.10.0
1414
requests==2.32.3
15+
sentry-sdk==2.22.0
1516
syncer==2.0.3
1617
tap.py==3.1

0 commit comments

Comments
 (0)