azure - 从代码访问 Microsoft.Storage/storageAccounts/blobServices 指标

标签 azure azure-blob-storage azure-storage azure-monitor azure-metrics-advisor

我想从 Azure 存储帐户检索与 Blob 指标命名空间相关的指标。我需要读取 BlobCount 值。 enter image description here

最初我尝试过这样的:

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

但是什么也没有返回。

然后我尝试使用此调用获取支持的指标 namespace :

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

唯一有效的指标似乎是 Microsoft.Storage/storageAccounts,它没有 blob 计数指标。

知道如何从代码中读取 BlobCount 值吗?

还有一个选项来检索容器列表并迭代它以计算 blob,但这是我想避免的事情。

最佳答案

在微软支持人员的帮助下,可行的解决方案:

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

关于azure - 从代码访问 Microsoft.Storage/storageAccounts/blobServices 指标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73235051/

相关文章:

python - Azure 网站 Kudu REST API - Python 中的身份验证

Azure Functions blob 触发器比 WebJobs blob 触发器更可靠?

java - Azure AD 授权的 Blob 存储 java sdk

azure - 使用 Azure Functions 和 Azure 存储处理长时间运行的任务

Azure DF 参数化

debugging - 当我在 VS2012 中调试时,如何让 Azure 模拟器启动特定的主机 header 定义的站点?

azure - 仅用于 Windows Azure 上的 Blob 存储的小型库?

azure - 用于白名单的 Azure 存储 blob IP 范围是多少

java - 使用java的Azure文件图像路径

仅当部署在服务器上而非本地主机上时出现 Azure 异常