Azure / azure-sdk-for-net

This repository is for active development of the Azure SDK for .NET. For consumers of the SDK we recommend visiting our public developer docs at https://learn.microsoft.com/dotnet/azure/ or our versioned developer docs at https://azure.github.io/azure-sdk-for-net.
MIT License
5.25k stars 4.58k forks source link

MetricsQueryClient returns empty results #30903

Closed Ankit-MSFT closed 8 months ago

Ankit-MSFT commented 2 years ago

[Enter feedback here]

Hello, we tried to use Azure.Monitor.Query package to retrieve metrics data for our resources. We were seeing empty results in MetricResult.TimeSeries in the response, when using below code. No error.

string resourceId = "/subscriptions/SUB_ID/resourceGroups/RESOURCE_GROUP/providers/Microsoft.Storage/storageAccounts/STORAGE_ACCOUNT";
MetricsQueryClient metricsClient = new MetricsQueryClient(new DefaultAzureCredential()); 
var blobCount = (decimal)0.0;

var blobCountResult = metricsClient.QueryResource(resourceId, new[] { "BlobCount" },
  new MetricsQueryOptions
  {
      MetricNamespace = "Blob",
      Aggregations =
      {
          MetricAggregationType.Average
      },
      Granularity = TimeSpan.FromMinutes(10),
      TimeRange = new QueryTimeRange(TimeSpan.FromMinutes(60))
  });

After troubleshooting multiple times, capturing requests through Fiddler and comparing it with REST API we figured out the values we were passing in the code were not correct.

Correct usage:

string resourceId = "subscriptions/SUB_ID/resourceGroups/RESOURCE_GROUP/providers/Microsoft.Storage/storageAccounts/STORAGE_ACCOUNT/blobServices/default";
MetricsQueryClient metricsClient = new MetricsQueryClient(new DefaultAzureCredential()); 
var blobCount = (decimal)0.0;

var blobCountResult = metricsClient.QueryResource(resourceId, new[] { "BlobCount" },
  new MetricsQueryOptions
  {
      MetricNamespace = "Microsoft.Storage/storageAccounts/blobServices",
      Aggregations =
      {
          MetricAggregationType.Average
      },
      Granularity = TimeSpan.FromHours(1),
      TimeRange = new QueryTimeRange(TimeSpan.FromMinutes(60))
  });

We had referred to multiple documentation: Documentation reference for the package List of supported metrics & resource providers Monitoring Storage account

Only after we used Metrics List API and compared it to the request Azure.Monitor.Query package was sending (captured details through fiddler), we were able to understand we were passing the wrong the values.

Queries:

  1. Using 'BlobCount' as metric with 'Blob' as namespace should have thrown error. But we did not see any error instead we could see success response with empty result.

  2. Can we update the documentation of Azure.Monitor.Query package MetricsQueryClient to include some more examples so that we understand what values to use in the options.

Thanks.


Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

jsquire commented 2 years ago

Thank you for your feedback. Tagging and routing to the team member best able to assist.

nisha-bhatia commented 1 year ago

Hi @Ankit-MSFT, we will look into why the error is not propagated and update the documentation. Thank you for your feedback!