Adding Custom metrics to Azure Application Insights

Azure Application Insights is popular for infrastructure monitoring in Azure. But it can also be used for monitoring system and user behaviours.

The steps required for getting insights about different behaviours in any application hosted in azure is listed below

Let’s assume that our application is a Function App and create the application in Azure portal

Create App Function with Application Insights enabled

New Function App
Enable Application Insights

Create a new function below the created function app

Select In Portal Option

Select In Portal option

Select Webhook API


Select Webhook API
New Function Created

Add Application Insights key to Function App Settings

Copy the Instrumentation Key from The created Application Insights overview page and add it to
APPINSIGHTS_INSTRUMENTATIONKEY in app settings of the function app

Copy Instrumentation key
Add key to app settings

Use “Microsoft.ApplicationInsights” nuget package to record the data that is going to be used for the insights

Install the “Microsoft.ApplicationInsights” to your code. Below line in portal editor is equivalent to install package is Visual Studio.

#r "Microsoft.ApplicationInsights"

Use “TelemetryClient” class to create different metrics.

private static TelemetryClient telemetryClient = new TelemetryClient();

var numberOfRequestsMetric = telemetryClient.GetMetric("NumberOfRequests");
var failedRequestsMetric = telemetryClient.GetMetric("FailedRequests"); 
var successRequestsMetric = telemetryClient.GetMetric("SuccessRequests");

Then the created metrics objects will be used for recording the required values

numberOfRequestsMetric.TrackValue(numberOfRequests);
failedRequestsMetric.TrackValue(failedRequests);
successRequestsMetric.TrackValue(successRequests);

The complete code is as follows

#r "Newtonsoft.Json"
#r "Microsoft.ApplicationInsights"

using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;
using System;

using Microsoft.ApplicationInsights;

private static TelemetryClient telemetryClient = new TelemetryClient();

public static async Task<IActionResult> Run(HttpRequest req, ILogger log)
{
    log.LogInformation("C# HTTP trigger function processed a request.");

    Random randm = new Random();
    int numberOfRequests = randm.Next(1,200);
    int failedRequests = randm.Next(1,100);
    int successRequests = randm.Next(1,100);

    var numberOfRequestsMetric = telemetryClient.GetMetric("NumberOfRequests");
    numberOfRequestsMetric.TrackValue(numberOfRequests);

    var failedRequestsMetric = telemetryClient.GetMetric("FailedRequests");
    failedRequestsMetric.TrackValue(failedRequests);

    var successRequestsMetric = telemetryClient.GetMetric("SuccessRequests");
    successRequestsMetric.TrackValue(successRequests);

    string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
    return (ActionResult)new OkObjectResult("Recorded");
}

Run new API many times to create the metrics and insert some random values

Click run button to run the logic

In previous section we have created three metrics in Azure Application Insights and inserted some random values to them. These metrics can be viewed in Application Insights charts as it is done for regular infrastructure metrics.

View created metrics in Azure Application Insights charts

Click Application Insights link in Function App Overview tab

Navigate to metrics page and select azure.applicationinsights namespace from drop down list. Then you can find the metrics created in previous steps in metric drop down list.

Add metrics to dashboard

The final dashboard can be like the below image

Chart of created metrics

2 thoughts on “Adding Custom metrics to Azure Application Insights”

Leave a Reply

Your email address will not be published. Required fields are marked *