Overview

Getting Started with OpenTelemetry .NET is a comprehensive guide designed to help developers install, configure, and use the OpenTelemetry .NET library for collecting and exporting distributed tracing and monitoring data. This library is part of the OpenTelemetry project, which is an open-source, cross-language observability framework.

Prerequisites

Before getting started, ensure you have the following prerequisites:

  1. .NET SDK: Install the latest version of the .NET SDK from Microsoft’s website.
  2. Git: Install Git to clone the repository and manage dependencies. You can download Git from Git’s official website.

Installing OpenTelemetry .NET

To install OpenTelemetry .NET, follow these steps:

Step 1: Clone the Repository

Clone the OpenTelemetry .NET repository using Git:

git clone https://github.com/open-telemetry/opentelemetry-dotnet.git
          

Step 2: Install NuGet Packages

Navigate to the cloned repository and install the required NuGet packages using the following command:

cd opentelemetry-dotnet
          dotnet add package OpenTelemetry.API
          dotnet add package OpenTelemetry.Instrumentation.AspNetCore
          dotnet add package OpenTelemetry.Exporter.Jaeger
          

Step 3: Configure the SDK

Create a new appsettings.json file in the src/<YourProjectName>/ directory and add the following configuration:

{
            "OpenTelemetry": {
              "Exporter": {
                "Jaeger": {
                  "AgentHostPort": "localhost:6831"
                }
              }
            }
          }
          

Update the Program.cs file to load the configuration:

using OpenTelemetry;
          using OpenTelemetry.API;
          using OpenTelemetry.Instrumentation.AspNetCore;
          using OpenTelemetry.Exporter.Jaeger;
          using Microsoft.Extensions.DependencyInjection;
          
          public static IHostBuilder CreateHostBuilder(string[] args) =>
              Host.CreateDefaultBuilder(args)
                  .ConfigureWebHostDefaults(webBuilder =>
                  {
                      webBuilder.UseStartup<Startup>();
                      webBuilder.UseOpenTelemetry();
                  })
                  .ConfigureServices(services =>
                  {
                      services.AddOpenTelemetryTracing(tracingOptions =>
                      {
                          tracingOptions.AddJaegerExporter(exporterOptions =>
                          {
                              exporterOptions.AgentHostPort = Configuration["OpenTelemetry:Exporter:Jaeger:AgentHostPort"]!;
                          });
                      });
                  });
          

Using OpenTelemetry .NET

After installing and configuring OpenTelemetry .NET, you can start using it in your application. For more information on using OpenTelemetry .NET, refer to the official documentation.

Conclusion

Getting Started with OpenTelemetry .NET is an essential guide for developers looking to implement distributed tracing and monitoring in their .NET applications using the OpenTelemetry framework. By following the steps outlined in this guide, you’ll be able to install, configure, and use OpenTelemetry .NET to collect and export valuable observability data.

OpenTelemetry .NET GitHub Repository