Application Insights, a feature of Azure Monitor, is an extensible Application Performance Management (APM) service for developers and DevOps professionals.
We can use it to monitor live applications. It will automatically detect performance anomalies, and includes powerful analytics tools to help diagnose issues .
It’s designed to help you continuously improve performance and usability. It works for apps on a wide variety of platforms including .NET, Node.js, Java, and Python hosted on-premises, hybrid, or any public cloud.
After you’ve deployed your web app/website, you can set up recurring tests to monitor availability and responsiveness. Application Insights sends web requests to your application at regular intervals from points around the world. It can alert you if your application isn’t responding, or if it responds too slowly.
You can set up availability tests for any HTTP or HTTPS endpoint that is accessible from the public internet. You don’t have to make any changes to the website you’re testing. In fact, it doesn’t even have to be a site you own. You can test the availability of a REST API that your service depends on.
We are going to implement this using Terraform.
First of all we require to deploy our application insight resource.
resource "azurerm_application_insights" "apinsights" {
name = "chabot-ai-availability-${var.environment}"
location = "West Europe"
resource_group_name = var.resource_group
application_type = "web"
}
You should already have an action group created to send email notifications, if not then you can use the following block to deploy it:
resource "azurerm_monitor_action_group" "action_group_alert" {
name = "action-group-bot-alert-prod"
resource_group_name = var.resource_group
short_name = "ag-botprod"
depends_on=["module.vm"]
dynamic "email_receiver" {
for_each = var.admin_email
content {
name = "sendto-${email_receiver.key}"
email_address = email_receiver.value
}
}
arm_role_receiver {
name = "sentorolemonitoringreader"
role_id = "43d0d8ad-25c7-4714-9337-8ba259a9fe05"
use_common_alert_schema = true
}
arm_role_receiver {
name = "sentorolemonitoringcontributor"
role_id = "749f88d5-cbae-40b8-bcfc-e573ddc772fa"
use_common_alert_schema = true
}
}
Then we will create a web availability test that will monitor an specific endpoint to see if it return an specific http code (200):
resource "azurerm_application_insights_web_test" "bot-web-test" {
name = "chabot-ai-availability-web-test-${var.environment}"
location = "West Europe"
resource_group_name = var.resource_group
application_insights_id = azurerm_application_insights.apinsights.id
kind = "ping"
frequency = 300
timeout = 60
enabled = true
geo_locations = ["emea-nl-ams-azr", "emea-gb-db3-azr"]
configuration = <<XML
<WebTest Name="WebTest1" Id="ABD48585-0831-40CB-9069-682EA6BB3583" Enabled="True" CssProjectStructure="" CssIteration="" Timeout="0" WorkItemIds="" xmlns="http://microsoft.com/schemas/VisualStudio/TeamTest/2010" Description="" CredentialUserName="" CredentialPassword="" PreAuthenticate="True" Proxy="default" StopOnError="False" RecordedResultFile="" ResultsLocale="">
<Items>
<Request Method="GET" Guid="a5f10126-e4cd-570d-961c-cea43999a200" Version="1.1" Url="https://${var.vm.fqdn}.francecentral.cloudapp.azure.com/servlet/api/serverstatus?details=true&pretty=true" ThinkTime="0" Timeout="300" ParseDependentRequests="True" FollowRedirects="True" RecordResult="True" Cache="False" ResponseTimeGoal="0" Encoding="utf-8" ExpectedHttpStatusCode="200" ExpectedResponseUrl="" ReportingName="" IgnoreHttpStatusCode="False" />
</Items>
</WebTest>
XML
Notice that in order to configure the Webtest use XML where we can define the Url to monitor and the expected http status code. Applications insights will query this URL every now and then and if it returns a different code (such as 500) or nothing at all then this can be used to trigger an Alert.
As we have seen in previous articles, we can use an Azure Monitor Scheduled Rule Alert in order to create an alert:
resource "azurerm_monitor_scheduled_query_rules_alert" "example" {
name = "monitor-bot-availability-${var.environment}"
location = azurerm_application_insights_web_test.bot-web-test.location
resource_group_name = var.resource_group
action {
action_group = [ azurerm_monitor_action_group.action_group_alert.id ]
email_subject = "Avalability alert of site https://${var.vm.fqdn}.francecentral.cloudapp.azure.com"
}
data_source_id = azurerm_application_insights.apinsights.id
description = "Avalability alert of site https://${var.vm.fqdn}.francecentral.cloudapp.azure.com"
enabled = true
# Count all requests with server error result code grouped into 5-minute bins
query = <<-QUERY
availabilityResults
| where timestamp >= ago(10min) and success==0
| summarize count() by bin(timestamp, 10m)
| order by timestamp asc | render timechart
QUERY
severity = 1
frequency = 5
time_window = 5
trigger {
operator = "GreaterThan"
threshold = 0
}
}
Once this has been done, if your website or api goes down then we you will get an alert by mail (as defined in the action group).
Happy terraforming!