Creating Custom Tiles In Microsoft Azure Application Insights

Introduction

 
Application Insights allow developers to report logs, performance metrics, request logs, generate application maps of a software system, and much more. The main purpose is to keep track of the software system and monitor its activity and performance. Application Insights allow the developers to create their own custom tiles for the dashboard.
 
Problem Statement
 
We have an Azure Function (Http Trigger) that takes a number as a query from URL and reports "001" if the number is odd, "002" if the number is even, and "003" if the number is prime. It's reported to Azure Application Insights as a trace. We have to make a tile for the dashboard that displays how many times each of the code was reported in a day and display it as a chart.
 
Solution
 
Develop a custom tile using a custom query in Kusto in Application Insights Logs, and pin the tile to the dashboard.
 
Prerequisites
  • Microsoft Azure Subscription
  • Application Insights resource deployed on Azure Portal
  • Visual Studio 2019
  • Azure SDK for Visual Studio
  • .NET Core
  • Postman for testing

Obtaining Instrumentation Key From Application Insights

 
Once the Application Insights resource is deployed on the Azure portal. Go to your Application Insights resource dashboard and retrieve the instrumentation key as shown below.
 
Creating Custom Tiles in Microsoft Azure Application Insights
 

Developing an Azure Function

 
Installing Microsoft.ApplicationInsights Package
 
In order to use application insights, you have to install Microsoft.ApplicationInsights from NuGet package manager in Visual Studio.
 
Creating Custom Tiles in Microsoft Azure Application Insights
 
Function Code
 
Open Visual Studio and develop the Azure Function with an Http Trigger, as shown in the code below:
  1. using System;  
  2. using System.IO;  
  3. using System.Threading.Tasks;  
  4. using Microsoft.AspNetCore.Mvc;  
  5. using Microsoft.Azure.WebJobs;  
  6. using Microsoft.Azure.WebJobs.Extensions.Http;  
  7. using Microsoft.AspNetCore.Http;  
  8. using Microsoft.Extensions.Logging;  
  9. using Newtonsoft.Json;  
  10. using Microsoft.ApplicationInsights;  
  11. using Microsoft.ApplicationInsights.Extensibility;  
  12.   
  13. namespace CheckNumber  
  14. {  
  15.     public static class CheckNumber  
  16.     {  
  17.         // Create TelemetryClient object  
  18.         private static TelemetryClient telemetryClient = new TelemetryClient(new TelemetryConfiguration("INSTRUMENTATION_KEY"));  
  19.   
  20.         [FunctionName("CheckNumber")]  
  21.         public static async Task<IActionResult> Run(  
  22.             [HttpTrigger(AuthorizationLevel.Function, "get", Route = null)] HttpRequest req,  
  23.             ILogger log)  
  24.         {  
  25.             // Getting number query from request  
  26.             string number = req.Query["number"];  
  27.   
  28.             string code;  
  29.   
  30.             // Converting number to integer  
  31.             int num = Convert.ToInt32(number);  
  32.   
  33.             // Check if the number is prime  
  34.             if (isPrime(num))  
  35.             {  
  36.                 code = "003";  
  37.             }  
  38.             // Check if the number is even  
  39.             else if (num % 2 == 0)  
  40.             {  
  41.                 code = "002";  
  42.             }  
  43.             // Else number is odd  
  44.             else  
  45.             {  
  46.                 code = "001";  
  47.             }  
  48.   
  49.             // Report the code to Application Insights as Trace  
  50.             telemetryClient.TrackTrace(code);  
  51.   
  52.             // Also return it  
  53.             return new OkObjectResult(code);  
  54.         }  
  55.   
  56.         private static bool isPrime(int num)  
  57.         {  
  58.             int n1 = num / 2;  
  59.   
  60.             for (int x = 2; x <= n1; x++)  
  61.             {  
  62.                 if (num % x == 0)  
  63.                 {  
  64.                     return false;  
  65.                 }  
  66.             }  
  67.             return true;  
  68.         }  
  69.     }  
  70. }  

Running and Testing the Function

 
Run the function.
 
Creating Custom Tiles in Microsoft Azure Application Insights
 
Open up postman and test the function as shown in the picture below.
 
Creating Custom Tiles in Microsoft Azure Application Insights
 
Test it multiple times with different numbers so the function can report logs to Application Insights.
 

Creating a Custom Tile

 
Go to your Application Insights resource deployed on Azure Portal. In the left panel, under Monitoring click on Logs and it will open a window on the right where you can write your custom queries in Kusto.
 
Creating Custom Tiles in Microsoft Azure Application Insights
 
Now write the following query in the panel and click on Run.
  1. traces    
  2. | summarize count() by message    
Now you can view the results, click on Charts and select the chart you want.
 
Creating Custom Tiles in Microsoft Azure Application Insights
 
The next step is to pin the chart results to the dashboard, just click on Pin to dashboard and select the dashboard you want to pin it to.
 
Creating Custom Tiles in Microsoft Azure Application Insights
 
View Tile in Dashboard
 
Go to your dashboard where you had pinned the tile and you can view it there, it will always show the latest results.
  
Creating Custom Tiles in Microsoft Azure Application Insights
 

Conclusion

 
You can create any tile you want for anything. In this case, we have used simple Trace, but one can use Requests, Availability, Exceptions, and much more. You can write even more complex queries with Kusto and generate better charts and tiles, such as using unions and joins, etc. Many operations that can be done in SQL can be done by Kusto. For more information see the SQL to Kusto cheat sheat.