Skip to content

ref(crons): Add information to short-interval cron error message #2246

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

Merged
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
10 changes: 6 additions & 4 deletions sentry_sdk/integrations/celery.py
Original file line number Diff line number Diff line change
Expand Up @@ -395,8 +395,8 @@ def _get_humanized_interval(seconds):
return (int(seconds), "second")


def _get_monitor_config(celery_schedule, app):
# type: (Any, Celery) -> Dict[str, Any]
def _get_monitor_config(celery_schedule, app, monitor_name):
# type: (Any, Celery, str) -> Dict[str, Any]
monitor_config = {} # type: Dict[str, Any]
schedule_type = None # type: Optional[str]
schedule_value = None # type: Optional[Union[str, int]]
Expand All @@ -419,7 +419,9 @@ def _get_monitor_config(celery_schedule, app):

if schedule_unit == "second":
logger.warning(
"Intervals shorter than one minute are not supported by Sentry Crons."
"Intervals shorter than one minute are not supported by Sentry Crons. Monitor '%s' has an interval of %s seconds. Use the `exclude_beat_tasks` option in the celery integration to exclude it.",
monitor_name,
schedule_value,
)
return {}

Expand Down Expand Up @@ -466,7 +468,7 @@ def sentry_apply_entry(*args, **kwargs):
# When tasks are started from Celery Beat, make sure each task has its own trace.
scope.set_new_propagation_context()

monitor_config = _get_monitor_config(celery_schedule, app)
monitor_config = _get_monitor_config(celery_schedule, app, monitor_name)

is_supported_schedule = bool(monitor_config)
if is_supported_schedule:
Expand Down
21 changes: 15 additions & 6 deletions tests/integrations/celery/test_celery_beat_crons.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ def test_get_monitor_config_crontab():
app.conf.timezone = "Europe/Vienna"

celery_schedule = crontab(day_of_month="3", hour="12", minute="*/10")
monitor_config = _get_monitor_config(celery_schedule, app)
monitor_config = _get_monitor_config(celery_schedule, app, "foo")
assert monitor_config == {
"schedule": {
"type": "crontab",
Expand All @@ -230,8 +230,17 @@ def test_get_monitor_config_seconds():
app.conf.timezone = "Europe/Vienna"

celery_schedule = schedule(run_every=3) # seconds
monitor_config = _get_monitor_config(celery_schedule, app)
assert monitor_config == {}

with mock.patch(
"sentry_sdk.integrations.celery.logger.warning"
) as mock_logger_warning:
monitor_config = _get_monitor_config(celery_schedule, app, "foo")
mock_logger_warning.assert_called_with(
"Intervals shorter than one minute are not supported by Sentry Crons. Monitor '%s' has an interval of %s seconds. Use the `exclude_beat_tasks` option in the celery integration to exclude it.",
"foo",
3,
)
assert monitor_config == {}


def test_get_monitor_config_minutes():
Expand All @@ -240,7 +249,7 @@ def test_get_monitor_config_minutes():
app.conf.timezone = "Europe/Vienna"

celery_schedule = schedule(run_every=60) # seconds
monitor_config = _get_monitor_config(celery_schedule, app)
monitor_config = _get_monitor_config(celery_schedule, app, "foo")
assert monitor_config == {
"schedule": {
"type": "interval",
Expand All @@ -257,7 +266,7 @@ def test_get_monitor_config_unknown():
app.conf.timezone = "Europe/Vienna"

unknown_celery_schedule = MagicMock()
monitor_config = _get_monitor_config(unknown_celery_schedule, app)
monitor_config = _get_monitor_config(unknown_celery_schedule, app, "foo")
assert monitor_config == {}


Expand All @@ -268,7 +277,7 @@ def test_get_monitor_config_default_timezone():

celery_schedule = crontab(day_of_month="3", hour="12", minute="*/10")

monitor_config = _get_monitor_config(celery_schedule, app)
monitor_config = _get_monitor_config(celery_schedule, app, "foo")

assert monitor_config["timezone"] == "UTC"

Expand Down