Use MetricsQueryClient to retrieve BlobCount for an Azure Storage Account

Angela Calborean 71 Reputation points
2022-08-04T08:59:01.47+00:00

Hello,
I have a requirement to save and report some metric values related to Azure Storage Accounts used in my application. I am interested in Used capacity and Blob count.

I was able to get the used capacity for a storage account using the MetricsQueryClient, but at different runs, the TimeSeries is null (sometimes it gets populates, sometimes is does not). Why is that?

This code works for UsedCapacity. How can I change it to obtain the blob count value?

    var resourceGroupName = "...";  
    var subscriptionID = "..";  
    var storageAccountName = "...";  
    var resourceId = "/subscriptions/" + subscriptionID + "/resourceGroups/" + resourceGroupName + "/providers/Microsoft.Storage/storageAccounts/" + storageAccountName;  
     
    var metricsClient = new MetricsQueryClient(new DefaultAzureCredential());  
    Response<MetricsQueryResult> results = await metricsClient.QueryResourceAsync(resourceId, new[] { "UsedCapacity" });  

I have tried to specify the MetricNamespace, but although the GetRawResponse().Status is 200, nothing is populated.

      var usedCapacityResults = await metricsClient.QueryResourceAsync(resourceId, new[] { "BlobCount" },  
            new MetricsQueryOptions { MetricNamespace = "Blob" });  

        if (usedCapacityResults.GetRawResponse().Status == StatusCodes.Status200OK)  
        {  
            var usedCapacityMetric = usedCapacityResults.Value.Metrics.FirstOrDefault(m => m.Name == "BlobCount" && m.Error == null);  
            var metricValue = usedCapacityMetric?.TimeSeries.FirstOrDefault();  
            if (metricValue != null && !metricValue.Values.IsNullOrEmpty())  
            {  
                var average = metricValue.Values[0].Average;  
                if (average != null) blobCount = (decimal)average;  
            }  
        }  
Azure Storage Accounts
Azure Storage Accounts
Globally unique resources that provide access to data management services and serve as the parent namespace for the services.
2,721 questions
Azure Blob Storage
Azure Blob Storage
An Azure service that stores unstructured data in the cloud as blobs.
2,449 questions
Azure AI Metrics Advisor
Azure AI Metrics Advisor
An Azure artificial intelligence analytics service that proactively monitors metrics and diagnoses issues.
80 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Angela Calborean 71 Reputation points
    2022-09-09T08:11:49.57+00:00

    This is the solution that was provided to me by MS Support team:

            private async Task<decimal> GetStorageAccountBlobCount(MetricsQueryClient metricsClient, string resourceId)  
            {  
                var blobCount = (decimal)0.0;  
                try  
                {  
                    resourceId = $"{resourceId}/blobServices/default";  
                    var blobCountResult = await metricsClient.QueryResourceAsync(resourceId, new[] { "BlobCount" },  
                        new MetricsQueryOptions  
                        {  
                            MetricNamespace = "Microsoft.Storage/storageAccounts/blobServices",  
                            Aggregations =  
                            {  
                                MetricAggregationType.Average  
                            },  
                            Granularity = TimeSpan.FromHours(1),  
                            TimeRange = new QueryTimeRange(TimeSpan.FromMinutes(60))  
                        });  
      
                    if (blobCountResult.GetRawResponse().Status == StatusCodes.Status200OK)  
                    {  
                        var blobCountMetric = blobCountResult.Value.Metrics.FirstOrDefault(m => m.Name == "BlobCount" && m.Error == null);  
                        var metricValue = blobCountMetric?.TimeSeries.FirstOrDefault();  
                        if (metricValue != null && !metricValue.Values.IsNullOrEmpty())  
                        {  
                            var average = metricValue.Values[0].Average;  
                            if (average != null) blobCount = (decimal)average;  
                        }  
                    }  
                }  
                catch (Exception ex)  
                {  
                    _logger.LogError($"Error on calculate blob count for {resourceId}", ex);  
                }  
      
                return blobCount;  
            }  
    
    1 person found this answer helpful.
    0 comments No comments