Metrics Explorer – Custom metrics, events and properties

Knowing how people use your application is vital to shaping your development priorities and architecture. To get that information, Visual Studio Application Insights makes it easy to insert a few snippets of code that send telemetry about how each feature is being used.

With this release of Application Insights, we have added segmentation and filtering on custom metrics, events and properties. In a previous blog, I’ve shown how you can segment and filter standard events like page views. But custom metrics, events and properties add an extra level of power to let you really understand what users are doing with your app.

Using custom metrics, properties and events to gain insights

Let’s take an example of a web app that lets you upload and download videos. I’ll show how easy it is to get answers to useful questions by inserting just a few lines of code. In this example, the code is in the server side of a web app, but you can do the same thing in web pages, or mobile apps..

Tracking events

How many video uploads and downloads are there?

To answer this question, I insert some code into my app, to record each event::

var appInsights = new TelemetryClient();

// In my file upload method:

appInsights.TrackEvent(“FileUpload”);

// In my file download method:

appInsights.TrackEvent(“FileDownload”);

After deploying the app, I can go to Application Insights to compare the trends in uploads and downloads. From Application Insights overview blade I open Metrics Explorer(ME) and select the chart to edit it. I pick Events(Sum) as the metric and then segment by Event name (In future you’ll be able to pick individual events also as a metric).

 

Which video file formats are most popular?

To answer this question, I have to add the file type information to each event. While I’m there, I’ll add some data about file sizes and load times as well. Again, I insert this code in the right place in my app:

var appInsights = new TelemetryClient();

// In my file upload method:

var properties = new Dictionary <string, string> ();

var metrics = new Dictionary <string, double> ();

properties[“FileType”] = fileType;

metrics[“FileSize(MB)”] = fileSize;

metrics[“LoadTime(ms)”] = processingTime;

appInsights.TrackEvent(“FileUpload”, properties, metrics);

 In Metrics Explorer, segment the Events by File Type: 

 

What are the average sizes of different types of file?

We can get that from the telemetry I coded above. Just chart the File size, segmenting by File Type:

Tracking pure metrics

Sometimes you want to track metrics that aren’t attached to any specific event. For example, I’d like to know my processing queue length and CPU occupancy, which are rather independent of customer events.

So I write some code that sends these measurements at regular intervals in a background task. I use the TrackMetric() API instead of TrackEvent(). For example:

// Background thread:

private void Run() {

 var appInsights = new TelemetryClient();

 while (true) {

  Thread.Sleep(60000);

  appInsights.TrackMetric(“ProcessingQueueLength”, queue.Length);

 }

}

 

Adding just a few more lines of code, I can quickly display a rich set of metrics that tell me about how my app is being used, and how it is performing

When to use TrackMetric() and TrackEvent()

Use TrackEvent when:

  • Each specific event is important – you can view them in Diagnostic Search.
  • Metrics are measured with each event, so that you can segment the metrics by event type and properties of the event.
  • Users/sessions centric metrics are important for the event for e.g. if you want to know the region from which users are uploading files (enables you to make a decision about getting a processing server in that region)

Use TrackMetric when:

  • The metric is independent of any specific event.
  • You want to measure things on specific intervals (such as CPU or queue length every minute).
  • You have a high volume of metrics and would like to collect and aggregate locally before sending it.  We will talk about aggregation in depth in a later blog post.

Summary

Custom metrics, events and properties give you a 360-degree view of your application with  powerful insights into what users are doing with your application and how it is performing. 

Get started with Application Insights now if you have not already done so.