Skip to content

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

Open
rupakInfinitiBit wants to merge 1 commit into
monitor-incident-refactor-beforefrom
monitor-incident-refactor-after
Open

ref(crons): Reorganize incident creation / issue occurrence logic#7
rupakInfinitiBit wants to merge 1 commit into
monitor-incident-refactor-beforefrom
monitor-incident-refactor-after

Conversation

@rupakInfinitiBit

@rupakInfinitiBit rupakInfinitiBit commented Mar 12, 2026

Copy link
Copy Markdown
Owner

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

…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
@pr-review-agent-rupakinfinitibit

Copy link
Copy Markdown

🔍 Reviewing PR #7

ref(crons): Reorganize incident creation / issue occurrence logic (#8
monitor-incident-refactor-aftermonitor-incident-refactor-before  ·  4 file(s)  ·  @rupakInfinitiBit

…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

⏳ Analyzing changes — review incoming shortly.

@graphbit-prflow-dev

Copy link
Copy Markdown

@rupakInfinitiBit 👋 I've started reviewing this pull request. I'll post a detailed review once I'm done — this may take a moment.

@coderabbitai

coderabbitai Bot commented Mar 12, 2026

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 9d6fdfca-60fb-4e6b-8463-f1cc1d060984

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch monitor-incident-refactor-after

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@graphbit-prflow-dev graphbit-prflow-dev Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔍 Automated PR Review

PR Title: ref(crons): Reorganize incident creation / issue occurrence logic (#8
Overall Quality Score: 7/10
Recommendation: REQUEST_CHANGES


🧠 Summary

The code refactoring successfully reorganizes incident creation logic but contains several quality issues including typos, performance concerns with database queries, and missing documentation that should be addressed.


⚠️ Critical Issues

No critical issues detected in this review.


✅ Key Suggestions

Fix typo in docstring: Change "humam" to "human" in the docstring at line 131 of incident_occurrence.py

Optimize database queries: The loop creating occurrences performs multiple individual database queries which could cause performance issues. Consider batching these operations instead

Add input validation: The failed_checkin parameter in create_incident_occurrence should be validated to ensure it's a valid MonitorCheckIn instance

Improve function naming: Consider renaming try_incident_threshold to handle_incident_threshold or process_incident_threshold for better clarity about its actual behavior

Use assigned variables: The config variable is assigned but not used - either utilize it in the return dictionary or remove the assignment

Add comprehensive documentation: Both new modules and key functions are missing docstrings that would help developers understand their purpose and usage

Modernize string formatting: Replace old-style string formatting with f-strings for better readability and consistency


Copy link
Copy Markdown

Choose a reason for hiding this comment

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

✅ Approved

████████░░ 8.5/10  ·  4 files reviewed  ·  1 issues found

Key concerns:

  • Minor typo in docstring that should be corrected
📝 Walkthrough (4 files)
File Changes Summary
src/sentry/monitors/logic/incident_occurrence.py ✏️ +171/-0 New module extracting incident occurrence creation logic from mark_failed.py, with proper separation of concerns for issue platform integration.
src/sentry/monitors/logic/incidents.py ✏️ +104/-0 New module extracting incident threshold logic from mark_failed.py, handling monitor status transitions and incident creation.
src/sentry/monitors/logic/mark_failed.py ✏️ +4/-264 Significantly refactored to focus on core failure marking logic, delegating incident handling to the new incidents module.
src/sentry/monitors/types.py ✏️ +10/-0 Added SimpleCheckIn TypedDict to centralize the type definition previously inline in mark_failed.py.

🔍 Issues

🔵 src/sentry/monitors/logic/incident_occurrence.py

  • 🔵 Line 130Typo in docstring

    There's a typo in the docstring: 'humam readible' should be 'human readable'.

    💡 Suggestion: Builds a human readable string from a list of failed check-ins.

✨ Strengths

  • ✅ Excellent separation of concerns improving code organization
  • ✅ Clean refactoring with no functional changes
  • ✅ Proper type definitions and imports maintained throughout

🤖 PRFlow  ·  🔴 Critical  🟠 Important  🟡 Suggestion  🔵 Minor  ℹ️ Note

@rupakInfinitiBit rupakInfinitiBit changed the title ref(crons): Reorganize incident creation / issue occurrence logic (#8… ref(crons): Reorganize incident creation / issue occurrence logic Mar 13, 2026
@rupakInfinitiBit rupakInfinitiBit marked this pull request as draft March 24, 2026 09:20
@rupakInfinitiBit rupakInfinitiBit marked this pull request as ready for review March 24, 2026 09:20
@pr-review-agent-rupakinfinitibit

Copy link
Copy Markdown

🔍 Reviewing PR #7

ref(crons): Reorganize incident creation / issue occurrence logic
monitor-incident-refactor-aftermonitor-incident-refactor-before  ·  4 file(s)  ·  @rupakInfinitiBit

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

⏳ Analyzing changes — review incoming shortly.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔧 Changes Requested

██████░░░░ 6.5/10  ·  4 files reviewed  ·  6 issues found

Key concerns:

  • Truncated function call in produce_occurrence_to_kafka will cause syntax error
  • Incomplete incident creation logic in incidents.py
  • Missing error handling for empty failure counters
  • Lack of trace_id validation before calling .hex method
📝 Walkthrough (4 files)
File Changes Summary
src/sentry/monitors/logic/incident_occurrence.py ✏️ +171/-0 New module for handling incident occurrence creation with issue evidence, monitoring context, and Kafka event production.
src/sentry/monitors/logic/incidents.py ✏️ +104/-0 New module containing incident threshold evaluation logic extracted from mark_failed.py.
src/sentry/monitors/logic/mark_failed.py ✏️ +4/-264 Refactored to extract incident and occurrence logic into separate modules, keeping only the core failure marking functionality.
src/sentry/monitors/types.py ✏️ +10/-0 Added SimpleCheckIn TypedDict to centralize type definitions for monitor check-ins.

🔍 Issues

🟠 src/sentry/monitors/logic/incident_occurrence.py

  • 🟡 Line 87Missing trace_id validation

    The code checks if failed_checkin.trace_id exists but doesn't validate that it's a proper UUID format before calling .hex. This could raise an AttributeError if trace_id is not a UUID object.

if failed_checkin.trace_id and hasattr(failed_checkin.trace_id, 'hex'):
        trace_id = failed_checkin.trace_id.hex
    else:
        trace_id = None
  • 🟠 Line 118Truncated function call in produce_occurrence_to_kafka

    The function call to produce_occurrence_to_kafka appears to be truncated at 'even...' which would cause a syntax error. The 'event_data' parameter is incomplete.

produce_occurrence_to_kafka(
        payload_type=PayloadType.OCCURRENCE,
        occurrence=occurrence,
        event_data=event_data,
    )
  • 🟡 Line 149Potential KeyError in failure_reason_counter access

    The function accesses failure_reason_counter.most_common(1)[0] without checking if the counter is empty, which could raise an IndexError if failed_checkins is empty.

most_common = failure_reason_counter.most_common(1)
    if not most_common:
        return _("Unknown")
    return most_common[0][0]

🟡 src/sentry/monitors/logic/incidents.py

  • 🟡 Line 65Incomplete incident creation logic

    The incident creation logic appears to be truncated with 'incident, _' assignment incomplete. This will cause a syntax error and the function won't work properly.

incident, created = MonitorIncident.objects.get_or_create(
            monitor_environment=monitor_env,
            resolving_checkin=None,
            defaults={
                "starting_checkin_id": starting_checkin["id"],
                "starting_timestamp": starting_checkin["date_added"],
                "grouphash": monitor_env.monitor.get_incident_grouphash(),
            },
        )

🔵 src/sentry/monitors/logic/mark_failed.py

  • 🔵 Line 79Good separation of concerns

    The refactoring successfully separates the incident threshold logic into its own module, making the mark_failed function more focused and maintainable.

ℹ️ src/sentry/monitors/types.py

  • ℹ️ Line 10Good type definition placement

    Moving the SimpleCheckIn TypedDict to the types module improves code organization and makes the type available for reuse across modules.

✨ Strengths

  • ✅ Good separation of concerns by extracting incident logic into dedicated modules
  • ✅ Improved code organization with centralized type definitions
  • ✅ Maintains existing functionality while improving maintainability

🤖 PRFlow  ·  🔴 Critical  🟠 Important  🟡 Suggestion  🔵 Minor  ℹ️ Note

trace_id = failed_checkin.trace_id.hex
else:
trace_id = None

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔧 Improvement | 🟡 Suggestion

Missing trace_id validation

The code checks if failed_checkin.trace_id exists but doesn't validate that it's a proper UUID format before calling .hex. This could raise an AttributeError if trace_id is not a UUID object.

Suggested change
if failed_checkin.trace_id and hasattr(failed_checkin.trace_id, 'hex'):
trace_id = failed_checkin.trace_id.hex
else:
trace_id = None
🤖 Prompt for AI Agents
In `@src/sentry/monitors/logic/incident_occurrence.py` around line 87, The code checks if failed_checkin.trace_id exists but doesn't validate that it's a proper UUID format before calling .hex. This could raise an AttributeError if trace_id is not a UUID object.; apply the proposed fix to resolve this error handling.


HUMAN_FAILURE_STATUS_MAP: Mapping[int, _StrPromise] = {
CheckInStatus.ERROR: _("error"),
CheckInStatus.MISSED: _("missed"),

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Important

Important: Truncated function call in produce_occurrence_to_kafka

The function call to produce_occurrence_to_kafka appears to be truncated at 'even...' which would cause a syntax error. The 'event_data' parameter is incomplete.

Suggested change
CheckInStatus.MISSED: _("missed"),
produce_occurrence_to_kafka(
payload_type=PayloadType.OCCURRENCE,
occurrence=occurrence,
event_data=event_data,
)
🤖 Prompt for AI Agents
In `@src/sentry/monitors/logic/incident_occurrence.py` around line 118, The function call to produce_occurrence_to_kafka appears to be truncated at 'even...' which would cause a syntax error. The 'event_data' parameter is incomplete.; apply the proposed fix to resolve this bug.

return SINGULAR_HUMAN_FAILURE_MAP[list(status_counts.keys())[0]]

human_status = get_text_list(
[

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔧 Improvement | 🟡 Suggestion

Potential KeyError in failure_reason_counter access

The function accesses failure_reason_counter.most_common(1)[0] without checking if the counter is empty, which could raise an IndexError if failed_checkins is empty.

Suggested change
[
most_common = failure_reason_counter.most_common(1)
if not most_common:
return _("Unknown")
return most_common[0][0]
🤖 Prompt for AI Agents
In `@src/sentry/monitors/logic/incident_occurrence.py` around line 149, The function accesses failure_reason_counter.most_common(1)[0] without checking if the counter is empty, which could raise an IndexError if failed_checkins is empty.; apply the proposed fix to resolve this logic.

incident, _ = MonitorIncident.objects.get_or_create(
monitor_environment=monitor_env,
resolving_checkin=None,
defaults={

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔧 Improvement | 🟡 Suggestion

Incomplete incident creation logic

The incident creation logic appears to be truncated with 'incident, _' assignment incomplete. This will cause a syntax error and the function won't work properly.

Suggested change
defaults={
incident, created = MonitorIncident.objects.get_or_create(
monitor_environment=monitor_env,
resolving_checkin=None,
defaults={
"starting_checkin_id": starting_checkin["id"],
"starting_timestamp": starting_checkin["date_added"],
"grouphash": monitor_env.monitor.get_incident_grouphash(),
},
)
🤖 Prompt for AI Agents
In `@src/sentry/monitors/logic/incidents.py` around line 65, The incident creation logic appears to be truncated with 'incident, _' assignment incomplete. This will cause a syntax error and the function won't work properly.; apply the proposed fix to resolve this error handling.

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