{"id":8623,"date":"2015-02-07T14:53:00","date_gmt":"2015-02-07T14:53:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/visualstudioalm\/2015\/02\/07\/metrics-explorer-custom-metrics-events-and-properties\/"},"modified":"2022-07-28T02:28:20","modified_gmt":"2022-07-28T10:28:20","slug":"metrics-explorer-custom-metrics-events-and-properties","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/devops\/metrics-explorer-custom-metrics-events-and-properties\/","title":{"rendered":"Metrics Explorer &#8211; Custom metrics, events and properties"},"content":{"rendered":"<p>Knowing how people use your application is vital to shaping your development priorities and architecture. To get that information,<a href=\"http:\/\/azure.microsoft.com\/en-us\/services\/application-insights\/\"> Visual Studio Application\u00a0Insights<\/a> makes it easy to insert a few snippets of code that send telemetry about how each feature is being used.<\/p>\n<p>With this release of Application Insights, we have added <a href=\"http:\/\/azure.microsoft.com\/documentation\/articles\/app-insights-metrics-explorer\/\">segmentation and filtering<\/a> on custom metrics, events and properties. In a <a href=\"http:\/\/blogs.msdn.com\/b\/visualstudioalm\/archive\/2014\/12\/15\/application-insights-metric-explorer-me-part-2.aspx\">previous blog<\/a>, I&#8217;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.<\/p>\n<h2>Using custom metrics, properties and events to gain insights<\/h2>\n<p>Let\u2019s take an example of a web app that lets you upload and download videos. I\u2019ll 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,\u00a0<span>or <a href=\"http:\/\/azure.microsoft.com\/documentation\/articles\/app-insights-windows-monitor-usage-crashes\/\">mobile apps<\/a>.<\/span>.<\/p>\n<h3>Tracking events<\/h3>\n<p><strong>How many video uploads and downloads are there?<\/strong><\/p>\n<p>To answer this question, I insert some code into my app, to record each event::<\/p>\n<table style=\"background-color: #e0dedd\" border=\"1\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td valign=\"top\" width=\"623\">\n<p class=\"code\">\n          var appInsights = new TelemetryClient();\n        <\/p>\n<p class=\"code\">\n          &#8230;\n        <\/p>\n<p class=\"code\">\n          \/\/ In my file upload method:\n        <\/p>\n<p class=\"code\">\n          appInsights.TrackEvent(&#8220;FileUpload&#8221;);\n        <\/p>\n<p class=\"code\">\n          &#8230;\n        <\/p>\n<p class=\"code\">\n          \/\/ In my file download method:\n        <\/p>\n<p class=\"code\">\n          appInsights.TrackEvent(&#8220;FileDownload&#8221;);\n        <\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>After deploying the app, I can go to <a href=\"http:\/\/portal.azure.com\" title=\"Azure Portal - Application Insights\">Application Insights<\/a> to compare the trends in uploads and downloads. From Application Insights overview blade I open\u00a0<span><a href=\"http:\/\/azure.microsoft.com\/documentation\/articles\/app-insights-metrics-explorer\/\">Metrics Explorer(ME)<\/a><\/span>\u00a0and select the chart to edit it. I pick Events(Sum) as the metric and then segment by Event name (In future you\u2019ll be able to pick individual events also as a metric).<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2015\/02\/1537.EventsByFileType3.png\" alt=\"\" border=\"0\" \/><\/p>\n<p>\u00a0<\/p>\n<p>**Which video file formats are most popular?******<\/p>\n<p>To answer this question, I have to add the file type information to each event. While I\u2019m there, I\u2019ll add some data about file sizes and load times as well. Again, I insert this code in the right place in my app:<\/p>\n<table style=\"background-color: #e0dedd\" border=\"1\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td valign=\"top\" width=\"623\">\n<p class=\"code\">\n          var appInsights = new TelemetryClient();\n        <\/p>\n<p class=\"code\">\n          &#8230;\n        <\/p>\n<p class=\"code\">\n          \/\/ In my file upload method:\n        <\/p>\n<p class=\"code\">\n          var properties = new Dictionary <string, string> ();\n        <\/p>\n<p class=\"code\">\n          var metrics = new Dictionary <string, double> ();\n        <\/p>\n<p class=\"code\">\n          properties[&#8220;FileType&#8221;] = fileType;\n        <\/p>\n<p class=\"code\">\n          metrics[&#8220;FileSize(MB)&#8221;] = fileSize;\n        <\/p>\n<p class=\"code\">\n          metrics[&#8220;LoadTime(ms)&#8221;] = processingTime;\n        <\/p>\n<p class=\"code\">\n          appInsights.TrackEvent(&#8220;FileUpload&#8221;, properties, metrics);\n        <\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u00a0In <span><a href=\"http:\/\/azure.microsoft.com\/documentation\/articles\/app-insights-metrics-explorer\/\">Metrics Explorer<\/a><\/span>, segment the Events by File Type:\u00a0<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2015\/02\/2502.EventsByFileType2.png\" alt=\"\" border=\"0\" \/><\/p>\n<p>\u00a0<\/p>\n<p><strong>What are the average sizes of different types of file?<\/strong><\/p>\n<p>We can get that from the telemetry I coded above. Just chart the File size, segmenting by File Type:****<\/p>\n<p><strong><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2015\/02\/6355.FileSizeFormats2.png\" alt=\"\" border=\"0\" \/><\/strong><\/p>\n<h3>Tracking pure metrics<\/h3>\n<p>Sometimes you want to track metrics that aren\u2019t attached to any specific event. For example, I\u2019d like to know my processing queue length and CPU occupancy, which are rather independent of customer events.<\/p>\n<p>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:<\/p>\n<table style=\"background-color: #e0dedd\" border=\"1\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td valign=\"top\" width=\"623\">\n<p class=\"code\">\n          \/\/ Background thread:\n        <\/p>\n<p class=\"code\">\n          private void Run() {\n        <\/p>\n<p class=\"code\">\n          \u00a0var appInsights = new TelemetryClient();\n        <\/p>\n<p class=\"code\">\n          \u00a0while (true) {\n        <\/p>\n<p class=\"code\">\n          \u00a0 Thread.Sleep(60000);\n        <\/p>\n<p class=\"code\">\n          \u00a0 appInsights.TrackMetric(&#8220;ProcessingQueueLength&#8221;, queue.Length);\n        <\/p>\n<p class=\"code\">\n          \u00a0}\n        <\/p>\n<p class=\"code\">\n          }\n        <\/p>\n<p class=\"code\">\n          \u00a0\n        <\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>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<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2015\/02\/3007.CustomEventsMetrics.png\" alt=\"\" border=\"0\" \/><\/p>\n<h2>When to use TrackMetric() and TrackEvent()<\/h2>\n<p>Use TrackEvent when:<\/p>\n<ul>\n<li>Each specific event is important \u2013 you can view them in <a href=\"http:\/\/azure.microsoft.com\/documentation\/articles\/app-insights-diagnostic-search\/\">Diagnostic Search<\/a>.<\/li>\n<li>Metrics are measured with each event, so that you can segment the metrics by event type and properties of the event.<\/li>\n<li>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)<\/li>\n<\/ul>\n<div>\n<p>\n    Use TrackMetric when:\n  <\/p>\n<ul>\n<li>\n      The metric is independent of any specific event.\n    <\/li>\n<li>\n      You want to measure things on specific intervals (such as CPU or queue length every minute).\n    <\/li>\n<li>\n      You have a high volume of metrics and would like to collect and aggregate locally before sending it. \u00a0We will talk about aggregation in depth in a later blog post.\n    <\/li>\n<\/ul>\n<h2>\n    Summary\n  <\/h2>\n<p>\n    Custom metrics, events and properties give you <span>a 360-degree view of your application with\u00a0<\/span>\u00a0powerful insights into what users are doing with your application and how it is performing.\u00a0\n  <\/p>\n<p>\n    <a href=\"http:\/\/azure.microsoft.com\/documentation\/articles\/app-insights-get-started\/\">Get started with Application Insights<\/a> now if you have not already done so.\n  <\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Knowing how people use your application is vital to shaping your development priorities and architecture. To get that information, Visual Studio Application\u00a0Insights 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 [&hellip;]<\/p>\n","protected":false},"author":141,"featured_media":45953,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[224,1],"tags":[],"class_list":["post-8623","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-azure","category-devops"],"acf":[],"blog_post_summary":"<p>Knowing how people use your application is vital to shaping your development priorities and architecture. To get that information, Visual Studio Application\u00a0Insights 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 [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/posts\/8623","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/users\/141"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/comments?post=8623"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/posts\/8623\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/media\/45953"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/media?parent=8623"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/categories?post=8623"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/tags?post=8623"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}