This project demonstrates a comprehensive OpenTelemetry setup using .NET 9, Serilog, and .NET Aspire with its built-in observability dashboard.
- .NET 9 ASP.NET Core Web API
- OpenTelemetry integration for traces, metrics, and logs
- Serilog with OpenTelemetry sink for structured logging
- .NET Aspire for local development orchestration with built-in observability dashboard
- Scalar API documentation UI for easy API testing and exploration
- All telemetry (logs, traces, metrics) automatically routed to Aspire Dashboard
This project demonstrates many OpenTelemetry concepts:
- Baggage Propagation - Cross-cutting context propagation
- Span Links - Relating non-parent-child operations
- Advanced Metrics - Counters, Histograms, UpDownCounters, Observable instruments
- Custom Sampling - Intelligent trace sampling strategies
- Context Propagation - Manual trace context handling
- Rich Telemetry - Custom attributes, events, and status codes
π See the Complete OpenTelemetry Feature Guide for a hands-on tour of all features.
π‘ OTLP Configuration Guide - Learn how to configure OTLP export for logs, traces, and metrics.
- .NET 9 SDK
- Docker and Docker Compose
- Visual Studio 2022 or VS Code (optional)
OpenTelemetryDemo/
βββ OpenTelemetryDemo.Api/ # ASP.NET Core API project
βββ OpenTelemetryDemo.ServiceDefaults/ # Shared OpenTelemetry configuration
βββ OpenTelemetryDemo.AppHost/ # .NET Aspire orchestrator
βββ requests.http # Sample HTTP requests
cd OpenTelemetryDemo.AppHost
dotnet run
This will:
- Start the Aspire Dashboard (automatically opens in browser)
- Launch the API project
- Configure all telemetry to flow to the Aspire Dashboard
- The API will be available at the port shown in the Aspire Dashboard
Note: When you see the error about missing environment variables, the launch settings file will configure them automatically on the next run.
Use the provided requests.http
file with VS Code REST Client extension or any HTTP client:
# Get weather forecast
curl http://localhost:5000/WeatherForecast
# Simulate error
curl http://localhost:5000/WeatherForecast/error
# Test slow endpoint
curl http://localhost:5000/WeatherForecast/slow?delayMs=3000
# Process with metrics
curl http://localhost:5000/Metrics/process?complexity=2
Navigate to /scalar/v1
on your API endpoint (check the Aspire Dashboard for the port) to access the Scalar API documentation UI. For example:
http://localhost:5089/scalar/v1
Scalar provides:
- Interactive API documentation
- Built-in API testing capabilities
- Code generation for multiple languages
- Beautiful, modern UI
The Aspire Dashboard automatically opens when you run the application. You can view:
- Structured Logs: All Serilog logs with trace correlation
- Distributed Traces: Full request traces with spans
- Metrics: Real-time metrics for your application
- Resources: Status of your running services
The ServiceDefaults
project contains centralized OpenTelemetry configuration:
- Traces: ASP.NET Core and HTTP client instrumentation
- Metrics: Runtime, process, and custom metrics
- Logs: Serilog integration with OpenTelemetry sink
The API includes examples of:
- Custom ActivitySource for distributed tracing
- Custom Meters for business metrics
- Structured logging with trace correlation
- Error handling with proper telemetry
GET /WeatherForecast
- Get weather forecastGET /WeatherForecast/{date}
- Get forecast for specific dateGET /WeatherForecast/error
- Simulate errorGET /WeatherForecast/slow
- Simulate slow response
GET /Metrics/process
- Process with custom metricsGET /Metrics/batch
- Batch processing exampleGET /Metrics/status
- Application status
GET /TelemetryDemo/baggage
- Demonstrates baggage propagationGET /TelemetryDemo/span-links
- Shows span linking for batch operationsGET /TelemetryDemo/metrics-demo
- Showcases all metric typesGET /TelemetryDemo/custom-telemetry
- Rich telemetry with events and attributesGET /TelemetryDemo/context-propagation
- Manual context propagation
GET /health
- Health checkGET /scalar/v1
- Interactive API documentation
ASPNETCORE_ENVIRONMENT
- Environment name (Development/Production)- Aspire automatically configures
OTEL_EXPORTER_OTLP_ENDPOINT
when running
Serilog is configured to:
- Write to console with structured output
- Send logs to OpenTelemetry collector via OTLP
- Include trace and span IDs for correlation
- Enrich logs with environment and thread information
- Ensure the application is running through the AppHost project
- Check the console output for any errors
- Verify you're making requests to generate telemetry
- The dashboard should automatically receive all telemetry data
Ensure you have .NET 9 SDK installed: dotnet --version
var meter = new Meter("YourApp", "1.0.0");
var counter = meter.CreateCounter<long>("custom_metric");
counter.Add(1, new("tag", "value"));
using var activity = ActivitySource.StartActivity("CustomOperation");
activity?.SetTag("custom.tag", "value");
- Zero Configuration: Aspire automatically configures OTLP endpoints
- Integrated Experience: See logs, traces, and metrics in one place
- Development Focused: Optimized for local development scenarios
- Real-time Updates: Live streaming of telemetry data
- Correlation: Easy navigation between related logs and traces
This is a demo project for educational purposes.