Skip to content

Add documentation for Crons in Elixir #9305

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions docs/platforms/elixir/crons/index.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---
title: Set Up Crons
description: "Learn how to enable Cron Monitoring in your app"
sidebar_order: 5000
---

Sentry Crons allows you to monitor the uptime and performance of any scheduled, recurring job. 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

<PlatformContent includePath="crons/requirements" />

<PlatformContent includePath="crons/setup" />

## 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 connected 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](/platforms/ruby/common/crons/crons-alerts-example.png)

Learn more in [Issue Alert Configuration](/product/alerts/create-alerts/issue-alert-config/).
25 changes: 25 additions & 0 deletions docs/platforms/elixir/crons/troubleshooting.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
title: Troubleshooting
description: "Learn how to troubleshoot your Cron Monitoring setup."
sidebar_order: 500
---

<PlatformContent includePath="crons/troubleshooting" />

### Why Aren't Recurring Job Errors Showing Up on My Monitor Details Page?

You may not have <PlatformLink to="/crons/#connecting-errors-to-cron-monitors">linked errors to your monitor</PlatformLink>.

### Why Am I Not Receiving Alerts When My Monitor Fails?

You may not have <PlatformLink to="/crons/#alerts">set up alerts for your monitor</PlatformLink>.

### What Is the Crons Data Retention Policy for Check-ins and Attachments?

Our current data retention policy is 90 days.

### Do You Support a Monitor Schedule With a Six-field Crontab Expression?

Currently, we only support crontab expressions with five fields.

Still having trouble? Reach out to [[email protected]](mailto:[email protected]).
1 change: 1 addition & 0 deletions docs/product/crons/getting-started/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ To set up Sentry Crons, use the links below for supported SDKs or the Sentry CLI
- [Java](/platforms/java/crons/)
- [Spring Boot](/platforms/java/guides/spring-boot/crons/)
- [Ruby](/platforms/ruby/crons/)
- [Elixir](/platforms/elixir/crons/)

<Alert level="note" title="Don't see your platform?">

Expand Down
2 changes: 2 additions & 0 deletions platform-includes/crons/requirements/elixir.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Use our <PlatformLink to="/">getting started</PlatformLink> guide to install and configure the Sentry Elixir SDK (min v10.2.0) for your recurring job.
- [Create and configure](https://sentry.io/crons/create/) your first monitor.
99 changes: 99 additions & 0 deletions platform-includes/crons/setup/elixir.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
## Job Monitoring

If you're using [Oban](https://github.com/sorentwo/oban) or [Quantum](https://github.com/quantum-elixir/quantum-core), Sentry can automatically capture check-ins for all jobs that are scheduled to run periodically. To achieve this, you need to enable the corresponding Sentry plugin:

```elixir
config :sentry,
integrations: [
oban: [cron: [enabled: true]],
# Or for Quantum:
quantum: [cron: [enabled: true]],
]
```

## Manual Setup

If you're using another library or a custom solution for scheduling jobs, you'll need to instrument those jobs manually.

### Check-Ins (Recommended)

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).

```elixir
{:ok, check_in_id} = Sentry.capture_check_in(status: :in_progress, monitor_slug: "<monitor-slug>")

# Execute your scheduled task here
my_scheduled_job()

Sentry.capture_check_in(check_in_id: check_in_id, status: :ok, monitor_slug: "<monitor-slug>")
```

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

```elixir
Sentry.capture_check_in(check_in_id: check_in_id, status: :error, monitor_slug: "<monitor-slug>")
```

### Heartbeats

Heartbeat monitoring notifies Sentry of a job's status through _a single 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.

```elixir
Sentry.capture_check_in(status: :ok, monitor_slug: "<monitor-slug>")
```

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

```elixir
Sentry.capture_check_in(status: :error, monitor_slug: "<monitor-slug>")
```

## Upserting Cron Monitors

You can create and update your monitors programmatically with code rather than [creating and configuring them in Sentry](https://sentry.io/crons/create/).

To do that, you need to pass a `:monitor_config` set of options to `Sentry.capture_check_in/3`:

```elixir
# Create a config from a crontab schedule (every 10 minutes)
monitor_config = [
schedule: [
type: :crontab,
value: "5 * * * *",
],
checkin_margin: 5, # Optional check-in margin in minutes
max_runtime: 15, # Optional max runtime in minutes
timezone: "Europe/Vienna", # Optional timezone
]

# Alternatively, create a config from an interval schedule (every 10 minutes in this case):
monitor_config = [
schedule: [
type: :interval,
unit: :minute,
value: 10
],
checkin_margin: 5, # Optional check-in margin in minutes
max_runtime: 15, # Optional max runtime in minutes
timezone: "Europe/Vienna", # Optional timezone
]

# Notify Sentry your job is running:
{:ok, check_in_id} =
Sentry.capture_check_in(
status: :in_progress,
monitor_slug: "<monitor-slug>",
monitor_config: monitor_config
)

# Execute your job:
execute_job()

# Notify Sentry your job has completed successfully:
Sentry.capture_check_in(
status: :ok,
check_in_id: check_in_id,
monitor_slug: "<monitor-slug>",
monitor_config: monitor_config
)
```
3 changes: 3 additions & 0 deletions platform-includes/crons/troubleshooting/elixir.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
### How Do I Send an Attachment With a Check-in (Such as a Log Output)?

Attachments aren't supported by our Elixir SDK yet. For now, you can use the [check-in attachments API](/product/crons/getting-started/http/#check-in-attachment-optional).