In this guide, we will walk you through the process of setting up and using OpenTelemetry in .NET. You will learn how to instrument a simple .Net. application to produce traces, metrics, and logs and export them to KloudMate.
Before diving into OpenTelemetry, be sure that you have the following installed:
Step 2: Example Application
If you already have a .NET application and don't need to create one from scratch, you can directly jump to the Instrumentation part of this guide.
For this tutorial, we will be using a basic Minimal API with ASP.NET Core application. However, OpenTelemetry .Net is compatible with other web frameworks as well. Feel free to adapt the instructions to your preferred framework.
Set up an environment in a new directory called dotnet-simple. Run the following command within this directory.
2. Within the same directory, replace the content of Program.cswith the following code
using System.Globalization;
using Microsoft.AspNetCore.Mvc;
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
string HandleRollDice([FromServices]ILogger<Program> logger, string? player)
{
var result = RollDice();
if (string.IsNullOrEmpty(player))
{
logger.LogInformation("Anonymous player is rolling the dice: {result}", result);
}
else
{
logger.LogInformation("{player} is rolling the dice: {result}", player, result);
}
return result.ToString(CultureInfo.InvariantCulture);
}
int RollDice()
{
return Random.Shared.Next(1, 7);
}
app.MapGet("/rolldice/{player?}", HandleRollDice);
app.Run();
3. In the Properties subdirectory, replace the content of launchSettings.json with the following:
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "http://localhost:8080",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
To install the instrumentation packages, we will use the NuGet packages from OpenTelemetry. These packages will automatically generates telemetry data.
1. Add the packages
dotnet add package OpenTelemetry.Extensions.Hosting
dotnet add package OpenTelemetry.Instrumentation.AspNetCore --prerelease
2. In Program.cs, replace the following lines:
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
using OpenTelemetry.Logs;
using OpenTelemetry.Metrics;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;
var builder = WebApplication.CreateBuilder(args);
const string serviceName = "roll-dice";
builder.Logging.AddOpenTelemetry(options =>
{
options
.SetResourceBuilder(
ResourceBuilder.CreateDefault()
.AddService(serviceName))
.AddOtlpExporter(otlpOptions =>
{
otlpOptions.Endpoint = new Uri("https://otel.kloudmate.com:4318");
otlpOptions.Protocol = OtlpExportProtocol.HttpProtoBuf;
string headerKey = "Authorization";
string headerValue = "<KM_PRIVATE_KEY>";
string formattedHeader = $"{headerKey}={headerValue}";
otlpOptions.Headers = formattedHeader;
}));
});
builder.Services.AddOpenTelemetry()
.ConfigureResource(resource => resource.AddService(serviceName))
.WithTracing(tracing => tracing
.AddAspNetCoreInstrumentation()
.AddOtlpExporter(otlpOptions =>
{
otlpOptions.Endpoint = new Uri("https://otel.kloudmate.com:4318");
otlpOptions.Protocol = OtlpExportProtocol.HttpProtoBuf;
string headerKey = "Authorization";
string headerValue = "<KM_PRIVATE_KEY>";
string formattedHeader = $"{headerKey}={headerValue}";
otlpOptions.Headers = formattedHeader;
}));
.WithMetrics(metrics => metrics
.AddAspNetCoreInstrumentation()
.AddOtlpExporter(otlpOptions =>
{
otlpOptions.Endpoint = new Uri("https://otel.kloudmate.com:4318");
otlpOptions.Protocol = OtlpExportProtocol.HttpProtoBuf;
string headerKey = "Authorization";
string headerValue = "<KM_PRIVATE_KEY>";
string formattedHeader = $"{headerKey}={headerValue}";
otlpOptions.Headers = formattedHeader;
}));
var app = builder.Build();
Step 5: Run the Instrumented App
1. Run the instrumented application
Note the output from the dotnet run.
2. From another terminal send a request using curl:
curl localhost:8080/rolldice