Calculating availability based on App Insights logs with Azure CLI

In this blog post, I will describe how to calculate availability (SLA) based on logs generated for existing "Web Test" in Application Insights. We will use Azure CLI to get data from a given period and Powershell to perform simple math to get the availability percentage value.

Calculating availability based on App Insights logs with Azure CLI

The idea

Let's assume we have an existing Application Insights resource with three availability tests. We want to calculate the availability for the given month for the Web Test named SampleWebTest2 programmatically.

Application Insights Web Tests

The idea is to fetch all logs related to availability and compare the number of "successful" ones with the sum of all logs. To simplify, we will grab logs for all regions included in the test instead of individual ones. If you were doing it manually, you would go to the Logs menu item in your App Insights resource and write a query to get logs of type availabilityResults. You can't see it in the screenshot, but I set the time range for January 2023.

Application Insights Logs

In the case above, availability would equal 44590 / 44652 * 100% ≈ 99.86%. This result coincides with the one obtained on the UI after narrowing down the date filter.

Application Insights Filter
Application Insights Availability

Authentication

First of all, we need to authenticate. Depending on your needs you can use your account:

az login
Command

or a service principal.

az login    --service-principal    --username {client id}    --password {client secret}    --tenant {tenant id}
Command

After that, select the subscription where the Application Insights resource is created.

az account set -s {your azure subscription id}
Command

If you're using Azure Pipelines, you can use AzureCLI@2 task, which will do it automatically for you.

Calculating availability

Before using the script below, you have to install the Azure CLI extension named application-insights. To do that, just run:

az extension add --name application-insights
Command

The Powershell script below takes year and month as parameters for which we would like to calculate availability. It calculates the first and last day of a month and invokes the az monitor app-insights query command including the query we prepared earlier. After fetching logs, we can calculate the availability. Before using, please adjust the $resource_group_name, $app_insights_name and $web_test_name variables to your case.

param (	[Parameter(Mandatory = $true)][string]$year,	[Parameter(Mandatory = $true)][string]$month)
$resource_group_name = "rg-sample"$app_insights_name = "appi-sample"$web_test_name = "SampleWebTest2"
$date = Get-Date -Month $month -Year $year -Hour 0 -Minute 0 -Second 0$first_day_of_month = Get-Date $date -Day 1$last_day_of_month = Get-Date $first_day_of_month.AddMonths(1).AddSeconds(-1)
$start_date = $first_day_of_month.ToString("yyyy-MM-dd HH:mm:ss")$end_date = $last_day_of_month.ToString("yyyy-MM-dd HH:mm:ss")
$query = "availabilityResults | where name == '$web_test_name' | " +         "summarize countif(success == 1), count()"
$logs = az monitor app-insights query `    --resource-group $resource_group_name `    --app $app_insights_name `    --analytics-query $query `    --start-time "$start_date" --end-time "$end_date" | Out-String | ConvertFrom-Json
return [math]::Round($logs.tables.rows[0] / $logs.tables.rows[1] * 100, 2)
PowerShell

When we invoke the script we get the result.

Application Insights availability result

Conclusion

I use this script in my automation to generate the monthly availability report for my apps - it works fine for me. The script from this article includes logs from all supported regions, but you can modify this to calculate availability per region.

Tagged: Azure, CI/CD

Comments

The comments system is based on GitHub Issues API. Once you add your comment to the linked issue on my GitHub repository, it will show up below.