Skip to content

ref(crons): Reorganize incident creation / issue occurrence logic#14

Open
ShashankFC wants to merge 1 commit intomonitor-incident-refactor-beforefrom
monitor-incident-refactor-after
Open

ref(crons): Reorganize incident creation / issue occurrence logic#14
ShashankFC wants to merge 1 commit intomonitor-incident-refactor-beforefrom
monitor-incident-refactor-after

Conversation

@ShashankFC
Copy link
Collaborator

…0528)

Since we'll be doing more with issue occurrences split out the concept
of incidents into it's own logic module, as well as incident_occurrence
into it's own module

Part of GH-80527
@greptile-apps
Copy link

greptile-apps bot commented Jan 5, 2026

Greptile Summary

This PR refactors the monitor incident and occurrence creation logic by extracting code from mark_failed.py into two new specialized modules for better separation of concerns.

Key Changes:

  • Extracted incident threshold logic into incidents.py (mark_failed_thresholdtry_incident_threshold)
  • Extracted occurrence creation logic into incident_occurrence.py (create_issue_platform_occurrencecreate_incident_occurrence)
  • Moved SimpleCheckIn TypedDict to centralized types.py module
  • Simplified mark_failed.py to delegate to the new modules

Issues Found:

  • Minor typo in docstring: "humam readible" should be "human readable"

Confidence Score: 5/5

  • This PR is safe to merge with minimal risk
  • Clean refactoring that extracts code into logical modules without changing behavior. The logic is moved verbatim from mark_failed.py into two new files with only function names changed (mark_failed_threshold → try_incident_threshold, create_issue_platform_occurrence → create_incident_occurrence). The SimpleCheckIn type is appropriately moved to the centralized types module. Only issue is a minor typo in a docstring.
  • No files require special attention

Important Files Changed

Filename Overview
src/sentry/monitors/logic/incident_occurrence.py New file extracting occurrence creation logic from mark_failed.py; includes typo in docstring
src/sentry/monitors/logic/incidents.py New file extracting incident threshold logic from mark_failed.py; clean refactor with no issues
src/sentry/monitors/logic/mark_failed.py Simplified by removing extracted logic and delegating to new incidents module
src/sentry/monitors/types.py Moved SimpleCheckIn TypedDict from mark_failed.py to centralized types module

Sequence Diagram

sequenceDiagram
    participant Consumer as Monitor Consumer
    participant MF as mark_failed.py
    participant TIT as try_incident_threshold<br/>(incidents.py)
    participant CIO as create_incident_occurrence<br/>(incident_occurrence.py)
    participant DB as Database
    participant Kafka as Kafka

    Consumer->>MF: mark_failed(failed_checkin, ts, received)
    MF->>MF: Get failure_issue_threshold from config
    MF->>MF: Calculate next check-in times
    MF->>DB: Update MonitorEnvironment timestamps
    alt Update successful
        MF->>TIT: try_incident_threshold(failed_checkin, threshold, received)
        TIT->>TIT: Check monitor_env.status
        alt Status is OK/ACTIVE
            TIT->>DB: Query previous check-ins
            TIT->>TIT: Check if threshold reached
            alt Threshold reached
                TIT->>DB: Set status to ERROR
                TIT->>DB: Create/get MonitorIncident
            end
        else Status is ERROR
            TIT->>DB: Get active_incident
        end
        alt Incident exists and not muted
            TIT->>DB: Query MonitorCheckIn objects
            loop For each check-in
                TIT->>CIO: create_incident_occurrence(checkins, checkin, incident, received)
                CIO->>CIO: Build IssueOccurrence with evidence
                CIO->>CIO: Build event_data with context
                CIO->>Kafka: produce_occurrence_to_kafka()
            end
        end
        TIT->>TIT: Send monitor_environment_failed signal
        TIT-->>MF: Return True
    else Update failed
        MF-->>Consumer: Return False
    end
    MF-->>Consumer: Return result
Loading

Copy link

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

4 files reviewed, 1 comment

Edit Code Review Agent Settings | Greptile


def get_failure_reason(failed_checkins: Sequence[SimpleCheckIn]):
"""
Builds a humam readible string from a list of failed check-ins.
Copy link

Choose a reason for hiding this comment

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

syntax: Typo: "humam readible" should be "human readable"

Suggested change
Builds a humam readible string from a list of failed check-ins.
Builds a human readable string from a list of failed check-ins.
Prompt To Fix With AI
This is a comment left during a code review.
Path: src/sentry/monitors/logic/incident_occurrence.py
Line: 132:132

Comment:
**syntax:** Typo: "humam readible" should be "human readable"

```suggestion
    Builds a human readable string from a list of failed check-ins.
```

How can I resolve this? If you propose a fix, please make it concise.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants