Distributed Tracing
Trace requests across services with Activity.
Distributed Tracing is a free C# Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What Is Distributed Tracing?
Distributed tracing tracks one request as it travels across services and components. Each unit of work becomes a span, and the connected spans form a trace, visualized as a timeline of nested operations.
Spans And Traces
A span has a name, start and end time, attributes and a status. Spans nest: a parent span (the HTTP request) contains child spans (a database query, an outbound call). A whole tree shares one trace id.
Activity Is .NET's Span
In .NET, the System.Diagnostics.Activity class is the implementation of an OpenTelemetry span. You create activities from an ActivitySource.
using System.Diagnostics;
private static readonly ActivitySource Source =
new("Checkout.Orders");Starting An Activity
StartActivity begins a span. Wrap it in a using so it ends (records its duration) when the block exits.
using Activity activity = Source.StartActivity("PlaceOrder");
// work happens here; span ends when disposedAdding Tags To A Span
Tags (attributes) attach key-value context to a span, like an order id or amount, making traces searchable and meaningful.
activity?.SetTag("order.id", orderId);
activity?.SetTag("order.amount", amount);Recording Events And Status
Use AddEvent for point-in-time annotations and SetStatus to mark success or failure, which surfaces errors in your tracing backend.
activity?.AddEvent(new ActivityEvent("inventory.reserved"));
activity?.SetStatus(ActivityStatusCode.Error, "Payment declined");Registering The ActivitySource
OTel only collects activities from sources you register with AddSource, so tell the tracer about your ActivitySource name.
builder.Services.AddOpenTelemetry()
.WithTracing(tracing => tracing
.AddSource("Checkout.Orders"));Automatic Instrumentation
Built-in instrumentation creates spans for incoming ASP.NET Core requests and outgoing HttpClient calls automatically, so you get a baseline trace without writing code.
tracing
.AddAspNetCoreInstrumentation()
.AddHttpClientInstrumentation();Context Propagation
For a trace to span services, the trace id must travel between them. OTel injects it into outgoing HTTP headers (W3C traceparent) and reads it on the receiving end, stitching spans into one trace automatically.
Parent And Child Relationships
When you start an activity while another is current, the new one becomes its child via Activity.Current. This nesting builds the trace tree without manual wiring.
using var parent = Source.StartActivity("PlaceOrder");
using var child = Source.StartActivity("ChargeCard");
// child is nested under parent automaticallySampling
Tracing every request can be expensive. Sampling records only a portion (for example 10%) to control cost while keeping representative data.
Quick Check
Test distributed tracing.
Recap
Distributed tracing follows a request as connected spans sharing a trace id. In .NET, Activity is the span, created from a registered ActivitySource via StartActivity (use using to end it). Add context with SetTag, AddEvent and SetStatus. Auto-instrumentation plus W3C context propagation stitch traces across services; sampling controls cost.
Frequently asked questions
Is the “Distributed Tracing” lesson free?
Yes — the full text of “Distributed Tracing” is free to read here on the web, and the C# Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO.
What will I learn in “Distributed Tracing”?
Trace requests across services with Activity. You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C# Academy?
No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Distributed Tracing” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C# Academy lesson?
Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- The Three Pillars of Observability
- Distributed Tracing
- Collecting Metrics
- Exporting to Backends