Setting Up the Development Environment for OpenTelemetry in .NET
Getting Started
The OpenTelemetry .NET SDK supports various configuration options to instrument applications effectively. For the development environment, setting configurations primarily involves initializing the SDK and specifying exporters and processors. Below is a detailed step-by-step guide on configuring the OpenTelemetry SDK in your .NET application.
Prerequisites
Ensure that the necessary packages are installed in your .NET application. Use the following commands in your terminal:
dotnet add package OpenTelemetry
dotnet add package OpenTelemetry.Exporter.Zipkin
dotnet add package OpenTelemetry.Extensions.Hosting
Step 1: Configure OpenTelemetry in Program.cs
In your Program.cs
file (or equivalent for a non-ASP.NET Core application), you can set up OpenTelemetry with environment-specific configurations.
Here’s a sample configuration for a web API project that integrates Zipkin as the tracing exporter:
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
using OpenTelemetry;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
})
.ConfigureServices(services =>
{
services.AddOpenTelemetryTracing(builder =>
{
builder
.SetResourceBuilder(ResourceBuilder.CreateDefault().AddService("YourServiceName"))
.AddAspNetCoreInstrumentation()
.AddHttpClientInstrumentation()
.AddZipkinExporter(options =>
{
options.Endpoint = new Uri("http://zipkin:9411/api/v2/spans");
});
});
});
}
Step 2: Environment Variables
For the example provided, environment variables must be set correctly in your docker-compose.yml
. This helps the services communicate seamlessly:
webapi:
environment:
- ASPNETCORE_ENVIRONMENT=Development
- RABBITMQ_HOSTNAME=rabbitmq
- ZIPKIN_HOSTNAME=zipkin
Step 3: Configuration of RabbitMQ
In your WorkerService
, similar to the Web API configuration, ensure RabbitMQ settings are configured through the environment variables:
workerservice:
environment:
- DOTNET_ENVIRONMENT=Development
- RABBITMQ_HOSTNAME=rabbitmq
Use these settings to instantiate your RabbitMQ communications within your application, ensuring that the settings are communicated accurately.
Step 4: Adjusting Logging and Tracing Levels
For improved monitoring, it can be beneficial to adjust the verbosity of logs and traces in development. This can be done in the appsettings.Development.json
or at runtime depending on your configuration approach.
Example appsettings.Development.json
configuration:
{
"Logging": {
"LogLevel": {
"Default": "Debug",
"Microsoft.AspNetCore": "Warning"
}
}
}
Step 5: Building and Running with Docker
When building and running your application via Docker, ensure that the docker-compose.yml
reflects the necessary settings, which are already provided in the example above.
Run the following commands to build and start the containers:
docker-compose build
docker-compose up
Conclusion
This setup provides a robust configuration for the development environment using the OpenTelemetry .NET SDK. Ensure that you monitor the deployments using Zipkin for effective trace management. Adjust configurations as necessary based on the specific requirements of your application environment.
Source: Configuration details are based on standard practices for integrating OpenTelemetry with .NET applications. Notable configuration patterns include resource builders and exporter definitions in addition to using Docker for service orchestration.