Microservices Observability with OpenTelemetry

On this page

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

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

Manual Instrumentation

C#9 lines
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.

Written by

Nithin Mohan TK

Hello Namaste! 👋🏻 I'm an accomplished Enterprise Solution Architect & Engineering Leader with 20+ years of progressive experience architecting, building, and scaling transformative enterprise solutions. ⚜️ Solution Architect / AI & Cloud 📌 Galway, Ireland

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.