Skip to content

openobserve/dotnet-opentelemetry-tracing-application

Repository files navigation

.NET Sample Application with OpenTelemetry Tracing

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.

About the Demo Application

This demo implements two main endpoints:

  • POST /order - Create a new order
  • GET /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

Prerequisites

Getting Started

  1. Clone the Repository:
git clone https://github.com/openobserve/dotnet-opentelemetry-tracing-application
cd dotnet-opentelemetry-tracing-application
  1. 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
  1. Restore Dependencies:
dotnet restore
  1. 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

  1. Run the Application:
dotnet run

Using the API

  1. 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
  2. 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
  1. Retrieve an Order:
    • Using Swagger UI: Try the GET /order/{id} endpoint
    • Using curl:
curl -k https://localhost:5xxx/order/1

Viewing Traces in OpenObserve

  1. Navigate to the Traces section
  2. You should see traces appearing after making API requests

Image

Note: There might be a slight delay before traces appear due to batching and export intervals

Troubleshooting

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

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages