Skip to content

Commit a7512c6

Browse files
feat(crons): Always update date_updated when updating a check-in (#91723)
Prior to this change, the date_updated would ONLY be chnaged when a in_progress heart-beat was sent. This is rather confusing since this column is named in such a way that you would expect the date to reflect the last time the check-in was modified. Any time we update a check-in we will now modify the date_updated. This can be helpful to understand the last time a check-in was sent for this job. Fixes [NEW-321: Always set `date_updated`, not just when sending a `in_progress` heart beat](https://linear.app/getsentry/issue/NEW-321/always-set-date-updated-not-just-when-sending-a-in-progress-heart-beat)
1 parent 9740cd8 commit a7512c6

File tree

4 files changed

+37
-5
lines changed

4 files changed

+37
-5
lines changed

src/sentry/monitors/clock_tasks/check_missed.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ def mark_environment_missing(monitor_environment_id: int, ts: datetime):
118118
monitor_environment=monitor_environment,
119119
status=CheckInStatus.MISSED,
120120
date_added=expected_time,
121+
date_updated=expected_time,
121122
date_clock=ts,
122123
expected_time=expected_time,
123124
monitor_config=monitor.get_validated_config(),

src/sentry/monitors/consumers/monitor_consumer.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,7 @@ def update_existing_check_in(
381381
updated_checkin: _CheckinUpdateKwargs = {
382382
"status": updated_status,
383383
"duration": updated_duration,
384+
"date_updated": start_time,
384385
}
385386

386387
# XXX(epurkhiser): We currently allow a existing timed-out check-in to
@@ -394,7 +395,7 @@ def update_existing_check_in(
394395
if updated_duration_only:
395396
del updated_checkin["status"]
396397

397-
# IN_PROGRESS heartbeats bump the timeout
398+
# IN_PROGRESS heartbeats bump the timeout, terminal staus's set None
398399
updated_checkin["timeout_at"] = get_new_timeout_at(
399400
existing_check_in,
400401
updated_status,
@@ -405,12 +406,10 @@ def update_existing_check_in(
405406
tags={**metric_kwargs, "status": "updated_existing_checkin"},
406407
)
407408

408-
# IN_PROGRESS heartbeats bump the date_updated
409+
# XXX(epurkhiser): Tracking metrics on updating the date_updated since
410+
# we may want to remove this 'heart-beat' feature at some point.
409411
if updated_status == CheckInStatus.IN_PROGRESS:
410-
# XXX(epurkhiser): Tracking metrics on updating the date_updated since
411-
# we may weant to remove this 'heart-beat' feature.
412412
metrics.incr("monitors.in_progress_heart_beat", tags=metric_kwargs)
413-
updated_checkin["date_updated"] = start_time
414413

415414
existing_check_in.update(**updated_checkin)
416415

tests/sentry/monitors/clock_tasks/test_check_missed.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ def test_missing_checkin(self, mock_produce_task):
9595
next_checkin = next_checkin.replace(second=0, microsecond=0)
9696

9797
assert missed_checkin.date_added == next_checkin
98+
assert missed_checkin.date_updated == next_checkin
9899
assert missed_checkin.expected_time == next_checkin
99100
assert missed_checkin.monitor_config == monitor.config
100101

@@ -255,6 +256,7 @@ def test_missing_checkin_with_margin(self, mock_produce_task):
255256
checkin_date = checkin_date.replace(second=0, microsecond=0)
256257

257258
assert missed_checkin.date_added == checkin_date
259+
assert missed_checkin.date_updated == checkin_date
258260
assert missed_checkin.expected_time == checkin_date
259261
assert missed_checkin.monitor_config == monitor.config
260262

@@ -347,6 +349,7 @@ def test_missing_checkin_with_margin_schedule_overlap(self, mock_produce_task):
347349
status=CheckInStatus.MISSED,
348350
)
349351
assert missed_checkin.date_added == ts
352+
assert missed_checkin.date_updated == ts
350353
assert missed_checkin.expected_time == ts
351354

352355
monitor_env = MonitorEnvironment.objects.get(
@@ -694,6 +697,7 @@ def test_missed_checkin_backlog_handled(self, mock_produce_task):
694697
monitor_environment=monitor_environment.id, status=CheckInStatus.MISSED
695698
)
696699
assert missed_checkin.date_added == ts
700+
assert missed_checkin.date_updated == ts
697701
assert missed_checkin.expected_time == ts
698702

699703
# Execute the second task. This should detect that we've already moved

tests/sentry/monitors/consumers/test_monitor_consumer.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,34 @@ def test_check_in_no_in_progress(self):
320320
assert checkin.status == CheckInStatus.OK
321321
assert checkin.date_added == now.replace(tzinfo=UTC)
322322

323+
def test_check_date_updated(self):
324+
now = datetime.now()
325+
guid = uuid.uuid4().hex
326+
327+
monitor = self._create_monitor(slug="my-monitor")
328+
self.send_checkin(monitor.slug, guid=guid, ts=now, status="in_progress")
329+
330+
checkin = MonitorCheckIn.objects.get(guid=self.guid)
331+
assert checkin.status == CheckInStatus.IN_PROGRESS
332+
assert checkin.date_added == now.replace(tzinfo=UTC)
333+
assert checkin.date_updated == now.replace(tzinfo=UTC)
334+
335+
# Another in_progress moves the date_updated forward
336+
self.send_checkin(
337+
monitor.slug, guid=guid, ts=now + timedelta(seconds=10), status="in_progress"
338+
)
339+
checkin.refresh_from_db()
340+
assert checkin.status == CheckInStatus.IN_PROGRESS
341+
assert checkin.date_added == now.replace(tzinfo=UTC)
342+
assert checkin.date_updated == now.replace(tzinfo=UTC) + timedelta(seconds=10)
343+
344+
# Closing check in moves the date_updated forward
345+
self.send_checkin(monitor.slug, guid=guid, ts=now + timedelta(seconds=20), status="ok")
346+
checkin.refresh_from_db()
347+
assert checkin.status == CheckInStatus.OK
348+
assert checkin.date_added == now.replace(tzinfo=UTC)
349+
assert checkin.date_updated == now.replace(tzinfo=UTC) + timedelta(seconds=20)
350+
323351
def test_check_in_date_clock(self):
324352
monitor = self._create_monitor(slug="my-monitor")
325353
now = datetime.now()

0 commit comments

Comments
 (0)