Skip to content

Commit cf043de

Browse files
committed
Add an Alert on Exceptions for App Insights
We want to trigger an alert whenever there is an exception for manage-breast-screening - specifically for the Invite team's container app jobs in the notifications app. This should trigger whenever the Exception count is above 0 - may want to tweak that but in theory there should never be an Exception unless something has completely failed.
1 parent b7a1915 commit cf043de

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
resource "azurerm_monitor_metric_alert" "exceptions" {
2+
count = var.enable_alerting ? 1 : 0
3+
4+
auto_mitigate = true
5+
description = "Triggered by any Exception"
6+
enabled = true
7+
frequency = var.alert_frequency
8+
name = "Exceptions"
9+
resource_group_name = var.resource_group_name
10+
scopes = [azurerm_application_insights.appins.id]
11+
severity = 1
12+
window_size = local.alert_window_size
13+
14+
action {
15+
action_group_id = var.action_group_id
16+
}
17+
18+
criteria {
19+
aggregation = "Count"
20+
metric_name = "exceptions/count"
21+
metric_namespace = "microsoft.insights/components"
22+
operator = "GreaterThan"
23+
skip_metric_validation = false
24+
threshold = 0
25+
}
26+
}

infrastructure/modules/app-insights/tfdocs.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,30 @@ Type: `string`
3838

3939
The following input variables are optional (have default values):
4040

41+
### <a name="input_action_group_id"></a> [action\_group\_id](#input\_action\_group\_id)
42+
43+
Description: The ID of the Action Group to use for alerts.
44+
45+
Type: `string`
46+
47+
Default: `null`
48+
49+
### <a name="input_alert_frequency"></a> [alert\_frequency](#input\_alert\_frequency)
50+
51+
Description: The frequency an alert is checked e.g. PT1M, PT5M, PT15M, PT30M, PT1H, PT6H, PT12H.
52+
53+
Type: `string`
54+
55+
Default: `"PT5M"`
56+
57+
### <a name="input_enable_alerting"></a> [enable\_alerting](#input\_enable\_alerting)
58+
59+
Description: Whether monitoring and alerting is enabled for this module.
60+
61+
Type: `bool`
62+
63+
Default: `false`
64+
4165
### <a name="input_tags"></a> [tags](#input\_tags)
4266

4367
Description: A mapping of tags to assign to the resource.
@@ -70,3 +94,4 @@ Description: n/a
7094
The following resources are used by this module:
7195

7296
- [azurerm_application_insights.appins](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/application_insights) (resource)
97+
- [azurerm_monitor_metric_alert.exceptions](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/monitor_metric_alert) (resource)

infrastructure/modules/app-insights/variables.tf

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,30 @@ variable "resource_group_name" {
3333
type = string
3434
description = "The name of the resource group in which the App Insights is created. Changing this forces a new resource to be created."
3535
}
36+
37+
variable "enable_alerting" {
38+
description = "Whether monitoring and alerting is enabled for this module."
39+
type = bool
40+
default = false
41+
}
42+
43+
variable "alert_frequency" {
44+
type = string
45+
nullable = true
46+
default = "PT5M"
47+
validation {
48+
condition = contains(["PT1M", "PT5M", "PT15M", "PT30M", "PT1H"], var.alert_frequency)
49+
error_message = "The alert_frequency must be one of: PT1M, PT5M, PT15M, PT30M, PT1H"
50+
}
51+
description = "The frequency an alert is checked e.g. PT1M, PT5M, PT15M, PT30M, PT1H, PT6H, PT12H."
52+
}
53+
54+
variable "action_group_id" {
55+
type = string
56+
description = "The ID of the Action Group to use for alerts."
57+
default = null
58+
}
59+
60+
locals {
61+
alert_window_size = var.alert_frequency
62+
}

0 commit comments

Comments
 (0)