Azure

Exporting Customized OpenTelemetry Metrics to Azure Monitor utilizing ASP.NET Core

OpenTelemetry Metrics

OpenTelemetry is an observability framework that gives APIs, libraries, brokers, and instrumentation to gather distributed traces and metrics from an software. It is an open-source, CNCF undertaking that goals to standardize the era and assortment of telemetry information (metrics, logs, and traces) for cloud-native software program.

On this article, you’ll learn to export customized OpenTelemetry metrics out of your ASP.NET Core purposes to Azure Monitor.

Listed here are the steps to observe alongside.

  • Exchange the contents of the Program.cs file with the next code.
    utilizing System.Diagnostics.Metrics;
    utilizing Azure.Monitor.OpenTelemetry.AspNetCore;
    utilizing Azure.Monitor.OpenTelemetry.Exporter;
    utilizing OpenTelemetry.Metrics;
    
    var builder = WebApplication.CreateBuilder(args);
    
    // Configure the OpenTelemetry meter supplier so as to add a meter named "OTel.AzureMonitor.Demo".
    builder.Companies.ConfigureOpenTelemetryMeterProvider((sp, builder) =>
        builder.AddMeter("OTel.AzureMonitor.Demo"));
    
    // Add the Azure Monitor telemetry service to the appliance.
    // This service will gather and ship telemetry information to Azure Monitor.
    builder.Companies.AddOpenTelemetry().UseAzureMonitor();
    
    // Construct the ASP.NET Core net software.
    var app = builder.Construct();
    
    // Create a brand new meter named "OTel.AzureMonitor.Demo".
    var meter = new Meter("OTel.AzureMonitor.Demo");
    
    // Create a brand new counter metric named "MyDiceCounter".
    var myDiceCounter = meter.CreateCounter<lengthy>("MyDiceCounter");
    
    app.MapGet("/metrics-demo", () =>
    {
        var gameId = Guid.NewGuid().ToString();
        for (var i = 0; i < 5; i++)
        {
            var diceValue = new Random().Subsequent(1, 6);
            myDiceCounter.Add(1, new("faceValue", diceValue), new("gameId", gameId));
        }
    
        return "Counter incremented";
    });
    
    // Run the appliance.
    app.Run();
    

The above code snippet demonstrates tips on how to arrange an ASP.NET Core net software with OpenTelemetry and Azure Monitor integration for the aim of amassing and exporting telemetry information. It configures the OpenTelemetry meter supplier so as to add a meter named OTel.AzureMonitor.Demo. Moreover, it provides the Azure Monitor telemetry service to the appliance, which is answerable for amassing and sending telemetry information to Azure Monitor.

Following the setup, this system creates a brand new meter named OTel.AzureMonitor.Demo. Then, it creates a brand new counter metric named MyDiceCounter from the meter.

To clarify the utilization of the metric, let’s take into account a state of affairs the place we need to play a sport the place we rely the variety of instances a cube worth is generated. On this case, when the endpoint at /metrics-demo is invoked, this system increments the MyDiceCounter counter by 1. The worth of the cube roll is randomly generated 5 instances between 1 and 6 and is saved as a tag for the metric worth. Moreover, the distinctive sport ID can also be recorded with the metric worth as a tag. Lastly, the endpoint returns the message “Counter incremented” as the results of the operation.

Earlier than continuing additional with debugging the appliance, it is advisable to configure the connection string for the Software Insights useful resource that you just created earlier. To take action, set the APPLICATIONINSIGHTS_CONNECTION_STRING surroundings variable to the connection string. You are able to do this by working the next command within the terminal.

APPLICATIONINSIGHTS_CONNECTION_STRING=<connection-string>

Output

Debug the appliance and invoke the endpoint at /metrics-demo just a few instances. Each profitable invocation of the endpoint will increment the metric for the counter by 1. Each measurement recorded might be exported to Azure Monitor the place it is going to be summed by tags which can be related to the metric.

To view the exported metrics, go to the Azure Portal and navigate to the Software Insights useful resource that you just created earlier. Click on on the Logs tab and run the next question.

customMetrics   
| undertaking timestamp, worth, faceValue = tostring(customDimensions["faceValue"]), gameId = tostring(customDimensions["gameId"])

The next screenshot exhibits the output of the question.

Let’s break down the question to know the output. The question first selects the customized metrics desk, which comprises the customized metrics that had been exported to Azure Monitor. Then, it initiatives the timestamp, worth, faceValue, and gameId columns from the desk. The face worth and gameId columns are extracted from the customDimensions column, which comprises the tags related to the metric.

Now you can plot the outcomes on a chart by clicking on the Chart button. The next screenshot exhibits the chart for the question output.

 Chart button

To discover the mixing between Azure Monitor and OpenTelemetry additional, together with the varied sorts of metrics that may be exported, please consult with the official documentation.

Know extra about our firm at Skrots. Know extra about our providers at Skrots Companies, Additionally checkout all different blogs at Weblog at Skrots

Show More

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button