Skip to content

Commit

Permalink
success logs for issue alert dual write
Browse files Browse the repository at this point in the history
  • Loading branch information
cathteng committed Mar 3, 2025
1 parent 8c60284 commit 5975097
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 8 deletions.
7 changes: 6 additions & 1 deletion src/sentry/api/endpoints/project_rule_details.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,7 @@ def delete(self, request: Request, project, rule) -> Response:
- Filters: help control noise by triggering an alert only if the issue matches the specified criteria.
- Actions: specify what should happen when the trigger conditions are met and the filters match.
"""
rule_id = rule.id
rule.update(status=ObjectStatus.PENDING_DELETION)
RuleActivity.objects.create(
rule=rule, user_id=request.user.id, type=RuleActivityType.DELETED.value
Expand All @@ -375,7 +376,11 @@ def delete(self, request: Request, project, rule) -> Response:
if features.has(
"organizations:workflow-engine-issue-alert-dual-write", project.organization
):
delete_migrated_issue_alert(rule)
workflow_id = delete_migrated_issue_alert(rule)
logger.info(
"workflow_engine.issue_alert.deleted",
extra={"rule_id": rule_id, "workflow_id": workflow_id},
)

self.create_audit_entry(
request=request,
Expand Down
13 changes: 11 additions & 2 deletions src/sentry/projects/project_rules/creator.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
from collections.abc import Sequence
from dataclasses import dataclass
from typing import Any
Expand All @@ -11,6 +12,8 @@
from sentry.types.actor import Actor
from sentry.workflow_engine.migration_helpers.issue_alert_migration import IssueAlertMigrator

logger = logging.getLogger(__name__)


@dataclass
class ProjectRuleCreator:
Expand All @@ -32,8 +35,14 @@ def run(self) -> Rule:
if features.has(
"organizations:workflow-engine-issue-alert-dual-write", self.project.organization
):
# TODO(cathy): handle errors from broken actions
IssueAlertMigrator(self.rule, self.request.user.id if self.request else None).run()
# uncaught errors will rollback the transaction
workflow = IssueAlertMigrator(
self.rule, self.request.user.id if self.request else None
).run()
logger.info(
"workflow_engine.issue_alert.migrated",
extra={"rule_id": self.rule.id, "workflow_id": workflow.id},
)

return self.rule

Expand Down
11 changes: 9 additions & 2 deletions src/sentry/projects/project_rules/updater.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
from collections.abc import Sequence

from attr import dataclass
Expand All @@ -12,6 +13,8 @@
update_migrated_issue_alert,
)

logger = logging.getLogger(__name__)


@dataclass
class ProjectRuleUpdater:
Expand Down Expand Up @@ -43,8 +46,12 @@ def run(self) -> Rule:
if features.has(
"organizations:workflow-engine-issue-alert-dual-write", self.project.organization
):
# TODO(cathy): handle errors from broken actions
update_migrated_issue_alert(self.rule)
# uncaught errors will rollback the transaction
workflow = update_migrated_issue_alert(self.rule)
logger.info(
"workflow_engine.issue_alert.updated",
extra={"rule_id": self.rule.id, "workflow_id": workflow.id},
)
return self.rule

def _update_name(self) -> None:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def create_workflow_actions(if_dcg: DataConditionGroup, actions: list[dict[str,
DataConditionGroupAction.objects.bulk_create(dcg_actions)


def update_migrated_issue_alert(rule: Rule):
def update_migrated_issue_alert(rule: Rule) -> Workflow:
data = rule.data

try:
Expand Down Expand Up @@ -146,6 +146,8 @@ def update_migrated_issue_alert(rule: Rule):
workflow.enabled = True
workflow.save()

return workflow


def update_dcg(
dcg: DataConditionGroup,
Expand Down Expand Up @@ -175,14 +177,15 @@ def update_dcg(
return dcg


def delete_migrated_issue_alert(rule: Rule):
def delete_migrated_issue_alert(rule: Rule) -> int:
try:
alert_rule_workflow = AlertRuleWorkflow.objects.get(rule=rule)
except AlertRuleWorkflow.DoesNotExist:
logger.exception("AlertRuleWorkflow does not exist", extra={"rule_id": rule.id})
return

workflow: Workflow = alert_rule_workflow.workflow
workflow_id = workflow.id

try:
# delete all associated IF DCGs and their conditions
Expand Down Expand Up @@ -210,6 +213,8 @@ def delete_migrated_issue_alert(rule: Rule):
workflow.delete()
alert_rule_workflow.delete()

return workflow_id


def delete_workflow_actions(if_dcg: DataConditionGroup):
dcg_actions = DataConditionGroupAction.objects.filter(condition_group=if_dcg)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def __init__(
self.project = rule.project
self.organization = self.project.organization

def run(self) -> None:
def run(self) -> Workflow:
error_detector = self._create_detector_lookup()
conditions, filters = split_conditions_and_filters(self.data["conditions"])
action_match = self.data.get("action_match") or Rule.DEFAULT_CONDITION_MATCH
Expand Down

0 comments on commit 5975097

Please sign in to comment.