Skip to content

Commit 4f0ab40

Browse files
Do not support sub-minute cron intervals (#2172)
* Do not support sub-minute cron intervals * Do not send checkins for unsupported schedule types --------- Co-authored-by: Ivana Kellyerova <[email protected]>
1 parent fe7e501 commit 4f0ab40

File tree

2 files changed

+55
-24
lines changed

2 files changed

+55
-24
lines changed

sentry_sdk/integrations/celery.py

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ def _get_humanized_interval(seconds):
375375
interval = int(seconds / divider)
376376
return (interval, unit)
377377

378-
return (1, "minute")
378+
return (int(seconds), "second")
379379

380380

381381
def _get_monitor_config(celery_schedule, app):
@@ -400,6 +400,12 @@ def _get_monitor_config(celery_schedule, app):
400400
celery_schedule.seconds
401401
)
402402

403+
if schedule_unit == "second":
404+
logger.warning(
405+
"Intervals shorter than one minute are not supported by Sentry Crons."
406+
)
407+
return {}
408+
403409
else:
404410
logger.warning(
405411
"Celery schedule type '%s' not supported by Sentry Crons.",
@@ -441,24 +447,27 @@ def sentry_apply_entry(*args, **kwargs):
441447

442448
monitor_config = _get_monitor_config(celery_schedule, app)
443449

444-
headers = schedule_entry.options.pop("headers", {})
445-
headers.update(
446-
{
447-
"sentry-monitor-slug": monitor_name,
448-
"sentry-monitor-config": monitor_config,
449-
}
450-
)
451-
452-
check_in_id = capture_checkin(
453-
monitor_slug=monitor_name,
454-
monitor_config=monitor_config,
455-
status=MonitorStatus.IN_PROGRESS,
456-
)
457-
headers.update({"sentry-monitor-check-in-id": check_in_id})
450+
is_supported_schedule = bool(monitor_config)
451+
if is_supported_schedule:
452+
headers = schedule_entry.options.pop("headers", {})
453+
headers.update(
454+
{
455+
"sentry-monitor-slug": monitor_name,
456+
"sentry-monitor-config": monitor_config,
457+
}
458+
)
459+
460+
check_in_id = capture_checkin(
461+
monitor_slug=monitor_name,
462+
monitor_config=monitor_config,
463+
status=MonitorStatus.IN_PROGRESS,
464+
)
465+
headers.update({"sentry-monitor-check-in-id": check_in_id})
466+
467+
# Set the Sentry configuration in the options of the ScheduleEntry.
468+
# Those will be picked up in `apply_async` and added to the headers.
469+
schedule_entry.options["headers"] = headers
458470

459-
# Set the Sentry configuration in the options of the ScheduleEntry.
460-
# Those will be picked up in `apply_async` and added to the headers.
461-
schedule_entry.options["headers"] = headers
462471
return original_apply_entry(*args, **kwargs)
463472

464473
Scheduler.apply_entry = sentry_apply_entry

tests/integrations/celery/test_celery_beat_crons.py

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,11 @@ def test_get_headers():
5959
@pytest.mark.parametrize(
6060
"seconds, expected_tuple",
6161
[
62-
(0, (1, "minute")),
63-
(0.00001, (1, "minute")),
64-
(1, (1, "minute")),
62+
(0, (0, "second")),
63+
(1, (1, "second")),
64+
(0.00001, (0, "second")),
65+
(59, (59, "second")),
66+
(60, (1, "minute")),
6567
(100, (1, "minute")),
6668
(1000, (16, "minute")),
6769
(10000, (2, "hour")),
@@ -205,13 +207,12 @@ def test_crons_task_retry():
205207
)
206208

207209

208-
def test_get_monitor_config():
210+
def test_get_monitor_config_crontab():
209211
app = MagicMock()
210212
app.conf = MagicMock()
211213
app.conf.timezone = "Europe/Vienna"
212214

213215
celery_schedule = crontab(day_of_month="3", hour="12", minute="*/10")
214-
215216
monitor_config = _get_monitor_config(celery_schedule, app)
216217
assert monitor_config == {
217218
"schedule": {
@@ -222,8 +223,23 @@ def test_get_monitor_config():
222223
}
223224
assert "unit" not in monitor_config["schedule"]
224225

225-
celery_schedule = schedule(run_every=3)
226226

227+
def test_get_monitor_config_seconds():
228+
app = MagicMock()
229+
app.conf = MagicMock()
230+
app.conf.timezone = "Europe/Vienna"
231+
232+
celery_schedule = schedule(run_every=3) # seconds
233+
monitor_config = _get_monitor_config(celery_schedule, app)
234+
assert monitor_config == {}
235+
236+
237+
def test_get_monitor_config_minutes():
238+
app = MagicMock()
239+
app.conf = MagicMock()
240+
app.conf.timezone = "Europe/Vienna"
241+
242+
celery_schedule = schedule(run_every=60) # seconds
227243
monitor_config = _get_monitor_config(celery_schedule, app)
228244
assert monitor_config == {
229245
"schedule": {
@@ -234,6 +250,12 @@ def test_get_monitor_config():
234250
"timezone": "Europe/Vienna",
235251
}
236252

253+
254+
def test_get_monitor_config_unknown():
255+
app = MagicMock()
256+
app.conf = MagicMock()
257+
app.conf.timezone = "Europe/Vienna"
258+
237259
unknown_celery_schedule = MagicMock()
238260
monitor_config = _get_monitor_config(unknown_celery_schedule, app)
239261
assert monitor_config == {}

0 commit comments

Comments
 (0)