Skip to content
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
5 changes: 3 additions & 2 deletions sentry_sdk/crons/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def capture_checkin(
status: "Optional[str]" = None,
duration: "Optional[float]" = None,
monitor_config: "Optional[MonitorConfig]" = None,
) -> str:
) -> "Optional[str]":
check_in_event = _create_check_in_event(
monitor_slug=monitor_slug,
check_in_id=check_in_id,
Expand All @@ -51,7 +51,8 @@ def capture_checkin(
monitor_config=monitor_config,
)

sentry_sdk.capture_event(check_in_event)
if sentry_sdk.capture_event(check_in_event) is None:
return None

logger.debug(
f"[Crons] Captured check-in ({check_in_event.get('check_in_id')}): {check_in_event.get('monitor_slug')} -> {check_in_event.get('status')}"
Expand Down
37 changes: 36 additions & 1 deletion tests/test_crons.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,42 @@ def test_capture_checkin_sdk_not_initialized():
status=None,
duration=None,
)
assert check_in_id == "112233"
assert check_in_id is None


def test_capture_checkin_dropped_by_before_send(sentry_init, capture_envelopes):
sentry_init(before_send=lambda event, hint: None)
envelopes = capture_envelopes()

check_in_id = capture_checkin(monitor_slug="abc123", status="in_progress")

assert check_in_id is None
assert envelopes == []
Comment thread
ericapisani marked this conversation as resolved.


def test_context_monitor_exit_successfully_sends_checkin_when_enter_checkin_dropped(
sentry_init, capture_envelopes
):
sentry_init(
before_send=lambda event, hint: (
None if event.get("status") == "in_progress" else event
)
)
envelopes = capture_envelopes()

monitor = sentry_sdk.monitor(monitor_slug="abc123")
with monitor:
# Confirms that the check in was dropped in the `__enter__`
assert monitor.check_in_id is None

(envelope,) = envelopes
check_in = envelope.items[0].payload.json

# Confirms that the second check in in `__exit__` works correctly
assert check_in["monitor_slug"] == "abc123"
assert check_in["check_in_id"]
assert check_in["status"] == "ok"
assert check_in["duration"] > 0


def test_scope_data_in_checkin(sentry_init, capture_envelopes):
Expand Down
Loading