Search code examples
azureazure-blob-storageazure-storageazure-monitorazure-metrics-advisor

Access Microsoft.Storage/storageAccounts/blobServices metrics from code


I want to retrieve metrics related to Blob metric namespace from Azure Storage Account. I need to read the BlobCount value. enter image description here

Initially I have tried like this:

            var usedCapacityResults = await metricsClient.QueryResourceAsync(resourceId, new[] { "BlobCount1" },
                new MetricsQueryOptions
                {
                    MetricNamespace = "Blob",
                    Aggregations =
                    {
                        MetricAggregationType.Average
                    },
                    Granularity = TimeSpan.FromMinutes(5),
                    TimeRange = new QueryTimeRange(TimeSpan.FromMinutes(10))
                });

            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;
                }
            }

But nothing gets returned.

Then I have tried to get the supported metric namespace, using this call:

GET https://management.azure.com/{resourceUri}/providers/microsoft.insights/metricNamespaces?api-version=2017-12-01-preview

and the only valid metric seems to be Microsoft.Storage/storageAccounts, that does not have the blob count metric.

Any idea how to read from code the BlobCount value?

There will be also the option to retrieve the list of containers and iterate though it to count the blobs, but this is something I want to avoid.


Solution

  • The working solution, with help from MS support:

    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;
            }