Set Up Crons

Learn how to set up Crons with the Go SDK to monitor the uptime and performance of any scheduled, recurring job in your application.

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

Check-in monitoring allows you to track a job's progress by completing 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).

Copied
// 🟡 Notify Sentry your job is running:
checkinId := sentry.CaptureCheckIn(
	&sentry.CheckIn{
		MonitorSlug: "<monitor-slug>",
		Status:      sentry.CheckInStatusInProgress,
	},
	nil,
)

// Execute your scheduled task here...

// 🟢 Notify Sentry your job has completed successfully:
sentry.CaptureCheckIn(
	&sentry.CheckIn{
		ID:          *checkinId,
		MonitorSlug: "<monitor-slug>",
		Status:      sentry.CheckInStatusOK,
	},
	nil,
)

If your job execution fails, you can notify Sentry about the failure:

Copied
// 🔴 Notify Sentry your job has failed:
sentry.CaptureCheckIn(
	&sentry.CheckIn{
		ID:          *checkinId,
		MonitorSlug: "<monitor-slug>",
		Status:      sentry.CheckInStatusError,
	},
	nil,
)

Heartbeat monitoring notifies Sentry of a job's status through one check-in. This setup will only notify you if your job didn't start when expected (missed). If you need to track a job to see if it exceeded its maximum runtime (failed), use check-ins instead.

Copied
// Execute your scheduled task...

// 🟢 Notify Sentry your job completed successfully:
sentry.CaptureCheckIn(
	&sentry.CheckIn{
		MonitorSlug: "<monitor-slug>",
		Status:      sentry.CheckInStatusOK,
		// Specify the duration of the job.
		Duration:    time.Second * 10,
	},
	nil,
)

If your job execution fails, you can:

Copied
// 🔴 Notify Sentry your job has failed:
sentry.CaptureCheckIn(
	&sentry.CheckIn{
		MonitorSlug: "<monitor-slug>",
		Status:      sentry.CheckInStatusError,
		Duration:    time.Second * 10,
	},
	nil,
)

You can create and update your Monitors programmatically with code rather than creating and configuring them in Sentry.io.

Copied
// Create a crontab schedule object (every 10 minutes)
monitorSchedule := sentry.CrontabSchedule("*/10 * * * *")

// Or create an interval schedule object (every 10 minutes)
monitorSchedule := sentry.IntervalSchedule(10, sentry.MonitorScheduleUnitMinute)

Supported units are:

  • sentry.MonitorScheduleUnitMinute
  • sentry.MonitorScheduleUnitHour
  • sentry.MonitorScheduleUnitDay
  • sentry.MonitorScheduleUnitWeek
  • sentry.MonitorScheduleUnitMonth
  • sentry.MonitorScheduleUnitYear
Copied
// Create a monitor config object
monitorConfig := &sentry.MonitorConfig{
	Schedule:      monitorSchedule,
	MaxRuntime:    2,
	CheckInMargin: 1,
	FailureIssueThreshold: 2,
}

// 🟡 Notify Sentry your job is running:
checkinId := sentry.CaptureCheckIn(
	&sentry.CheckIn{
		MonitorSlug: "<monitor-slug>",
		Status:      sentry.CheckInStatusInProgress,
	},
	monitorConfig,
)

// Execute your scheduled task here...

// 🟢 Notify Sentry your job has completed successfully:
sentry.CaptureCheckIn(
	&sentry.CheckIn{
		MonitorSlug: "<monitor-slug>",
		Status:      sentry.CheckInStatusOK,
	},
	monitorConfig,
)

A full end-to-end example can be found in the sentry-go repository.

To link any exceptions captured during your job's lifecycle, use Sentry's context with your monitor slug.

Copied
sentry.ConfigureScope(func(scope *sentry.Scope) {
	scope.SetContext("monitor", sentry.Context{"slug": "<monitor-slug>"})
})

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

Cron completed alert filter

Learn more in Issue Alert Configuration.

Crons imposes a rate limit on check-ins to prevent abuse and resource overuse. Specifically, you can only send a maximum of 6 check-ins per minute per existing monitor environment. This limit is enforced on a per-project basis, meaning that the rate limit applies collectively to all monitor environments within a given project. You can check if any of your check-ins are being dropped in the Usage Stats page.

To avoid dropped check-ins, it is crucial to manage and distribute your check-ins efficiently within the rate limits. This will help maintain accurate monitoring and ensure that all critical check-ins are captured and processed.

Help improve this content
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").