opentelemetry

OpenTelemetry observability for .NET 10 applications. Covers traces, metrics, and logs using the OpenTelemetry SDK with OTLP export. Includes custom ActivitySource, IMeterFactory metrics, resource configuration, and Aspire Dashboard integration. Load this skill when setting up distributed tracing, c

By codewithmukesh · 1,182 installs

npx skills add codewithmukesh/dotnet-claude-kit --skill opentelemetry

Source repository · Upstream listing

OpenTelemetry Core Principles 1. Three pillars, one setup — Configure traces, metrics, and logs through a single AddOpenTelemetry() call. Use UseOtlpExporter() for cross cutting export to any OTLP compatible backend. 2. Use IMeterFactory for metrics — Never create Meter instances with new . The factory manages lifetime through DI and prevents leaks. 3. Null safe activities — StartActivity() returns null when no listener is attached. Always use ?. when setting tags or events. 4. Environment variables over code — Use OTEL EXPORTER OTLP ENDPOINT and OTEL SERVICE NAME so deployments control telemetry routing without code changes. 5. Low cardinality metric tags — Keep metric tag combinations under ~1000 per instrument. Use span attributes or logs for high cardinality data like user IDs or request IDs. Patterns Full Setup with All Three Signals The OTLP endpoint defaults to http://localhost:4317 (gRPC). Override via: Custom Metrics with IMeterFactory Register a metrics class as a singleton. IMeterFactory handles Meter disposal through DI. Multi Dimensional Metric Tags Three or fewer tags are allocation free. For more, use TagList . Custom ActivitySource for Distributed Tracing Register the source: .AddSource("MyApp.Orders") in the tracing builder. Aspire Dashboard for Local Development Run the standalone Aspire Dashboard without Aspire orchestration: Then point your app at it: Dashboard UI is at http://localhost:18888 . Source Generated Logging with OTel For maximum performance, use [LoggerMessage] — eliminates boxing and allocations. OpenTelemetry logging automatically includes TraceId and SpanId when an Activity is current. Anti patterns Don't Create Meters Per Request Don't Skip Null Checks on Activity Don't Use High Cardinality Metric Tags Don't Mix UseOtlpExporter with AddOtlpExporter Don't Forget to Register Custom Sources Decision Guide Scenario Recommendation Full observability setup AddOpenTelemetry() with all three signals + UseOtlpExporter() Custom business metrics IMeterFactory + singleton metrics class Custom trace spans ActivitySource + StartActivity() Local development backend Aspire Dashboard standalone container Production backend OTel Collector as intermediary to Grafana/Datadog/etc. Sampling in production OTEL TRACES SAMPLER=parentbased traceidratio with 10% ratio High performance logging [LoggerMessage] source generator Metric tag cardinality Max ~1000 combinations per instrument Environment configuration OTEL env vars (also work via appsettings.json )