从.Net应用程序使用Azure Monitor Rest API

问题描述 投票:0回答:2

我对 Azure 还很陌生。以下是我想要实现的任务:

我想使用 C# 代码从 .Net 应用程序使用 Azure Monitor Rest API,并想在网页上显示一些指标(任意几个指标)(我正在使用 ASP.Net)。

为此,我创建了 Azure AD,从 Azure 门户获取了订阅 ID、租户 ID、客户端密钥和客户端 ID。我所要做的就是从.Net端开始,这是平衡性的,我找不到合适的资源来完成这个任务。

有人可以帮我吗?

c# .net rest azure monitor
2个回答
7
投票
  1. 如果您需要提取指标定义(Azure 监视器指标的结构),那么您需要使用
    MetricDefinitions
    Web 点。详细文档位于here
  2. 如果您需要获取监控指标值,则需要使用
    Metrics
    端点。文档链接位于此处

对于这两种情况,您都需要使用 Microsoft.Azure.Management.Monitor nuget 包中的 MonitorClient 对象。

您可以查看如何提取指标的好示例此处(提取一维指标)和此处(提取多维度指标)。

还可以关注几个有用的链接:


0
投票

Microsoft.Azure.Management.Monitor
软件包现已弃用,建议使用
Azure.ResourceManager.Monitor
软件包替代方案。

我无法从快速谷歌中找到任何示例代码,因此在这里添加一些快速而肮脏的示例。

using Azure.Core;
using Azure.Identity;
using Azure.ResourceManager;
using Azure.ResourceManager.Monitor;
using Azure.ResourceManager.Monitor.Models;


ArmClient client = new ArmClient(new DefaultAzureCredential());


ResourceIdentifier scope = new ("REDACTED RESOURCEID");

ArmResourceGetMonitorMetricsOptions options = new ArmResourceGetMonitorMetricsOptions()
{
    Aggregation = "total",
    Filter = "CapacityType eq '*' and DatabaseName eq '*' and Region eq '*' and CollectionName eq '*'",
    Orderby = "total desc",
    Top = 10000,
    Metricnames = "TotalRequestUnits",
    Timespan = "2024-04-26T16:00:00.000Z/2024-04-27T17:00:00.000Z",
    Interval = TimeSpan.FromHours(1)


};


await foreach (var metric in client.GetMonitorMetricsAsync(scope, options))
{
    foreach (var monitorTimeSeriesElement in metric.Timeseries)
    {
        //Not sure if I missed any inbuilt method to retrieve the metadata values more elegantly!
        var capacityType = monitorTimeSeriesElement.Metadatavalues.Single(md => md.Name.Value == "CapacityType".ToLowerInvariant()).Value;
        var databaseName = monitorTimeSeriesElement.Metadatavalues.Single(md => md.Name.Value == "DatabaseName".ToLowerInvariant()).Value;
        var collectionName = monitorTimeSeriesElement.Metadatavalues.Single(md => md.Name.Value == "CollectionName".ToLowerInvariant()).Value;
        var region = monitorTimeSeriesElement.Metadatavalues.Single(md => md.Name.Value == "Region".ToLowerInvariant()).Value;

        Console.WriteLine(databaseName + " -> " + collectionName + " -> " + region + " -> " + capacityType);

        foreach (var metricValue in monitorTimeSeriesElement.Data)
        {
            Console.WriteLine(metricValue.TimeStamp  + " -> " + metricValue.Total);
        }
    }
}
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.