user2083386
user2083386

Reputation: 135

Consume Azure Monitor Rest API from .Net Application

I am pretty new to Azure. Following is the task I am trying to achieve:

I wanted to consume Azure Monitor Rest API from .Net application using C# code and wanted to display few metrics(any couple of metrics) on the web page(I am using ASP.Net).

For this, I created Azure AD, got the subscription ID, Tenant ID, Client Secret and Client ID from Azure portal. All that I have to do is from .Net end, which is balance and I couldn't find proper resources to achieve this task.

Can someone help me out in this?

Upvotes: 3

Views: 2455

Answers (2)

Martin Smith
Martin Smith

Reputation: 453897

The Microsoft.Azure.Management.Monitor package is now deprecated with suggested alternative of Azure.ResourceManager.Monitor package.

I couldn't find any example code for that from a quick Google so adding some quick and dirty example here.

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


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


await foreach (var totalRequestUnitsMetricElement in GetTotalRequestUnitsMetrics(client))
{
    Console.WriteLine(totalRequestUnitsMetricElement);
}

async IAsyncEnumerable<TotalRequestUnitsMetricElement> GetTotalRequestUnitsMetrics(ArmClient armClient)
{
    //TODO: Parameterize the function instead of hardcoding these!
    ResourceIdentifier resourceIdentifier = 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 armClient.GetMonitorMetricsAsync(resourceIdentifier, 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;

            foreach (var metricValue in monitorTimeSeriesElement.Data)
            {
               yield return new TotalRequestUnitsMetricElement(metricValue.TimeStamp, metricValue.Total, capacityType, databaseName, collectionName, region);

            }
        }
    }
}

record TotalRequestUnitsMetricElement(DateTimeOffset TimeStamp, double? TotalRequestUnits, string CapacityType, string DatabaseName, string CollectionName, string Region);

Upvotes: 0

Alexander I.
Alexander I.

Reputation: 2714

  1. If you need to extract metric definitions (structure of azure monitor metrics) then you need to use MetricDefinitions web point. Detailed documentation is here.
  2. If you need to get monitoring metric values you need to use Metrics endpoint. Documentation link is here.

For both cases you need to use MonitorClient object from Microsoft.Azure.Management.Monitor nuget package.

You can review good examples how to extract metrics here(extract one-dimension metrics) and here(extract multi-dimension metrics).

Also follow up to couple helpful links:

Upvotes: 7

Related Questions