Exporting OpenTelemetry Traces to Azure Monitor utilizing ASP.NET Core

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

On this article, you’ll learn to export {custom} OpenTelemetry traces out of your ASP.NET Core functions to Azure Monitor.

Listed below are the steps to observe alongside.

  • Substitute the contents of the Program.cs file with the next code.
    utilizing System.Diagnostics;
    utilizing Azure.Monitor.OpenTelemetry.AspNetCore;
    utilizing OpenTelemetry.Hint;
    var activitySource = new ActivitySource("OtelDemo.App", "1.0.0");
    var builder = WebApplication.CreateBuilder(args);
    // Configure the OpenTelemetry tracer supplier so as to add a supply named "OtelDemo.App". It will be sure that all actions created by the exercise supply are traced.
    builder.Companies.ConfigureOpenTelemetryTracerProvider((sp, tracerProviderBuilder) => tracerProviderBuilder.AddSource("OtelDemo.App"));
    // Add the Azure Monitor telemetry service to the applying.
    // This service will accumulate and ship telemetry information to Azure Monitor.
    // Construct the ASP.NET Core net utility.
    var app = builder.Construct();
    app.MapGet("/traces-demo", () =>
        // Create a brand new exercise named "dad or mum". This might be recorded as a span.
        utilizing var exercise = activitySource.StartActivity("dad or mum");
        // Create a brand new exercise named "baby" that could be a baby of the "dad or mum" exercise. This might be recorded as a baby span.
        utilizing var childActivity = activitySource.StartActivity("baby");
        // Set a {custom} tag on the "baby" exercise. This may be any metadata that you just need to affiliate with the span.
        childActivity?.SetTag("custom-tag", "custom-value");
        return "Customized spans recorded";
    // Run the applying.

Let’s undergo the code to know the way it works.

Firstly, an ActivitySource occasion is outlined. That is used to start out new actions that may be traced. The parameters are the identify of the supply and its model. The .NET instrumentation of OpenTelemetry makes use of the prevailing constructs of the System. Diagnostics API to be OpenTelemetry compliant. Therefore, the .NET implementation of OpenTelemetry appears completely different from different language implementations on account of this reuse. You possibly can learn extra in regards to the finer particulars of .NET instrumentation for OpenTelemetry right here.

Subsequent, a tracer supplier is added to handle the lifecycle of tracers. Tracers are used to create spans, which signify items of labor inside a hint. A hint is a set of spans associated to one another, sometimes as a result of they signify a single operation and its sub-operations. The tracer supplier ensures that tracers are accurately initialized and disposed of. It additionally supplies configuration choices for the tracers, similar to setting the default sampler or including span processors.

Within the pattern utility, the tracer supplier is configured so as to add a supply named “OtelDemo.App”. Because of this it can create tracers that may start new spans for actions created by the ActivitySource with that identify. These spans will then be collected and exported by the OpenTelemetry SDK, which on this case is configured to ship them to Azure Monitor.

Subsequent, the Azure Monitor telemetry service is added to the applying. This service collects and sends telemetry information to Azure Monitor.

Lastly, to generate traces, we created an endpoint at “/traces-demo”. When this endpoint is hit, it begins a dad or mum exercise and a baby exercise, units a {custom} tag on the kid exercise, after which returns a string response. Since every exercise corresponds to a span, this endpoint will generate two spans: a dad or mum span and a baby span with a {custom} tag.

We’re able to debug our utility now, however earlier than that, now we have to configure the connection string for the Utility Insights useful resource that we created earlier. To perform this, set the APPLICATIONINSIGHTS_CONNECTION_STRING surroundings variable worth to the connection string that you just copied beforehand. You are able to do this by operating the next command within the terminal:



Debug the applying and invoke the endpoint at /traces-demo. Each profitable invocation of the endpoint will generate a hint with two spans a dad or mum span and a baby span. These spans might be exported to Azure Monitor.

To view the exported traces, go to the Azure Portal and navigate to the Utility Insights useful resource that you just created earlier. Subsequent, click on on the Transaction search tab after which on the request displayed within the checklist to view the main points of the hint.

The next screenshot reveals the main points of the hint and the spans related to it. You possibly can click on on the spans to view their particulars. You’ll discover that the kid span comprises the {custom} tag that we set earlier.


To discover the combination between Azure Monitor and OpenTelemetry additional, together with the varied forms of telemetry that may be exported, please consult with the official documentation.

Know extra about our firm at Skrots. Know extra about our companies 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