Skip to content

fix(crons): Serialize the config type correctly #85211

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 3 commits into from
Feb 14, 2025
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
12 changes: 9 additions & 3 deletions src/sentry/monitors/serializers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from collections import defaultdict
from collections.abc import MutableMapping, Sequence
from datetime import datetime
from typing import Any, Literal, TypedDict
from typing import Any, Literal, TypedDict, cast

from django.db.models import prefetch_related_objects

Expand All @@ -17,6 +17,7 @@
MonitorEnvironment,
MonitorIncident,
MonitorStatus,
ScheduleType,
)
from sentry.monitors.processing_errors.errors import (
CheckinProcessingError,
Expand Down Expand Up @@ -276,7 +277,7 @@ class MonitorCheckInSerializerResponse(MonitorCheckInSerializerResponseOptional)
duration: int
dateCreated: datetime
expectedTime: datetime
monitorConfig: Any
monitorConfig: MonitorConfigSerializerResponse


@register(MonitorCheckIn)
Expand Down Expand Up @@ -330,14 +331,19 @@ def get_attrs(self, item_list, user, **kwargs):
return attrs

def serialize(self, obj, attrs, user, **kwargs) -> MonitorCheckInSerializerResponse:
config = obj.monitor_config.copy() if obj.monitor_config else {}
if "schedule_type" in config:
# XXX: We don't use monitor.get_schedule_type_display() in case it differs from the
# config saved on the check-in
config["schedule_type"] = ScheduleType.get_name(config["schedule_type"])
Comment on lines +334 to +338
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feels like what we probably really should be doing is having some type of sub-serializer for the config object, but this is fine for now I think

result: MonitorCheckInSerializerResponse = {
"id": str(obj.guid),
"environment": attrs["environment_name"],
"status": obj.get_status_display(),
"duration": obj.duration,
"dateCreated": obj.date_added,
"expectedTime": obj.expected_time,
"monitorConfig": obj.monitor_config or {},
"monitorConfig": cast(MonitorConfigSerializerResponse, config),
}

if self._expand("groups"):
Expand Down
4 changes: 4 additions & 0 deletions src/sentry/testutils/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from sentry.models.team import Team
from sentry.monitors.models import (
Monitor,
MonitorCheckIn,
MonitorEnvironment,
MonitorIncident,
MonitorType,
Expand Down Expand Up @@ -465,6 +466,9 @@ def create_monitor_environment(self, **kwargs):
def create_monitor_incident(self, **kwargs):
return MonitorIncident.objects.create(**kwargs)

def create_monitor_checkin(self, **kwargs):
return MonitorCheckIn.objects.create(**kwargs)

def create_external_user(self, user=None, organization=None, integration=None, **kwargs):
if not user:
user = self.user
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from sentry.models.environment import Environment
from sentry.models.group import Group
from sentry.monitors.models import CheckInStatus, MonitorCheckIn, MonitorStatus
from sentry.monitors.models import CheckInStatus, MonitorCheckIn, MonitorStatus, ScheduleType
from sentry.testutils.cases import MonitorTestCase
from sentry.testutils.helpers.datetime import freeze_time
from sentry.testutils.skips import requires_snuba
Expand Down Expand Up @@ -225,3 +225,42 @@ def test_trace_ids(self):
assert resp.data[0]["groups"] == []
assert resp.data[1]["id"] == str(checkin1.guid)
assert resp.data[1]["groups"] == [{"id": group.id, "shortId": group.qualified_short_id}]

def test_serializes_monitor_config_correctly(self):
monitor = self.create_monitor(project=self.project)
config = {
"schedule": "0 0 * * *",
"schedule_type": ScheduleType.CRONTAB,
"timezone": "US/Arizona",
"max_runtime": None,
"checkin_margin": None,
}
monitor_environment = self._create_monitor_environment(monitor)
self.create_monitor_checkin(
monitor=monitor,
monitor_environment=monitor_environment,
project_id=self.project.id,
date_added=monitor.date_added - timedelta(minutes=2),
status=CheckInStatus.OK,
monitor_config=config,
)
# Mutating the monitor config to test that the check-in config is used
monitor.config = {
"schedule": "0 * * * *",
"schedule_type": ScheduleType.INTERVAL,
"timezone": "CA/Toronto",
"max_runtime": 1000,
"checkin_margin": 100,
}
monitor.save()
response = self.get_success_response(
self.project.organization.slug,
monitor.slug,
)
assert response.data[0]["monitorConfig"]["schedule_type"] == ScheduleType.get_name(
config["schedule_type"]
)
assert response.data[0]["monitorConfig"]["schedule"] == config["schedule"]
assert response.data[0]["monitorConfig"]["timezone"] == config["timezone"]
assert response.data[0]["monitorConfig"]["max_runtime"] == config["max_runtime"]
assert response.data[0]["monitorConfig"]["checkin_margin"] == config["checkin_margin"]
Loading