Introduction
OpenTelemetry is a powerful tool that helps you collect and monitor telemetry data, including traces, logs, and metrics, from your .NET application. In this guide, we’ll show you how to integrate OpenTelemetry into a .NET app using the RollDiceApp example from above.

Step 1: Install OpenTelemetry Packages

First, we need to install the necessary NuGet packages for OpenTelemetry instrumentation in .NET. These packages help track requests, generate logs, and capture metrics for your application.

Run the following commands in your RollDiceApp directory:

sudo dotnet add package OpenTelemetry.Extensions.Hosting
sudo dotnet add package OpenTelemetry.Instrumentation.AspNetCore
sudo dotnet add package OpenTelemetry.Exporter.Console
  • OpenTelemetry.Extensions.Hosting: Provides integration with .NET hosting for easier setup.
  • OpenTelemetry.Instrumentation.AspNetCore: Automatically instruments incoming requests for ASP.NET Core applications.
  • OpenTelemetry.Exporter.Console: Exports collected telemetry data to the console for easy debugging.

Step 2: Modify the Program.cs for OpenTelemetry

Next, you need to modify your Program.cs file to set up OpenTelemetry for logging, tracing, and metrics. Here’s how to do that:

  1. Add OpenTelemetry using statements at the top of the file:
using OpenTelemetry.Logs;
using OpenTelemetry.Metrics;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;
  1. Replace the lines:
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

with the following code:

var builder = WebApplication.CreateBuilder(args);

const string serviceName = "roll-dice";

// Configure OpenTelemetry logging, tracing, and metrics
builder.Logging.AddOpenTelemetry(options =>
{
    options
        .SetResourceBuilder(
            ResourceBuilder.CreateDefault()
                .AddService(serviceName))
        .AddConsoleExporter();
});

builder.Services.AddOpenTelemetry()
    .ConfigureResource(resource => resource.AddService(serviceName))
    .WithTracing(tracing => tracing
        .AddAspNetCoreInstrumentation()
        .AddConsoleExporter())
    .WithMetrics(metrics => metrics
        .AddAspNetCoreInstrumentation()
        .AddConsoleExporter());

var app = builder.Build();

In this configuration:

  • Service Name: We define the service name as “roll-dice”, so all telemetry data is grouped under this label.
  • Tracing: It captures requests and generates traces for each HTTP call.
  • Logging: Console logging is set up with OpenTelemetry to track log data.
  • Metrics: Metrics like request duration, success/failure rate, and more are captured and outputted to the console.

Step 3: Build and Run the Application

Now that OpenTelemetry is integrated, let’s build and run the application:

  1. Build the app:
sudo dotnet build
  1. Run the app:
sudo dotnet run

Step 4: Test the Application and View Telemetry

To test the telemetry, send requests to the /rolldice endpoint using a browser or curl. For example:

curl localhost:8080/rolldice

After about 30 seconds, stop the server by pressing Ctrl + C. You will see logs, traces, and metrics output to your console, which will include data such as the service name, dice roll results, and player information.

Here’s an example of what you might see in your console:

Resource associated with LogRecord:
  service.name: roll-dice
  telemetry.sdk.name: opentelemetry
  telemetry.sdk.language: dotnet
  telemetry.sdk.version: 1.9.0

You can analyze this data to get insights into your application’s performance and behavior.

Conclusion

By following these steps, you have successfully integrated OpenTelemetry into your .NET application. Now, you can monitor the health of your application with traces, logs, and metrics using OpenTelemetry, giving you greater visibility into how your app performs in production. As next steps, consider exporting data to other backends like Jaeger, Prometheus, or Azure Monitor for advanced monitoring.

Integrating OpenTelemetry enhances your ability to monitor and debug .NET applications, providing crucial insights into performance and reliability.

To know what is .NET please click on “.NET

For any queries contact us: Rushi-Infotech

Similar Posts

Leave a Reply

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