GithubHelp home page GithubHelp logo

isabella232 / monitor-dotnet-metrics-api Goto Github PK

View Code? Open in Web Editor NEW

This project forked from azure-samples/monitor-dotnet-metrics-api

0.0 0.0 0.0 14 KB

Code sample of the Azure Monitor metrics API using the .Net SDK

License: MIT License

C# 100.00%

monitor-dotnet-metrics-api's Introduction

page_type languages products description urlFragment
sample
csharp
azure
This sample explains how to retrieve Monitor metrics and metric definitions using the Azure .NET SDK releases 0.16.0-preview and 0.16.1-preview.
monitor-dotnet-metrics-api

Retrieve Azure Monitor metrics with .NET

This sample explains how to retrieve Monitor metrics and metric definitions using the Azure .NET SDK releases 0.16.0-preview and 0.16.1-preview.

NOTE: please refer to the Microsoft Open Source Code of Conduct.

NOTE: for contributions refer to the CONTRIBUTING.md file.

On this page

Run this sample

  1. If you don't have it, install the .NET Core SDK.

  2. Clone the repository.

    git clone https://github.com/Azure-Samples/monitor-dotnet-metrics-api.git
    
  3. Install the dependencies.

    dotnet restore
    
  4. Create an Azure service principal either through Azure CLI, PowerShell or the portal.

  5. Export these environment variables using your subscription id and the tenant id, client id and client secret from the service principal that you created.

    export AZURE_TENANT_ID={your tenant id}
    export AZURE_CLIENT_ID={your client id}
    export AZURE_CLIENT_SECRET={your client secret}
    export AZURE_SUBSCRIPTION_ID={your subscription id}
    
  6. Run the sample.

    dotnet run program
    

What is program.cs doing?

The sample retrieves metric definitions and metrics for a given resource. It starts by setting up a MonitorClient object using your subscription and credentials.

// Build the service credentials and Monitor client
var serviceCreds = await ApplicationTokenProvider.LoginSilentAsync(tenantId, clientId, secret);
var monitorClient = new MonitorClient(serviceCreds);
monitorClient.SubscriptionId = subscriptionId;

List metric definitions for a resource

List the metric definitions for the given resource, which is defined in the current subscription.

IEnumerable<MetricDefinition> metricDefinitions = await readOnlyClient.MetricDefinitions.ListAsync(resourceUri: resourceUri, cancellationToken: new CancellationToken());

or using a filter

var odataFilterMetricDef = new ODataQuery<MetricDefinition>("name.value eq 'CpuPercentage'");
metricDefinitions = await readOnlyClient.MetricDefinitions.ListAsync(resourceUri: resourceUri, odataQuery: odataFilterMetricDef, cancellationToken: new CancellationToken());

List metrics for a resource

IEnumerable<Metric> metrics = await readOnlyClient.Metrics.ListAsync(resourceUri: resourceUri, cancellationToken: CancellationToken.None);

or with a filter

// The comma-separated list of metric names must be present if a filter is used
var metricNames = "name.value eq 'CpuPercentage'"; // could be concatenated with " or name.value eq '<another name>'" ...

// Time grain is optional when metricNames is present
string timeGrain = " and timeGrain eq duration'PT5M'";

// Defaulting to 3 hours before the time of execution for these datetimes
string startDate = string.Format(" and startTime eq {0}", DateTime.Now.AddHours(-3).ToString("o"));
string endDate = string.Format(" and endTime eq {0}", DateTime.Now.ToString("o"));

var odataFilterMetrics = new ODataQuery<Metric>(
    string.Format(
        "{0}{1}{2}{3}",
        metricNames,
        timeGrain,
        startDate,
        endDate));

Write("Call with filter parameter (i.e. $filter = {0})", odataFilterMetrics);
metrics = await readOnlyClient.Metrics.ListAsync(resourceUri: resourceUri, odataQuery: odataFilterMetrics, cancellationToken: CancellationToken.None);

monitor-dotnet-metrics-api's People

Contributors

gucalder avatar microsoftopensource avatar msftgits avatar v-rajagt-zz avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.