Export to Jaeger
This guide will show you how to export OpenTelemetry .NET traces to Jaeger for visualization and analysis.
Prerequisites
- .NET SDK installed on your computer
- Jaeger downloaded (this guide covers installation)
- Familiarity with basic OpenTelemetry concepts (see Getting Started with Console)
Creating a .NET application with OTLP export
Create a new console application:
dotnet new console --output getting-started-jaeger
cd getting-started-jaeger
Install the required OpenTelemetry packages:
dotnet add package OpenTelemetry.Exporter.Console
dotnet add package OpenTelemetry.Exporter.OpenTelemetryProtocol
dotnet add package OpenTelemetry.Instrumentation.Http
Update the Program.cs
file with the following code:
using System.Diagnostics;
using OpenTelemetry;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;
namespace GettingStartedJaeger;
internal static class Program
{
private static readonly ActivitySource MyActivitySource = new("OpenTelemetry.Demo.Jaeger");
public static async Task Main()
{
using var tracerProvider = Sdk.CreateTracerProviderBuilder()
.SetResourceBuilder(ResourceBuilder.CreateDefault().AddService(
serviceName: "DemoApp",
serviceVersion: "1.0.0"))
.AddSource("OpenTelemetry.Demo.Jaeger")
.AddHttpClientInstrumentation()
.AddConsoleExporter()
.AddOtlpExporter()
.Build();
using var parent = MyActivitySource.StartActivity("JaegerDemo");
using (var client = new HttpClient())
{
using (var slow = MyActivitySource.StartActivity("SomethingSlow"))
{
await client.GetStringAsync(new Uri("https://httpstat.us/200?sleep=1000")).ConfigureAwait(false);
await client.GetStringAsync(new Uri("https://httpstat.us/200?sleep=1000")).ConfigureAwait(false);
}
using (var fast = MyActivitySource.StartActivity("SomethingFast"))
{
await client.GetStringAsync(new Uri("https://httpstat.us/301")).ConfigureAwait(false);
}
}
}
}
When you run this application, it will output traces to the console through the
ConsoleExporter
and also attempt to send traces to Jaeger using the
OtlpExporter
. Since Jaeger isn’t set up yet, those traces will initially be
dropped.
Setting up Jaeger
Jaeger is an open source distributed tracing system that helps monitor and troubleshoot microservices-based applications.
Installing and running Jaeger
- Download Jaeger from the official download page.
- Extract it to a location on your machine.
- Run the Jaeger all-in-one executable with OTLP enabled:
./jaeger-all-in-one --collector.otlp.enabled
This starts:
- Jaeger UI (
http://localhost:16686
) - Jaeger collector with OTLP receiver (
http://localhost:4317
) - Jaeger query service and other components
Viewing traces in Jaeger
- Open a web browser and navigate to http://localhost:16686
- Run your .NET application
- In the Jaeger UI:
- Select “DemoApp” from the “Service” dropdown
- Click “Find Traces”
You should see your application’s traces in the Jaeger UI. Click on a trace to see the detailed Gantt chart view of all spans in the trace.
Understanding the code
Trace provider configuration
The application configures OpenTelemetry with:
using var tracerProvider = Sdk.CreateTracerProviderBuilder()
.SetResourceBuilder(ResourceBuilder.CreateDefault().AddService(
serviceName: "DemoApp",
serviceVersion: "1.0.0"))
.AddSource("OpenTelemetry.Demo.Jaeger")
.AddHttpClientInstrumentation()
.AddConsoleExporter()
.AddOtlpExporter()
.Build();
This code:
- Sets up a resource with service name and version
- Registers our activity source
- Adds automatic instrumentation for HttpClient
- Configures console and OTLP exporters
Activity creation
The application creates spans using the ActivitySource:
private static readonly ActivitySource MyActivitySource = new("OpenTelemetry.Demo.Jaeger");
// Create a parent span
using var parent = MyActivitySource.StartActivity("JaegerDemo");
// Create child spans
using (var slow = MyActivitySource.StartActivity("SomethingSlow"))
{
// Operations inside this block will be part of the "SomethingSlow" span
}
Trace export flow
The trace data flows through the following components:
- Application creates spans using ActivitySource
- TracerProvider collects and processes spans
- OTLP Exporter sends spans to Jaeger through the OTLP protocol
- Jaeger stores and allows you to query and visualize the traces
Production usage
For production use, you should remove the Console Exporter and only use the OTLP Exporter:
using var tracerProvider = Sdk.CreateTracerProviderBuilder()
.SetResourceBuilder(ResourceBuilder.CreateDefault().AddService(
serviceName: "DemoApp",
serviceVersion: "1.0.0"))
.AddSource("OpenTelemetry.Demo.Jaeger")
.AddHttpClientInstrumentation()
// Remove Console Exporter
// .AddConsoleExporter()
.AddOtlpExporter()
.Build();
You can also remove the Console Exporter package:
dotnet remove package OpenTelemetry.Exporter.Console
Learn more
- Jaeger Tracing
- OTLP Exporter for OpenTelemetry .NET
- OpenTelemetry Tracing Specification
フィードバック
このページは役に立ちましたか?
Thank you. Your feedback is appreciated!
Please let us know how we can improve this page. Your feedback is appreciated!