Repository navigation
Conversation
Send check-ins around each run of a Huey periodic task when HueyIntegration(monitor_periodic_tasks=True) is set. Huey crontab() schedules are sent as the monitor config when Huey runs in UTC, so Sentry creates the monitor and keeps it in sync. Co-Authored-By: Claude <noreply@anthropic.com>
Coverage Results 📊✅ 133758 passed | ⏭️ 7293 skipped | Total: 141051 | Pass Rate: 94.83% | Execution Time: 444m 38s 📊 Comparison with Base Branch
All tests are passing successfully. ✅ Patch coverage is 96.00% (48 of 50 changed executable lines covered; target 80%). Changed files with executable lines (1)
Coverage diff@@ Coverage Diff @@
## master #8052 +/-##
==========================================
+ Coverage 90.26% 90.27% +0.01%
==========================================
Files 202 202 —
Tracked lines 26660 26709 +49
Branches 9926 9950 +24
==========================================
+ Hits 24062 24109 +47
- Misses 2598 2600 +2
- Partials 1510 1512 +2Generated by Coverage Action |
| import sentry_sdk | ||
| from sentry_sdk.api import continue_trace, get_baggage, get_traceparent | ||
| from sentry_sdk.consts import OP, SPANDATA, SPANSTATUS | ||
| from sentry_sdk.crons import monitor |
There was a problem hiding this comment.
Huey control-flow exceptions produce error check-ins
When CancelExecution, TaskLockedException, or RetryTask propagates from a monitored periodic task, the monitor records an error check-in even though Huey’s exception handler treats these as control-flow exceptions rather than failures. Consider applying that exception handling to the check-in status as well.
Evidence
_monitor_periodic_task()wraps the task’sexecutefunction withmonitor(...)when periodic-task monitoring is enabled.patch_execute()first wrapstask.executewith_wrap_task_execute(), then applies the monitor wrapper, so propagated exceptions reach the monitor._capture_exception()recognizes Huey control-flow exceptions and returns without capturing an error event, but_wrap_task_execute()re-raises them.monitor.__exit__()sets the check-in status toerrorwhenever an exception reaches it.
Also found at 2 additional locations
tests/integrations/huey/test_huey_crons.py:30-39sentry_sdk/integrations/huey.py:293-293
Identified by Warden · code-review · BY4-E5D
| _run_periodic_tasks(huey) | ||
|
|
||
| check_ins = _check_ins(envelopes) | ||
| assert [c["status"] for c in check_ins] == ["in_progress", "error"] | ||
| assert {c["monitor_slug"] for c in check_ins} == {"nightly"} | ||
|
|
||
| events = [envelope.get_event() for envelope in envelopes] | ||
| (error,) = [e for e in events if e is not None and e.get("exception")] | ||
| assert error["exception"]["values"][0]["type"] == "ValueError" |
There was a problem hiding this comment.
Error-path test lets Huey's re-raised exception escape
test_periodic_task_error calls _run_periodic_tasks() without handling the task's ValueError. Huey's execution path re-raises task failures, so the test exits before checking the error check-in or captured event. Catch the expected ValueError (for example, with pytest.raises) around the helper call, then run the assertions.
Evidence
test_periodic_task_errordefinesfailing()to raiseValueErrorand calls_run_periodic_tasks(huey)directly before its assertions._run_periodic_tasks()callshuey.execute(task)without catching exceptions._wrap_task_execute()insentry_sdk/integrations/huey.pycaptures task exceptions and re-raises them; the existingexecute_huey_task()test helper also catches exceptions aroundhuey.execute(task)for failing tasks.- The
monitordecorator records the error check-in but does not suppress the exception, so the call aborts before the new test's check-in and event assertions.
Identified by Warden · code-review · BCZ-G9S
| def _get_crontab_field(values: "List[int]", first: int, last: int) -> str: | ||
| full = list(range(first, last + 1)) | ||
| if values == full: | ||
| return "*" | ||
| if len(values) > 2: | ||
| step = values[1] - values[0] | ||
| if values == list(range(values[0], values[-1] + 1, step)): | ||
| if step == 1: | ||
| return "%d-%d" % (values[0], values[-1]) | ||
| if values == full[::step]: | ||
| return "*/%d" % step | ||
| return "%d-%d/%d" % (values[0], values[-1], step) | ||
| return ",".join(str(v) for v in values) |
There was a problem hiding this comment.
Huey cron settings sets break periodic-task monitor configuration
Huey stores cron fields as sets, but _get_crontab_field treats them as ordered lists. For normal fields with more than two values, indexing a set raises TypeError; the internal-exception guard then prevents the monitor wrapper from being applied, so the periodic task runs without check-ins. Sort the values before comparing or indexing them.
Evidence
_get_monitor_config()passes Huey’scron_settingsfields directly to_get_crontab_field(); only weekdays are sorted first._get_crontab_field()indexesvalues[0]andvalues[1]when the field has more than two values, which fails when Huey supplies a set.patch_execute()calls_monitor_periodic_task()insidecapture_internal_exceptions(); if config generation fails, the monitor decorator is not applied and the task still executes without check-ins.
Identified by Warden · find-bugs · X55-2A3
Description
Part of a series adding automatic Crons monitoring for the most-used job schedulers in each SDK. Check-ins for a monitor that doesn't exist yet are dropped, so today each monitor has to be created by hand with its schedule copied from the code. Sending the job's schedule as the monitor config lets Sentry create the monitor and keep it in sync.
This adds opt-in check-ins for Huey periodic tasks (
@huey.periodic_task(...)), modelled onCeleryIntegration(monitor_beat_tasks=True, exclude_beat_tasks=[...]):in_progresswhen a periodic task starts andokorerrorwhen it ends. Regular tasks are not affected.name=or the function name).exclude_periodic_taskstakes regexes, matched likeexclude_beat_tasks.crontab(...)schedules are sent as a crontab in UTC. The crontab is rebuilt from the values Huey parsed, so it matches what Huey runs.validate_datetimefunctions, for crontabs that restrict bothdayandday_of_week(Huey requires both to match, cron either), and whenutc=False, since Huey then runs in the system's local time.Issues
Docs: getsentry/sentry-docs#20057