Skip to content

chore(ACI): Add metric for dual processing in metric alerts #91979

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 20, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 17 additions & 17 deletions src/sentry/incidents/subscription_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,16 +368,6 @@ def process_update(self, subscription_update: QuerySubscriptionUpdate) -> None:
"organizations:anomaly-detection-alerts", organization
) and features.has("organizations:anomaly-detection-rollout", organization)

alert_triggered_tags = {
"detection_type": self.alert_rule.detection_type,
"organization_id": None,
}
if features.has(
"organizations:workflow-engine-metric-alert-dual-processing-logs",
self.subscription.project.organization,
):
alert_triggered_tags["organization_id"] = self.alert_rule.organization_id

potential_anomalies = None
if (
has_anomaly_detection
Expand Down Expand Up @@ -440,7 +430,7 @@ def process_update(self, subscription_update: QuerySubscriptionUpdate) -> None:
) and not self.check_trigger_matches_status(trigger, TriggerStatus.ACTIVE):
metrics.incr(
"incidents.alert_rules.threshold.alert",
tags=alert_triggered_tags,
tags={"detection_type": self.alert_rule.detection_type},
)
incident_trigger = self.trigger_alert_threshold(
trigger, aggregation_value
Expand All @@ -457,7 +447,7 @@ def process_update(self, subscription_update: QuerySubscriptionUpdate) -> None:
):
metrics.incr(
"incidents.alert_rules.threshold.resolve",
tags=alert_triggered_tags,
tags={"detection_type": self.alert_rule.detection_type},
)
incident_trigger = self.trigger_resolve_threshold(
trigger, aggregation_value
Expand All @@ -477,10 +467,12 @@ def process_update(self, subscription_update: QuerySubscriptionUpdate) -> None:
) and not self.check_trigger_matches_status(trigger, TriggerStatus.ACTIVE):
# If the value has breached our threshold (above/below)
# And the trigger is not yet active
metrics.incr(
"incidents.alert_rules.threshold.alert",
tags=alert_triggered_tags,
)
metrics.incr("incidents.alert_rules.threshold.alert")
if features.has(
"organizations:workflow-engine-metric-alert-dual-processing-logs",
self.subscription.project.organization,
):
Comment on lines +474 to +477
Copy link
Member

Choose a reason for hiding this comment

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

nit: pull into its own variable so we don't have to check the feature flag twice here

metrics.incr("dual_processing.alert_rules.fire")
Copy link
Member Author

Choose a reason for hiding this comment

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

I don't need the detection type to filter out anomaly detection alerts (which are not yet migrated) because those get fired and resolved in the if block above this

# triggering a threshold will create an incident and set the status to active
incident_trigger = self.trigger_alert_threshold(trigger, aggregation_value)
if incident_trigger is not None:
Expand All @@ -497,8 +489,16 @@ def process_update(self, subscription_update: QuerySubscriptionUpdate) -> None:
):
metrics.incr(
"incidents.alert_rules.threshold.resolve",
tags=alert_triggered_tags,
tags={"detection_type": self.alert_rule.detection_type},
)
if features.has(
"organizations:workflow-engine-metric-alert-dual-processing-logs",
self.subscription.project.organization,
):
metrics.incr(
"dual_processing.alert_rules.fire",
tags={"detection_type": self.alert_rule.detection_type},
)
incident_trigger = self.trigger_resolve_threshold(
trigger, aggregation_value
)
Expand Down
19 changes: 5 additions & 14 deletions src/sentry/workflow_engine/processors/workflow.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import logging
from dataclasses import asdict, replace
from enum import StrEnum
from typing import Any

import sentry_sdk
from django.db import router, transaction
Expand Down Expand Up @@ -218,14 +217,6 @@ def process_workflows(event_data: WorkflowEventData) -> set[Workflow]:

# TODO: remove fetching org, only used for feature flag checks
organization = detector.project.organization
workflow_metric_tags: dict[str, Any] = {
"detector_type": detector.type,
"organization_id": None,
}
if features.has(
"organizations:workflow-engine-metric-alert-dual-processing-logs", organization
):
workflow_metric_tags["organization_id"] = organization.id

# Get the workflows, evaluate the when_condition_group, finally evaluate the actions for workflows that are triggered
workflows = set(
Expand All @@ -240,7 +231,7 @@ def process_workflows(event_data: WorkflowEventData) -> set[Workflow]:
metrics.incr(
"workflow_engine.process_workflows",
len(workflows),
tags=workflow_metric_tags,
tags={"detector_type": detector.type},
)

logger.info(
Expand All @@ -263,7 +254,7 @@ def process_workflows(event_data: WorkflowEventData) -> set[Workflow]:
metrics.incr(
"workflow_engine.process_workflows.triggered_workflows",
len(triggered_workflows),
tags=workflow_metric_tags,
tags={"detector_type": detector.type},
)

logger.info(
Expand All @@ -284,7 +275,7 @@ def process_workflows(event_data: WorkflowEventData) -> set[Workflow]:
metrics.incr(
"workflow_engine.process_workflows.actions",
amount=len(actions),
tags=workflow_metric_tags,
tags={"detector_type": detector.type},
)

logger.info(
Expand All @@ -309,7 +300,7 @@ def process_workflows(event_data: WorkflowEventData) -> set[Workflow]:
metrics.incr(
"workflow_engine.process_workflows.triggered_actions",
amount=len(actions),
tags=workflow_metric_tags,
tags={"detector_type": detector.type},
)
logger.info(
"workflow_engine.process_workflows.triggered_actions (batch)",
Expand All @@ -324,7 +315,7 @@ def process_workflows(event_data: WorkflowEventData) -> set[Workflow]:
# in order to check if workflow engine is firing 1:1 with the old system, we must only count once rather than each action
metrics.incr(
"workflow_engine.process_workflows.fired_actions",
tags=workflow_metric_tags,
tags={"detector_type": detector.type},
)

return triggered_workflows
15 changes: 12 additions & 3 deletions tests/sentry/incidents/test_subscription_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -1075,7 +1075,10 @@ def test_alert_metrics(self, mock_metrics):
[
call(
"incidents.alert_rules.threshold.alert",
tags={"detection_type": "static", "organization_id": rule.organization_id},
tags={"detection_type": "static"},
),
call(
"dual_processing.alert_rules.fire",
),
call("incidents.alert_rules.trigger", tags={"type": "fire"}),
],
Expand Down Expand Up @@ -1271,12 +1274,18 @@ def test_resolve_metrics(self, mock_metrics):
[
call(
"incidents.alert_rules.threshold.alert",
tags={"detection_type": "static", "organization_id": rule.organization_id},
tags={"detection_type": "static"},
),
call(
"dual_processing.alert_rules.fire",
),
call("incidents.alert_rules.trigger", tags={"type": "fire"}),
call(
"incidents.alert_rules.threshold.resolve",
tags={"detection_type": "static", "organization_id": rule.organization_id},
tags={"detection_type": "static"},
),
call(
"dual_processing.alert_rules.fire",
),
call("incidents.alert_rules.trigger", tags={"type": "resolve"}),
]
Expand Down
10 changes: 3 additions & 7 deletions tests/sentry/workflow_engine/processors/test_workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,6 @@ def setUp(self):
id=1, is_new=False, is_regression=True, is_new_group_environment=False
),
)
self.workflow_metric_tags = {
"detector_type": self.error_detector.type,
"organization_id": None,
}

def test_skips_disabled_workflows(self):
workflow_triggers = self.create_data_condition_group()
Expand Down Expand Up @@ -280,7 +276,7 @@ def test_metrics_with_workflows(self, mock_incr):
mock_incr.assert_any_call(
"workflow_engine.process_workflows",
1,
tags=self.workflow_metric_tags,
tags={"detector_type": self.error_detector.type},
)

@patch("sentry.utils.metrics.incr")
Expand All @@ -290,7 +286,7 @@ def test_metrics_triggered_workflows(self, mock_incr):
mock_incr.assert_any_call(
"workflow_engine.process_workflows.triggered_workflows",
1,
tags=self.workflow_metric_tags,
tags={"detector_type": self.error_detector.type},
)

@with_feature("organizations:workflow-engine-process-workflows")
Expand All @@ -302,7 +298,7 @@ def test_metrics_triggered_actions(self, mock_incr):
mock_incr.assert_any_call(
"workflow_engine.process_workflows.triggered_actions",
amount=0,
tags=self.workflow_metric_tags,
tags={"detector_type": self.error_detector.type},
)

@with_feature("organizations:workflow-engine-process-workflows")
Expand Down
Loading