diff --git a/platform-includes/crons/setup/go.mdx b/platform-includes/crons/setup/go.mdx index 60d4926e327a3..b64fe32dde073 100644 --- a/platform-includes/crons/setup/go.mdx +++ b/platform-includes/crons/setup/go.mdx @@ -46,6 +46,38 @@ Pass `nil` as the last argument to use the default monitor settings. Specs Sentr With the default `time.Local` location, the timezone comes from `$TZ` or `/etc/localtime`. If that isn't an IANA name, such as `America/New_York`, only check-ins are sent, so set the schedule in Sentry or use `cron.WithLocation`. +### gocron + +If you schedule jobs with [gocron v2](https://github.com/go-co-op/gocron) (v2.22.0 or higher), install the package: + +```bash +go get github.com/getsentry/sentry-go/gocron +``` + +Pass `sentrygocron.NewSchedulerMonitor` to `gocron.WithSchedulerMonitor` to monitor every job. Each job name becomes a monitor slug, and each job's schedule and the scheduler's location become the monitor schedule and timezone. Check-ins go to the client carried by the context, or the global client: + +```go +import ( + "context" + "time" + + "github.com/go-co-op/gocron/v2" + sentrygocron "github.com/getsentry/sentry-go/gocron" +) + +s, err := gocron.NewScheduler( + gocron.WithLocation(time.UTC), + gocron.WithSchedulerMonitor(sentrygocron.NewSchedulerMonitor(context.Background(), sentrygocron.Options{ + // Same as gocron.WithLocation. Defaults to time.Local. + Location: time.UTC, + Exclude: []string{""}, + })), +) +s.NewJob(gocron.CronJob("0 * * * *", false), gocron.NewTask(task), gocron.WithName("")) +``` + +Name your jobs with `gocron.WithName`. Jobs running an anonymous function without a name, and jobs sharing a name, aren't monitored. `Exclude` skips jobs by exact name, and `ExcludePatterns` by regular expression. Random and one-time jobs, and schedules that can't be expressed in the Crons schedule format, aren't monitored. For example, a job that runs every 10 seconds, every 2 days, or on the last day of the month. The gocron integration ships in the next release of `sentry-go`. + ### Manual Check-Ins To send check-ins yourself, call `sentry.CaptureCheckIn` at the start and end of your job. Pass a monitor config with the in-progress check-in so Sentry creates or updates the monitor from your code: