Skip to content
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

feat(crons): Allow to upsert monitors #2929

Merged
merged 3 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
12 changes: 8 additions & 4 deletions sentry_sdk/crons/decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from sentry_sdk.utils import now

if TYPE_CHECKING:
from typing import Optional, Type
from typing import Any, Dict, Optional, Type

Check warning on line 8 in sentry_sdk/crons/decorator.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/crons/decorator.py#L8

Added line #L8 was not covered by tests
sentrivana marked this conversation as resolved.
Show resolved Hide resolved
from types import TracebackType

if PY2:
Expand Down Expand Up @@ -47,15 +47,18 @@
```
"""

def __init__(self, monitor_slug=None):
# type: (Optional[str]) -> None
def __init__(self, monitor_slug=None, monitor_config=None):
# type: (Optional[str], Optional[Dict[str, Any]]) -> None
sentrivana marked this conversation as resolved.
Show resolved Hide resolved
self.monitor_slug = monitor_slug
self.monitor_config = monitor_config

def __enter__(self):
# type: () -> None
self.start_timestamp = now()
self.check_in_id = capture_checkin(
monitor_slug=self.monitor_slug, status=MonitorStatus.IN_PROGRESS
monitor_slug=self.monitor_slug,
status=MonitorStatus.IN_PROGRESS,
monitor_config=self.monitor_config,
)

def __exit__(self, exc_type, exc_value, traceback):
Expand All @@ -72,4 +75,5 @@
check_in_id=self.check_in_id,
status=status,
duration=duration_s,
monitor_config=self.monitor_config,
)
69 changes: 65 additions & 4 deletions tests/crons/test_crons.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,22 @@ def _break_world_contextmanager(name):
return "Hello, {}".format(name)


@sentry_sdk.monitor(monitor_slug="ghi789", monitor_config=None)
def _no_monitor_config():
return


@sentry_sdk.monitor(
monitor_slug="ghi789",
monitor_config={
"schedule": {"type": "crontab", "value": "0 0 * * *"},
"failure_issue_threshold": 5,
},
)
def _with_monitor_config():
return


def test_decorator(sentry_init):
sentry_init()

Expand All @@ -45,7 +61,9 @@ def test_decorator(sentry_init):
# Check for initial checkin
fake_capture_checkin.assert_has_calls(
[
mock.call(monitor_slug="abc123", status="in_progress"),
mock.call(
monitor_slug="abc123", status="in_progress", monitor_config=None
),
]
)

Expand All @@ -70,7 +88,9 @@ def test_decorator_error(sentry_init):
# Check for initial checkin
fake_capture_checkin.assert_has_calls(
[
mock.call(monitor_slug="def456", status="in_progress"),
mock.call(
monitor_slug="def456", status="in_progress", monitor_config=None
),
]
)

Expand All @@ -93,7 +113,9 @@ def test_contextmanager(sentry_init):
# Check for initial checkin
fake_capture_checkin.assert_has_calls(
[
mock.call(monitor_slug="abc123", status="in_progress"),
mock.call(
monitor_slug="abc123", status="in_progress", monitor_config=None
),
]
)

Expand All @@ -118,7 +140,9 @@ def test_contextmanager_error(sentry_init):
# Check for initial checkin
fake_capture_checkin.assert_has_calls(
[
mock.call(monitor_slug="def456", status="in_progress"),
mock.call(
monitor_slug="def456", status="in_progress", monitor_config=None
),
]
)

Expand Down Expand Up @@ -194,6 +218,8 @@ def test_monitor_config(sentry_init, capture_envelopes):

monitor_config = {
"schedule": {"type": "crontab", "value": "0 0 * * *"},
"failure_issue_threshold": 5,
"recovery_threshold": 5,
}

capture_checkin(monitor_slug="abc123", monitor_config=monitor_config)
Expand All @@ -211,6 +237,41 @@ def test_monitor_config(sentry_init, capture_envelopes):
assert "monitor_config" not in check_in


def test_decorator_monitor_config(sentry_init, capture_envelopes):
sentry_init()
envelopes = capture_envelopes()

_with_monitor_config()

assert len(envelopes) == 2

for check_in_envelope in envelopes:
assert len(check_in_envelope.items) == 1
check_in = check_in_envelope.items[0].payload.json

assert check_in["monitor_slug"] == "ghi789"
assert check_in["monitor_config"] == {
"schedule": {"type": "crontab", "value": "0 0 * * *"},
"failure_issue_threshold": 5,
}


def test_decorator_no_monitor_config(sentry_init, capture_envelopes):
sentry_init()
envelopes = capture_envelopes()

_no_monitor_config()

assert len(envelopes) == 2

for check_in_envelope in envelopes:
assert len(check_in_envelope.items) == 1
check_in = check_in_envelope.items[0].payload.json

assert check_in["monitor_slug"] == "ghi789"
assert "monitor_config" not in check_in


def test_capture_checkin_sdk_not_initialized():
# Tests that the capture_checkin does not raise an error when Sentry SDK is not initialized.
# sentry_init() is intentionally omitted.
Expand Down
16 changes: 12 additions & 4 deletions tests/crons/test_crons_async_py3.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ async def test_decorator(sentry_init):
# Check for initial checkin
fake_capture_checkin.assert_has_calls(
[
mock.call(monitor_slug="abc123", status="in_progress"),
mock.call(
monitor_slug="abc123", status="in_progress", monitor_config=None
),
]
)

Expand All @@ -75,7 +77,9 @@ async def test_decorator_error(sentry_init):
# Check for initial checkin
fake_capture_checkin.assert_has_calls(
[
mock.call(monitor_slug="def456", status="in_progress"),
mock.call(
monitor_slug="def456", status="in_progress", monitor_config=None
),
]
)

Expand All @@ -99,7 +103,9 @@ async def test_contextmanager(sentry_init):
# Check for initial checkin
fake_capture_checkin.assert_has_calls(
[
mock.call(monitor_slug="abc123", status="in_progress"),
mock.call(
monitor_slug="abc123", status="in_progress", monitor_config=None
),
]
)

Expand All @@ -125,7 +131,9 @@ async def test_contextmanager_error(sentry_init):
# Check for initial checkin
fake_capture_checkin.assert_has_calls(
[
mock.call(monitor_slug="def456", status="in_progress"),
mock.call(
monitor_slug="def456", status="in_progress", monitor_config=None
),
]
)

Expand Down
Loading