Skip to content

feat(celery): allow custom monitor config for beat tasks - #7839

Draft
alexon1234 wants to merge 1 commit into
getsentry:masterfrom
alexon1234:feat/celery-beat-monitor-config
Draft

alexon1234 wants to merge 1 commit into
getsentry:masterfrom
alexon1234:feat/celery-beat-monitor-config

Conversation

@alexon1234

Copy link
Copy Markdown

Description

Celery Beat auto-instrumentation (CeleryIntegration(monitor_beat_tasks=True)) derives the cron monitor config from the schedule only (schedule + timezone), so there is no way to set max_runtime, checkin_margin, failure_issue_threshold, recovery_threshold, or owner on the monitors the SDK creates.

Consequence: a task that can legitimately run longer than Sentry's default 30-minute max_runtime is reported as a timed-out cron failure even though it completes successfully. The only workaround is to exclude it from auto-instrumentation and re-declare the schedule in a manual @sentry_sdk.monitor(..., monitor_config=...) call, which is awkward when the schedule differs per environment.

This adds a beat_task_monitor_config option to CeleryIntegration: a mapping of Celery Beat task name to a partial MonitorConfig that is merged over the config derived from the schedule.

import sentry_sdk
from sentry_sdk.integrations.celery import CeleryIntegration

sentry_sdk.init(
    integrations=[
        CeleryIntegration(
            monitor_beat_tasks=True,
            beat_task_monitor_config={
                "nightly-cleanup": {"max_runtime": 120, "checkin_margin": 10},
            },
        ),
    ],
)

The SDK still derives schedule and timezone; the override only needs the extra fields, and can still override the derived values if explicitly provided.

Changes:

  • CeleryIntegration: new beat_task_monitor_config option.
  • _get_monitor_config: accepts and merges monitor config overrides.
  • _apply_crons_data_to_schedule_entry: looks up the override for the schedule entry and passes it through (covers both the default scheduler and RedBeat, which share this path).
  • Tests: merge into the derived config, schedule override, and per-task matching (unmatched tasks are unaffected).

Issues

Reminders

Celery Beat auto-instrumentation derives the cron monitor config from the
schedule only, so max_runtime, checkin_margin, failure_issue_threshold,
recovery_threshold and owner cannot be set. Tasks whose runtime can exceed
Sentry's default 30-minute max_runtime are then reported as timed-out cron
failures even when they complete successfully.

Add a `beat_task_monitor_config` option to CeleryIntegration that maps beat
task names to partial monitor configs, merged over the derived config.

Refs getsentry#7838

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Celery Beat auto-instrumentation: allow configuring monitor limits (max_runtime, checkin_margin, ...)

1 participant