Skip to content

Latest commit

 

History

History
100 lines (73 loc) · 3.52 KB

File metadata and controls

100 lines (73 loc) · 3.52 KB
title Set Up Crons
sidebar_title Crons
description Sentry Crons allows you to monitor the uptime and performance of any scheduled, recurring job in your application.
sidebar_order 9
sidebar_section features

Once implemented, it'll allow you to get alerts and metrics to help you solve errors, detect timeouts, and prevent disruptions to your service.

Requirements

Configuring Cron Monitors

You can create and update your monitors programmatically with code rather than creating and configuring them in Sentry.io. If the monitor doesn't exist in Sentry yet, it will be created.

To create or update a monitor, use monitor as outlined above and pass in your monitor configuration as monitor_config. This requires SDK version 1.45.0 or higher.

# All keys except `schedule` are optional
monitor_config = {
    "schedule": {"type": "crontab", "value": "0 0 * * *"},
    "timezone": "Europe/Vienna",
    # If an expected check-in doesn't come in `checkin_margin`
    # minutes, it'll be considered missed
    "checkin_margin": 10,
    # The check-in is allowed to run for `max_runtime` minutes
    # before it's considered failed
    "max_runtime": 10,
    # It'll take `failure_issue_threshold` consecutive failed
    # check-ins to create an issue
    "failure_issue_threshold": 5,
    # It'll take `recovery_threshold` OK check-ins to resolve
    # an issue
    "recovery_threshold": 5,
    # Assign ownership to a team or user (format: "team:6" or "user:51")
    "owner": "team:6",
}

@monitor(monitor_slug='<monitor-slug>', monitor_config=monitor_config)
def tell_the_world():
    print('My scheduled task...')

If you're using manual check-ins, you can pass your monitor_config to the capture_checkin call:

check_in_id = capture_checkin(
    monitor_slug='<monitor-slug>',
    status=MonitorStatus.IN_PROGRESS,
    monitor_config=monitor_config,
)

Manual Check-Ins

Check-in monitoring allows you to track a job's progress by capturing two check-ins: one at the start of your job and another at the end of your job. This two-step process allows Sentry to notify you if your job didn't start when expected (missed) or if it exceeded its maximum runtime (failed).

If you use the monitor decorator/context manager, the SDK will create check-ins for the wrapped code automatically.

from sentry_sdk.crons import capture_checkin
from sentry_sdk.crons.consts import MonitorStatus

check_in_id = capture_checkin(
    monitor_slug='<monitor-slug>',
    status=MonitorStatus.IN_PROGRESS,
)

# Execute your task here...

capture_checkin(
    monitor_slug='<monitor-slug>',
    check_in_id=check_in_id,
    status=MonitorStatus.OK,
)

Alerts

When your recurring job fails to check in (missed), runs beyond its configured maximum runtime (failed), or manually reports a failure, Sentry will create an error event with a tag to your monitor.

To receive alerts about these events:

  1. Navigate to Alerts in the sidebar.
  2. Create a new alert and select "Issues" under "Errors" as the alert type.
  3. Configure your alert and define a filter match to use: The event's tags match {key} {match} {value}.

Example: The event's tags match monitor.slug equals my-monitor-slug-here

Learn more in Issue Alert Configuration.