Skip to content

Commit bf6738e

Browse files
authored
feat(python): Improve crons docs, add upserts (#9597)
1 parent 5a45cf1 commit bf6738e

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

docs/platforms/python/crons/index.mdx

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,71 @@ Sentry Crons allows you to monitor the uptime and performance of any scheduled,
1212

1313
<PlatformContent includePath="crons/setup" />
1414

15+
## Configuring Cron Monitors
16+
17+
You can create and update your monitors programmatically with code rather than [creating and configuring them in Sentry.io](https://sentry.io/crons/create/). If the monitor doesn't exist in Sentry yet, it will be created.
18+
19+
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.
20+
21+
```python
22+
# All keys except `schedule` are optional
23+
monitor_config = {
24+
"schedule": {"type": "crontab", "value": "0 0 * * *"},
25+
"timezone": "Europe/Vienna",
26+
# If an expected check-in doesn't come in `checkin_margin`
27+
# minutes, it'll be considered missed
28+
"checkin_margin": 10,
29+
# The check-in is allowed to run for `max_runtime` minutes
30+
# before it's considered failed
31+
"max_runtime": 10,
32+
# It'll take `failure_issue_threshold` consecutive failed
33+
# check-ins to create an issue
34+
"failure_issue_threshold": 5,
35+
# It'll take `recovery_threshold` OK check-ins to resolve
36+
# an issue
37+
"recovery_threshold": 5,
38+
}
39+
40+
@monitor(monitor_slug='<monitor-slug>', monitor_config=monitor_config)
41+
def tell_the_world():
42+
print('My scheduled task...')
43+
```
44+
45+
If you're using [manual check-ins](#check-ins), you can pass your `monitor_config` to the `capture_checkin` call:
46+
47+
```python
48+
check_in_id = capture_checkin(
49+
monitor_slug='<monitor-slug>',
50+
status=MonitorStatus.IN_PROGRESS,
51+
monitor_config=monitor_config,
52+
)
53+
```
54+
55+
## Manual Check-Ins
56+
57+
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).
58+
59+
If you use the `monitor` decorator/context manager, the SDK will create check-ins for the wrapped code automatically.
60+
61+
```python
62+
from sentry_sdk.crons import capture_checkin
63+
from sentry_sdk.crons.consts import MonitorStatus
64+
65+
check_in_id = capture_checkin(
66+
monitor_slug='<monitor-slug>',
67+
status=MonitorStatus.IN_PROGRESS,
68+
)
69+
70+
# Execute your task here...
71+
72+
capture_checkin(
73+
monitor_slug='<monitor-slug>',
74+
check_in_id=check_in_id,
75+
status=MonitorStatus.OK,
76+
)
77+
```
78+
79+
1580
## Alerts
1681

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

0 commit comments

Comments
 (0)