Search code examples
c#azureazure-application-insightsazure-rest-api

How to filter AppInsights custom metrics with REST API


I am trying to use REST API for retrieving AppInsights custom metrics and use a filter in the query.

The code to create the metric:

var telemetry = new MetricTelemetry();
telemetry.Context.InstrumentationKey = instrumentationKey;
telemetry.Name = FacebookFupMetricName;
telemetry.Value = amount;
telemetry.Timestamp = DateTime.Now;
telemetry.Properties["agencyCode"] = agencyCode;
telemetry.Properties["accountCode"] = accountCode;
telemetry.Properties["category"] = category;
telemetry.Properties["action"] = action;
var client = new TelemetryClient();
client.TrackMetric(telemetry);
client.Flush();

The metric is created in Azure when run this code.

Now I would like to use REST API for retrieving metrics and use a filter in the query. When I use a filter

category eq 'cat001'

the query ends with an error

The following dimensions are not valid in the filter clause for this metric: category

The query url is:

https://dev.applicationinsights.io/apiexplorer/metrics?appId=<appId>&apiKey=<apiKey>&metricId=customMetrics%2FFacebookFupAction&timespan=P1D&aggregation=sum&filter=category%20eq%20%27cat001%27

The question is, is it possible to filter custom metrics with dimensions?


Solution

  • In the filter field, you should use customDimensions/category eq 'cat001'.

    The generated url is in this format:

    `https://dev.applicationinsights.io/apiexplorer/metrics?appId=<appId>&apiKey=<apiKey>&metricId=customMetrics%2FFacebookFupMetricName&aggregation=sum&filter=customDimensions%2Fcategory%20eq%20'cat001'`
    

    The screenshot as below:

    enter image description here