Skip to content

Added documentation for Celery Beat auto monitoring #6526

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 7 commits into from
Apr 4, 2023
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
75 changes: 59 additions & 16 deletions src/platform-includes/crons/setup/python.celery.mdx
Original file line number Diff line number Diff line change
@@ -1,46 +1,89 @@
## Job Monitoring
## Celery Beat Auto Discovery

Use the Celery Beat integration to monitor and notify you if your job is missed (or doesn't start when expected), if it fails due to a problem in the runtime (such as an error), or if it fails by exceeding its maximum runtime.
Use the Celery integration to monitor your [Celery periodic tasks](https://docs.celeryq.dev/en/stable/userguide/periodic-tasks.html) and get notified when a task is missed (or doesn't start when expected), if it fails due to a problem in the runtime (such as an error), or if it fails by exceeding its maximum runtime.

First, set up your Celery beat configuration:
First, set up your Celery beat schedule:

```python
from celery import Celery, signals
# tasks.py
from celery import Celery
from celery.schedules import crontab

import sentry_sdk
from sentry_sdk.crons import monitor
from sentry_sdk.integrations.celery import CeleryIntegration

app = Celery('mytasks', broker='redis://localhost:6379/0')
app = Celery('tasks', broker='...')
app.conf.beat_schedule = {
'set-in-beat-schedule': {
'task': 'tasks.tell_the_world',
'schedule': crontab(hour='10', minute='15'),
'args': ("in beat_schedule set", ),
'args': ("Some important message!", ),
},
}
```

Initialize Sentry either in `celeryd_init` or `beat_init` signal:
Initialize Sentry in the `celeryd_init` or `beat_init` signal. Make sure to set `monitor_beat_tasks=True` in `CeleryIntegration`:

```python
#@signals.celeryd_init.connect
@signals.beat_init.connect
# tasks.py
from celery import signals

import sentry_sdk
from sentry_sdk.integrations.celery import CeleryIntegration


#@signals.beat_init.connect
@signals.celeryd_init.connect
def init_sentry(**kwargs):
sentry_sdk.init(
dsn='___PUBLIC_DSN___',
integrations=[CeleryIntegration()],
integrations=[CeleryIntegration(monitor_beat_tasks=True)], # 👈
environment="local.dev.grace",
release="v1.0.7-a1",
release="v1.0",
)
```

Finally, link your Celery task to a Sentry Cron Monitor:
Once Sentry Crons is set up, tasks in your Celery beat schedule will be auto-discoverable, and telemetry data will be captured when a task is started, when it finishes, and when it fails.

Start your Celery beat and worker services and see your tasks being monitored at [https://sentry.io/crons/](https://sentry.io/crons/).

<Note>

You don't need to create Cron Monitors for your tasks on Sentry.io, we'll do it for you.

</Note>

## Manual Task Monitoring

We provide a lightweight decorator to make monitoring individual tasks easier. To use it, add `@sentry_sdk.monitor` to your Celery task (or any function), then supply a `monitor_slug` of a monitor created previously on Sentry.io. Once this is done, every time the task (or function) is executed, telemetry data will be captured when a task is started, when it finishes, and when it fails.

<Note>

Make sure the Sentry `@sentry_sdk.monitor` decorator is below Celery's `@app.task` decorator.

</Note>

```python
# tasks.py
from celery import Celery, signals

import sentry_sdk
from sentry_sdk.integrations.celery import CeleryIntegration


app = Celery('tasks', broker='...')


@signals.celeryd_init.connect
def init_sentry(**kwargs):
sentry_sdk.init(
dsn='___PUBLIC_DSN___',
integrations=[CeleryIntegration()],
environment="local.dev.grace",
release="v1.0",
)


@app.task
@monitor(monitor_slug='<monitor-slug>')
@sentry_sdk.monitor(monitor_slug='<monitor-slug>') # 👈 this is the new line.
def tell_the_world(msg):
print(msg)
```
10 changes: 8 additions & 2 deletions src/platform-includes/crons/setup/python.mdx
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
## Job Monitoring

Use the Python SDK to monitor and notify you if your job is missed (or doesn't start when expected), if it fails due to a problem in the runtime (such as an error), or if it fails by exceeding its maximum runtime.
Use the Python SDK to monitor and notify you if your periodic task is missed (or doesn't start when expected), if it fails due to a problem in the runtime (such as an error), or if it fails by exceeding its maximum runtime.

<Alert level="info" title="Note on Celery">

If you're using **Celery Beat** to run your periodic tasks, have a look at our [Celery Beat Auto Discovery documentation](/platforms/python/guides/celery/crons/).

</Alert>

```python
import sentry_sdk
from sentry_sdk.crons import monitor

# Add the @monitor decorator to your job
# Add the @monitor decorator to your task
@monitor(monitor_slug='<monitor-slug>')
def tell_the_world(msg):
# Scheduled task here...
Expand Down