Skip to content

Commit 70342c8

Browse files
DTOSS-12524: Add opt-in log-based exception alert to app-insights module
Adds use_log_based_exception_alert variable (default false) to the app-insights module. When enabled, replaces the metric-based exceptions alert with a scheduled query rule that projects exception type, message, affected URL, and operation_Id — giving downstream processors (e.g. a Slack transformer Logic App) the fields needed to build rich notifications. The metric alert is preserved and unchanged for existing consumers. Also exposes app_id output for use with the App Insights REST API.
1 parent 0abead1 commit 70342c8

File tree

3 files changed

+64
-1
lines changed

3 files changed

+64
-1
lines changed

infrastructure/modules/app-insights/alerts.tf

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
# Metric alert — retained for backward compatibility with existing consumers.
2+
# Suppressed when use_log_based_exception_alert = true to avoid duplicate notifications.
13
resource "azurerm_monitor_metric_alert" "exceptions" {
2-
count = var.enable_alerting ? 1 : 0
4+
count = var.enable_alerting && !var.use_log_based_exception_alert ? 1 : 0
35

46
auto_mitigate = true
57
description = "Triggered by any Exception"
@@ -24,3 +26,48 @@ resource "azurerm_monitor_metric_alert" "exceptions" {
2426
threshold = 0
2527
}
2628
}
29+
30+
# Log-based alert — opt-in replacement for the metric alert.
31+
# Projects exception type, message, URL, and operation_Id so that downstream
32+
# processors (e.g. a Slack transformer Logic App) can surface those fields
33+
# directly in notifications without a secondary query.
34+
resource "azurerm_monitor_scheduled_query_rules_alert_v2" "exceptions" {
35+
count = var.enable_alerting && var.use_log_based_exception_alert ? 1 : 0
36+
37+
auto_mitigation_enabled = true
38+
description = "Application exception detected in ${var.name}"
39+
enabled = true
40+
evaluation_frequency = var.alert_frequency
41+
location = var.location
42+
name = "${var.name}-exceptions-alert"
43+
resource_group_name = var.resource_group_name
44+
scopes = [azurerm_application_insights.appins.id]
45+
severity = 1
46+
skip_query_validation = false
47+
target_resource_types = ["microsoft.insights/components"]
48+
window_duration = local.alert_window_size
49+
workspace_alerts_storage_enabled = false
50+
51+
action {
52+
action_groups = [var.action_group_id]
53+
}
54+
55+
criteria {
56+
operator = "GreaterThan"
57+
threshold = 0
58+
time_aggregation_method = "Count"
59+
60+
# Projects the fields needed for rich Slack notifications.
61+
# The alert fires whenever any exception appears in the evaluation window.
62+
query = <<-QUERY
63+
exceptions
64+
| where timestamp > ago(5m)
65+
| project timestamp, type, outerMessage, url = tostring(customDimensions["url"]), operation_Id
66+
QUERY
67+
68+
failing_periods {
69+
minimum_failing_periods_to_trigger_alert = 1
70+
number_of_evaluation_periods = 1
71+
}
72+
}
73+
}

infrastructure/modules/app-insights/output.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ output "id" {
66
value = azurerm_application_insights.appins.id
77
}
88

9+
output "app_id" {
10+
description = "The Application ID (GUID) used to authenticate with the App Insights REST API."
11+
value = azurerm_application_insights.appins.app_id
12+
}
13+
914
output "name" {
1015
value = azurerm_application_insights.appins.name
1116
}

infrastructure/modules/app-insights/variables.tf

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,17 @@ variable "action_group_id" {
5757
default = null
5858
}
5959

60+
variable "use_log_based_exception_alert" {
61+
description = <<EOT
62+
Replace the default metric-based exceptions alert with a log-based scheduled query alert.
63+
The log-based alert projects exception type, message, affected URL, and operation ID,
64+
which enables the Slack notification transformer to include those fields directly.
65+
Existing consumers of this module are unaffected — the default retains the metric alert.
66+
EOT
67+
type = bool
68+
default = false
69+
}
70+
6071
locals {
6172
alert_window_size = var.alert_frequency
6273
}

0 commit comments

Comments
 (0)