Tracking custom metrics in Application Insights is easy. I wanted to track how long our cash register takes to print receipts so I could compare performance across hardware and make better recommendations to our sales team and diagnose customer issues related to printing speed.
You will need a TelemetryClient instance. Use the GetMetric() method to get or create a metric by name. You can use the overloads to provide names for additional custom dimensions. In this case I am tracking the receipt number and the number of images printed on the receipt.
Call TrackValue() to add a new measurement. The TelemetryClient will aggregate the metrics over time and report them to Application Insights.
The default interval appears to be 54 seconds.
In my case, aggregation is not doing much since each printed receipt has unique dimensions and a register is not likely to print more than one receipt every 54 seconds.
var metric = _telemetry.GetMetric("PrintReceiptDurationMs", "ReceiptNumber", "ImageCount"); metric.TrackValue(sw.ElapsedMilliseconds, receipt.ReceiptNumber, imageCount.ToString());
In Log Analytics you can now query for the results.
customMetrics | where name == 'PrintReceiptDurationMs' | extend receipt_number = tostring(customDimensions.ReceiptNumber) | extend image_count = todouble(customDimensions.ImageCount) | project value, receipt_number, image_count | order by receipt_number desc
Or you could plot a chart.
customMetrics | where name == 'PrintReceiptDurationMs' | summarize avg(value) by todouble(customDimensions.ImageCount) | render barchart
You can query across all metrics if they share common custom dimensions.
customMetrics | where customDimensions.ReceiptNumber == 'RC-00092261-7' | project name, value, timestamp | order by name