|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import logging |
| 4 | +import uuid |
| 5 | +from collections import Counter |
| 6 | +from collections.abc import Mapping, Sequence |
| 7 | +from datetime import datetime, timezone |
| 8 | +from typing import TYPE_CHECKING |
| 9 | + |
| 10 | +from django.utils.text import get_text_list |
| 11 | +from django.utils.translation import gettext_lazy as _ |
| 12 | + |
| 13 | +from sentry.issues.grouptype import MonitorIncidentType |
| 14 | +from sentry.monitors.models import ( |
| 15 | + CheckInStatus, |
| 16 | + MonitorCheckIn, |
| 17 | + MonitorEnvironment, |
| 18 | + MonitorIncident, |
| 19 | +) |
| 20 | +from sentry.monitors.types import SimpleCheckIn |
| 21 | + |
| 22 | +if TYPE_CHECKING: |
| 23 | + from django.utils.functional import _StrPromise |
| 24 | + |
| 25 | +logger = logging.getLogger(__name__) |
| 26 | + |
| 27 | + |
| 28 | +def create_incident_occurrence( |
| 29 | + failed_checkins: Sequence[SimpleCheckIn], |
| 30 | + failed_checkin: MonitorCheckIn, |
| 31 | + incident: MonitorIncident, |
| 32 | + received: datetime | None, |
| 33 | +) -> None: |
| 34 | + from sentry.issues.issue_occurrence import IssueEvidence, IssueOccurrence |
| 35 | + from sentry.issues.producer import PayloadType, produce_occurrence_to_kafka |
| 36 | + |
| 37 | + monitor_env = failed_checkin.monitor_environment |
| 38 | + |
| 39 | + if monitor_env is None: |
| 40 | + return |
| 41 | + |
| 42 | + current_timestamp = datetime.now(timezone.utc) |
| 43 | + |
| 44 | + # Get last successful check-in to show in evidence display |
| 45 | + last_successful_checkin_timestamp = "Never" |
| 46 | + last_successful_checkin = monitor_env.get_last_successful_checkin() |
| 47 | + if last_successful_checkin: |
| 48 | + last_successful_checkin_timestamp = last_successful_checkin.date_added.isoformat() |
| 49 | + |
| 50 | + occurrence = IssueOccurrence( |
| 51 | + id=uuid.uuid4().hex, |
| 52 | + resource_id=None, |
| 53 | + project_id=monitor_env.monitor.project_id, |
| 54 | + event_id=uuid.uuid4().hex, |
| 55 | + fingerprint=[incident.grouphash], |
| 56 | + type=MonitorIncidentType, |
| 57 | + issue_title=f"Monitor failure: {monitor_env.monitor.name}", |
| 58 | + subtitle="Your monitor has reached its failure threshold.", |
| 59 | + evidence_display=[ |
| 60 | + IssueEvidence( |
| 61 | + name="Failure reason", |
| 62 | + value=str(get_failure_reason(failed_checkins)), |
| 63 | + important=True, |
| 64 | + ), |
| 65 | + IssueEvidence( |
| 66 | + name="Environment", |
| 67 | + value=monitor_env.get_environment().name, |
| 68 | + important=False, |
| 69 | + ), |
| 70 | + IssueEvidence( |
| 71 | + name="Last successful check-in", |
| 72 | + value=last_successful_checkin_timestamp, |
| 73 | + important=False, |
| 74 | + ), |
| 75 | + ], |
| 76 | + evidence_data={}, |
| 77 | + culprit="", |
| 78 | + detection_time=current_timestamp, |
| 79 | + level="error", |
| 80 | + assignee=monitor_env.monitor.owner_actor, |
| 81 | + ) |
| 82 | + |
| 83 | + if failed_checkin.trace_id: |
| 84 | + trace_id = failed_checkin.trace_id.hex |
| 85 | + else: |
| 86 | + trace_id = None |
| 87 | + |
| 88 | + event_data = { |
| 89 | + "contexts": {"monitor": get_monitor_environment_context(monitor_env)}, |
| 90 | + "environment": monitor_env.get_environment().name, |
| 91 | + "event_id": occurrence.event_id, |
| 92 | + "fingerprint": [incident.grouphash], |
| 93 | + "platform": "other", |
| 94 | + "project_id": monitor_env.monitor.project_id, |
| 95 | + # We set this to the time that the checkin that triggered the occurrence was written to relay if available |
| 96 | + "received": (received if received else current_timestamp).isoformat(), |
| 97 | + "sdk": None, |
| 98 | + "tags": { |
| 99 | + "monitor.id": str(monitor_env.monitor.guid), |
| 100 | + "monitor.slug": str(monitor_env.monitor.slug), |
| 101 | + "monitor.incident": str(incident.id), |
| 102 | + }, |
| 103 | + "timestamp": current_timestamp.isoformat(), |
| 104 | + } |
| 105 | + |
| 106 | + if trace_id: |
| 107 | + event_data["contexts"]["trace"] = {"trace_id": trace_id, "span_id": None} |
| 108 | + |
| 109 | + produce_occurrence_to_kafka( |
| 110 | + payload_type=PayloadType.OCCURRENCE, |
| 111 | + occurrence=occurrence, |
| 112 | + event_data=event_data, |
| 113 | + ) |
| 114 | + |
| 115 | + |
| 116 | +HUMAN_FAILURE_STATUS_MAP: Mapping[int, _StrPromise] = { |
| 117 | + CheckInStatus.ERROR: _("error"), |
| 118 | + CheckInStatus.MISSED: _("missed"), |
| 119 | + CheckInStatus.TIMEOUT: _("timeout"), |
| 120 | +} |
| 121 | + |
| 122 | +# Exists due to the vowel differences (A vs An) in the statuses |
| 123 | +SINGULAR_HUMAN_FAILURE_MAP: Mapping[int, _StrPromise] = { |
| 124 | + CheckInStatus.ERROR: _("An error check-in was detected"), |
| 125 | + CheckInStatus.MISSED: _("A missed check-in was detected"), |
| 126 | + CheckInStatus.TIMEOUT: _("A timeout check-in was detected"), |
| 127 | +} |
| 128 | + |
| 129 | + |
| 130 | +def get_failure_reason(failed_checkins: Sequence[SimpleCheckIn]): |
| 131 | + """ |
| 132 | + Builds a humam readible string from a list of failed check-ins. |
| 133 | +
|
| 134 | + "3 missed check-ins detected" |
| 135 | + "2 missed check-ins, 1 timeout check-in and 1 error check-in were detected" |
| 136 | + "A failed check-in was detected" |
| 137 | + """ |
| 138 | + |
| 139 | + status_counts = Counter( |
| 140 | + checkin["status"] |
| 141 | + for checkin in failed_checkins |
| 142 | + if checkin["status"] in HUMAN_FAILURE_STATUS_MAP.keys() |
| 143 | + ) |
| 144 | + |
| 145 | + if sum(status_counts.values()) == 1: |
| 146 | + return SINGULAR_HUMAN_FAILURE_MAP[list(status_counts.keys())[0]] |
| 147 | + |
| 148 | + human_status = get_text_list( |
| 149 | + [ |
| 150 | + "%(count)d %(status)s" % {"count": count, "status": HUMAN_FAILURE_STATUS_MAP[status]} |
| 151 | + for status, count in status_counts.items() |
| 152 | + ], |
| 153 | + last_word=_("and"), |
| 154 | + ) |
| 155 | + |
| 156 | + return _("%(problem_checkins)s check-ins detected") % {"problem_checkins": human_status} |
| 157 | + |
| 158 | + |
| 159 | +def get_monitor_environment_context(monitor_environment: MonitorEnvironment): |
| 160 | + config = monitor_environment.monitor.config.copy() |
| 161 | + if "schedule_type" in config: |
| 162 | + config["schedule_type"] = monitor_environment.monitor.get_schedule_type_display() |
| 163 | + |
| 164 | + return { |
| 165 | + "id": str(monitor_environment.monitor.guid), |
| 166 | + "slug": str(monitor_environment.monitor.slug), |
| 167 | + "name": monitor_environment.monitor.name, |
| 168 | + "config": monitor_environment.monitor.config, |
| 169 | + "status": monitor_environment.get_status_display(), |
| 170 | + "type": monitor_environment.monitor.get_type_display(), |
| 171 | + } |
0 commit comments