Skip to content

Commit 1c229f2

Browse files
committed
Merge branch 'master' into sentry-sdk-2.0
2 parents d9250aa + b85cd10 commit 1c229f2

File tree

2 files changed

+85
-11
lines changed

2 files changed

+85
-11
lines changed

sentry_sdk/crons/decorator.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
if TYPE_CHECKING:
1010
from types import TracebackType
1111
from typing import (
12+
Any,
1213
Awaitable,
1314
Callable,
1415
Optional,
@@ -51,15 +52,18 @@ def test(arg):
5152
```
5253
"""
5354

54-
def __init__(self, monitor_slug=None):
55-
# type: (Optional[str]) -> None
55+
def __init__(self, monitor_slug=None, monitor_config=None):
56+
# type: (Optional[str], Optional[dict[str, Any]]) -> None
5657
self.monitor_slug = monitor_slug
58+
self.monitor_config = monitor_config
5759

5860
def __enter__(self):
5961
# type: () -> None
6062
self.start_timestamp = now()
6163
self.check_in_id = capture_checkin(
62-
monitor_slug=self.monitor_slug, status=MonitorStatus.IN_PROGRESS
64+
monitor_slug=self.monitor_slug,
65+
status=MonitorStatus.IN_PROGRESS,
66+
monitor_config=self.monitor_config,
6367
)
6468

6569
def __exit__(self, exc_type, exc_value, traceback):
@@ -76,6 +80,7 @@ def __exit__(self, exc_type, exc_value, traceback):
7680
check_in_id=self.check_in_id,
7781
status=status,
7882
duration=duration_s,
83+
monitor_config=self.monitor_config,
7984
)
8085

8186
def __call__(self, fn):

tests/test_crons.py

Lines changed: 77 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,22 @@ async def _break_world_contextmanager_async(name):
5858
return "Hello, {}".format(name)
5959

6060

61+
@sentry_sdk.monitor(monitor_slug="ghi789", monitor_config=None)
62+
def _no_monitor_config():
63+
return
64+
65+
66+
@sentry_sdk.monitor(
67+
monitor_slug="ghi789",
68+
monitor_config={
69+
"schedule": {"type": "crontab", "value": "0 0 * * *"},
70+
"failure_issue_threshold": 5,
71+
},
72+
)
73+
def _with_monitor_config():
74+
return
75+
76+
6177
def test_decorator(sentry_init):
6278
sentry_init()
6379

@@ -70,7 +86,9 @@ def test_decorator(sentry_init):
7086
# Check for initial checkin
7187
fake_capture_checkin.assert_has_calls(
7288
[
73-
mock.call(monitor_slug="abc123", status="in_progress"),
89+
mock.call(
90+
monitor_slug="abc123", status="in_progress", monitor_config=None
91+
),
7492
]
7593
)
7694

@@ -95,7 +113,9 @@ def test_decorator_error(sentry_init):
95113
# Check for initial checkin
96114
fake_capture_checkin.assert_has_calls(
97115
[
98-
mock.call(monitor_slug="def456", status="in_progress"),
116+
mock.call(
117+
monitor_slug="def456", status="in_progress", monitor_config=None
118+
),
99119
]
100120
)
101121

@@ -118,7 +138,9 @@ def test_contextmanager(sentry_init):
118138
# Check for initial checkin
119139
fake_capture_checkin.assert_has_calls(
120140
[
121-
mock.call(monitor_slug="abc123", status="in_progress"),
141+
mock.call(
142+
monitor_slug="abc123", status="in_progress", monitor_config=None
143+
),
122144
]
123145
)
124146

@@ -143,7 +165,9 @@ def test_contextmanager_error(sentry_init):
143165
# Check for initial checkin
144166
fake_capture_checkin.assert_has_calls(
145167
[
146-
mock.call(monitor_slug="def456", status="in_progress"),
168+
mock.call(
169+
monitor_slug="def456", status="in_progress", monitor_config=None
170+
),
147171
]
148172
)
149173

@@ -219,6 +243,8 @@ def test_monitor_config(sentry_init, capture_envelopes):
219243

220244
monitor_config = {
221245
"schedule": {"type": "crontab", "value": "0 0 * * *"},
246+
"failure_issue_threshold": 5,
247+
"recovery_threshold": 5,
222248
}
223249

224250
capture_checkin(monitor_slug="abc123", monitor_config=monitor_config)
@@ -236,6 +262,41 @@ def test_monitor_config(sentry_init, capture_envelopes):
236262
assert "monitor_config" not in check_in
237263

238264

265+
def test_decorator_monitor_config(sentry_init, capture_envelopes):
266+
sentry_init()
267+
envelopes = capture_envelopes()
268+
269+
_with_monitor_config()
270+
271+
assert len(envelopes) == 2
272+
273+
for check_in_envelope in envelopes:
274+
assert len(check_in_envelope.items) == 1
275+
check_in = check_in_envelope.items[0].payload.json
276+
277+
assert check_in["monitor_slug"] == "ghi789"
278+
assert check_in["monitor_config"] == {
279+
"schedule": {"type": "crontab", "value": "0 0 * * *"},
280+
"failure_issue_threshold": 5,
281+
}
282+
283+
284+
def test_decorator_no_monitor_config(sentry_init, capture_envelopes):
285+
sentry_init()
286+
envelopes = capture_envelopes()
287+
288+
_no_monitor_config()
289+
290+
assert len(envelopes) == 2
291+
292+
for check_in_envelope in envelopes:
293+
assert len(check_in_envelope.items) == 1
294+
check_in = check_in_envelope.items[0].payload.json
295+
296+
assert check_in["monitor_slug"] == "ghi789"
297+
assert "monitor_config" not in check_in
298+
299+
239300
def test_capture_checkin_sdk_not_initialized():
240301
# Tests that the capture_checkin does not raise an error when Sentry SDK is not initialized.
241302
# sentry_init() is intentionally omitted.
@@ -320,7 +381,9 @@ async def test_decorator_async(sentry_init):
320381
# Check for initial checkin
321382
fake_capture_checkin.assert_has_calls(
322383
[
323-
mock.call(monitor_slug="abc123", status="in_progress"),
384+
mock.call(
385+
monitor_slug="abc123", status="in_progress", monitor_config=None
386+
),
324387
]
325388
)
326389

@@ -346,7 +409,9 @@ async def test_decorator_error_async(sentry_init):
346409
# Check for initial checkin
347410
fake_capture_checkin.assert_has_calls(
348411
[
349-
mock.call(monitor_slug="def456", status="in_progress"),
412+
mock.call(
413+
monitor_slug="def456", status="in_progress", monitor_config=None
414+
),
350415
]
351416
)
352417

@@ -370,7 +435,9 @@ async def test_contextmanager_async(sentry_init):
370435
# Check for initial checkin
371436
fake_capture_checkin.assert_has_calls(
372437
[
373-
mock.call(monitor_slug="abc123", status="in_progress"),
438+
mock.call(
439+
monitor_slug="abc123", status="in_progress", monitor_config=None
440+
),
374441
]
375442
)
376443

@@ -396,7 +463,9 @@ async def test_contextmanager_error_async(sentry_init):
396463
# Check for initial checkin
397464
fake_capture_checkin.assert_has_calls(
398465
[
399-
mock.call(monitor_slug="def456", status="in_progress"),
466+
mock.call(
467+
monitor_slug="def456", status="in_progress", monitor_config=None
468+
),
400469
]
401470
)
402471

0 commit comments

Comments
 (0)