|
@@ -1,6 +1,6 @@
|
|
|
-import datetime
|
|
|
import logging
|
|
|
import uuid
|
|
|
+from datetime import datetime, timedelta
|
|
|
from typing import Dict, Mapping, Optional
|
|
|
|
|
|
import msgpack
|
|
@@ -27,8 +27,13 @@ from sentry.monitors.models import (
|
|
|
MonitorLimitsExceeded,
|
|
|
MonitorType,
|
|
|
)
|
|
|
-from sentry.monitors.tasks import TIMEOUT
|
|
|
-from sentry.monitors.utils import signal_first_checkin, signal_first_monitor_created, valid_duration
|
|
|
+from sentry.monitors.utils import (
|
|
|
+ get_new_timeout_at,
|
|
|
+ get_timeout_at,
|
|
|
+ signal_first_checkin,
|
|
|
+ signal_first_monitor_created,
|
|
|
+ valid_duration,
|
|
|
+)
|
|
|
from sentry.monitors.validators import ConfigValidator, MonitorCheckInValidator
|
|
|
from sentry.utils import json, metrics
|
|
|
from sentry.utils.dates import to_datetime
|
|
@@ -151,7 +156,10 @@ def _process_message(wrapper: Dict) -> None:
|
|
|
return
|
|
|
|
|
|
def update_existing_check_in(
|
|
|
- existing_check_in: MonitorCheckIn, updated_status: CheckInStatus, updated_duration: float
|
|
|
+ existing_check_in: MonitorCheckIn,
|
|
|
+ updated_status: CheckInStatus,
|
|
|
+ updated_duration: float,
|
|
|
+ new_date_updated: datetime,
|
|
|
):
|
|
|
if (
|
|
|
existing_check_in.project_id != project_id
|
|
@@ -195,7 +203,19 @@ def _process_message(wrapper: Dict) -> None:
|
|
|
logger.debug("check-in implicit duration is invalid: %s", project.organization_id)
|
|
|
return
|
|
|
|
|
|
- existing_check_in.update(status=updated_status, duration=updated_duration)
|
|
|
+ # update date_added for heartbeat
|
|
|
+ date_updated = existing_check_in.date_updated
|
|
|
+ if updated_status == CheckInStatus.IN_PROGRESS:
|
|
|
+ date_updated = new_date_updated
|
|
|
+
|
|
|
+ updated_timeout_at = get_new_timeout_at(existing_check_in, updated_status, new_date_updated)
|
|
|
+
|
|
|
+ existing_check_in.update(
|
|
|
+ status=updated_status,
|
|
|
+ duration=updated_duration,
|
|
|
+ date_updated=date_updated,
|
|
|
+ timeout_at=updated_timeout_at,
|
|
|
+ )
|
|
|
|
|
|
return
|
|
|
|
|
@@ -286,7 +306,7 @@ def _process_message(wrapper: Dict) -> None:
|
|
|
guid=check_in_id,
|
|
|
)
|
|
|
|
|
|
- update_existing_check_in(check_in, status, validated_params["duration"])
|
|
|
+ update_existing_check_in(check_in, status, validated_params["duration"], start_time)
|
|
|
|
|
|
except MonitorCheckIn.DoesNotExist:
|
|
|
# Infer the original start time of the check-in from the duration.
|
|
@@ -294,7 +314,7 @@ def _process_message(wrapper: Dict) -> None:
|
|
|
date_added = start_time
|
|
|
duration = validated_params["duration"]
|
|
|
if duration is not None:
|
|
|
- date_added -= datetime.timedelta(milliseconds=duration)
|
|
|
+ date_added -= timedelta(milliseconds=duration)
|
|
|
|
|
|
expected_time = None
|
|
|
if monitor_environment.last_checkin:
|
|
@@ -303,11 +323,7 @@ def _process_message(wrapper: Dict) -> None:
|
|
|
)
|
|
|
|
|
|
monitor_config = monitor.get_validated_config()
|
|
|
- timeout_at = None
|
|
|
- if status == CheckInStatus.IN_PROGRESS:
|
|
|
- timeout_at = date_added.replace(second=0, microsecond=0) + datetime.timedelta(
|
|
|
- minutes=(monitor_config or {}).get("max_runtime") or TIMEOUT
|
|
|
- )
|
|
|
+ timeout_at = get_timeout_at(monitor_config, status, date_added)
|
|
|
|
|
|
trace_id = validated_params.get("contexts", {}).get("trace", {}).get("trace_id")
|
|
|
|
|
@@ -337,7 +353,7 @@ def _process_message(wrapper: Dict) -> None:
|
|
|
guid=guid,
|
|
|
)
|
|
|
if not created:
|
|
|
- update_existing_check_in(check_in, status, duration)
|
|
|
+ update_existing_check_in(check_in, status, duration, start_time)
|
|
|
else:
|
|
|
signal_first_checkin(project, monitor)
|
|
|
|