A sample .NET Web API application demonstrating distributed tracing with OpenTelemetry. The application implements a simple order processing system where you can create and retrieve orders, with all operations being traced and monitored with OpenObserve.
This demo implements two main endpoints:
POST /order
- Create a new orderGET /order/{id}
- Retrieve an order by ID
Each endpoint is instrumented with OpenTelemetry to capture:
- HTTP request details
- Order processing operations
- External service calls
- Error scenarios
- .NET SDK 6.0 or newer
- OpenObserve: You can get started with OpenObserve Cloud or a self hosted installation.
- Clone the Repository:
git clone https://github.com/openobserve/dotnet-opentelemetry-tracing-application
cd dotnet-opentelemetry-tracing-application
- Add Required Packages:
# Add core OpenTelemetry packages
dotnet add package OpenTelemetry --version 1.7.0
dotnet add package OpenTelemetry.Extensions.Hosting --version 1.7.0
dotnet add package OpenTelemetry.Instrumentation.AspNetCore --version 1.7.0
dotnet add package OpenTelemetry.Exporter.OpenTelemetryProtocol --version 1.7.0
dotnet add package OpenTelemetry.Instrumentation.Http --version 1.7.0
dotnet add package OpenTelemetry.Exporter.Console --version 1.7.0
# Add Swagger for API documentation
dotnet add package Swashbuckle.AspNetCore --version 6.5.0
- Restore Dependencies:
dotnet restore
- Configure OpenTelemetry:
Update Program.cs
with your OpenObserve credentials. You can find these in OpenObserve UI under Data Sources → Custom → Traces → OpenTelemetry → OTLP HTTP.
builder.Services.AddOpenTelemetry()
.WithTracing(builder => builder
.SetResourceBuilder(resourceBuilder)
.AddSource(TracingInstrumentation.ServiceName)
.AddAspNetCoreInstrumentation()
.AddHttpClientInstrumentation()
.AddOtlpExporter(opts =>
{
opts.Endpoint = new Uri("your_openobserve_url/v1/traces");
opts.Headers = "Authorization=Basic YOUR_AUTH_TOKEN";
opts.Protocol = OpenTelemetry.Exporter.OtlpExportProtocol.HttpProtobuf;
}));
Note: Remember to add
/v1/traces
to your OTLP HTTP endpoint
- Run the Application:
dotnet run
-
Access Swagger UI:
- Open
https://localhost:5xxx/swagger
in your browser - The exact port will be shown in the console when you run the application
- Open
-
Create an Order:
- Using Swagger UI: Try the POST /order endpoint
- Using curl:
curl -X POST https://localhost:5xxx/order \
-H "Content-Type: application/json" \
-d '{"customerName":"John Doe","amount":99.99}' \
-k
- Retrieve an Order:
- Using Swagger UI: Try the GET /order/{id} endpoint
- Using curl:
curl -k https://localhost:5xxx/order/1
- Navigate to the Traces section
- You should see traces appearing after making API requests
Note: There might be a slight delay before traces appear due to batching and export intervals
If traces aren't appearing in OpenObserve:
- Verify your OpenObserve endpoint URL and authentication token
- Check if you've added
/v1/traces
to the endpoint URL - Make some API requests to generate traces
- Wait a few moments for traces to be exported
- Check the application console for any export errors