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. namespace CheckNumber
  13. {
  14. public static class CheckNumber
  15. {
  16. // Create TelemetryClient object
  17. private static TelemetryClient telemetryClient = new TelemetryClient(new TelemetryConfiguration("INSTRUMENTATION_KEY"));
  18. [FunctionName("CheckNumber")]
  19. public static async Task<IActionResult> Run(
  20. [HttpTrigger(AuthorizationLevel.Function, "get", Route = null)] HttpRequest req,
  21. ILogger log)
  22. {
  23. // Getting number query from request
  24. string number = req.Query["number"];
  25. string code;
  26. // Converting number to integer
  27. int num = Convert.ToInt32(number);
  28. // Check if the number is prime
  29. if (isPrime(num))
  30. {
  31. code = "003";
  32. }
  33. // Check if the number is even
  34. else if (num % 2 == 0)
  35. {
  36. code = "002";
  37. }
  38. // Else number is odd
  39. else
  40. {
  41. code = "001";
  42. }
  43. // Report the code to Application Insights as Trace
  44. telemetryClient.TrackTrace(code);
  45. // Also return it
  46. return new OkObjectResult(code);
  47. }
  48. private static bool isPrime(int num)
  49. {
  50. int n1 = num / 2;
  51. for (int x = 2; x <= n1; x++)
  52. {
  53. if (num % x == 0)
  54. {
  55. return false;
  56. }
  57. }
  58. return true;
  59. }
  60. }
  61. }

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.