Microservices Observability with OpenTelemetry

OpenTelemetry (OTel) is the merger of OpenTracing and OpenCensus. In 2020, it is becoming the CNCF standard for collecting metrics and traces. This guide shows how to instrument a .NET Core app to send traces to Jaeger.

Setup

services.AddOpenTelemetryTracing(builder =>
{
    builder
        .AddAspNetCoreInstrumentation()
        .AddHttpClientInstrumentation()
        .AddSqlClientInstrumentation()
        .AddSource("MyCompany.OrderService")
        .AddJaegerExporter(o => 
        {
            o.AgentHost = "jaeger-agent";
            o.AgentPort = 6831;
        });
});

Manual Instrumentation

private static readonly ActivitySource Source = new ActivitySource("MyCompany.OrderService");

public async Task ProcessOrder()
{
    using var activity = Source.StartActivity("ProcessOrder");
    activity?.SetTag("orderId", 123);
    
    // Logic...
}

Key Takeaways

  • Vendor-neutral instrumentation prevents lock-in.
  • “Activity” in .NET 5+ is the native implementation of an OTel Span.
  • Context propagation works automatically over HTTP (W3C Trace Context).

Discover more from C4: Container, Code, Cloud & Context

Subscribe to get the latest posts sent to your email.

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.