You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: _posts/2024-12-27-trace-baggage-context-propagation-open-telementry.md
+86-25Lines changed: 86 additions & 25 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,34 +3,49 @@ layout: single
3
3
title: "Trace & Baggage Context Propagation in .NET with OpenTelemetry"
4
4
---
5
5
6
-
Context Propagation is an essential concept of distributed tracing. As the name suggests, it propagates the `trace` and `baggage` context over a network to other services.
6
+
Context propagation is a key part of distributed tracing. It enables transmitting both `trace` and `baggage` context across network boundaries so that different services in your system can participate in the same `trace`.
7
7
8
8
9
-
[Trace Context](https://www.w3.org/TR/trace-context/) and [Baggage Context](https://www.w3.org/TR/baggage/) are defined by the W3C standard. The propagation is by HTTP headers.
These contexts are transferred in HTTP headers (or other protocols), typically as traceparent for trace context and baggage for baggage context. Below, we will explore how this works in .NET with OpenTelemetry.
10
15
11
16
## TraceContext
12
17
13
-
The `traceparent` header represents incomming information about previous trace context.
18
+
The `traceparent` header contains information about the current trace.
To enable propagation (trace & baggege) over HTTP register
54
-
`AddAspNetCoreInstrumentation` and
55
-
`AddHttpClientInstrumentation` to do it by default.
72
+
## HTTP Propagation
73
+
74
+
By adding the following instrumentation, OpenTelemetry will automatically handle context propagation for incoming and outgoing HTTP requests.
75
+
76
+
***AddAspNetCoreInstrumentation** extracts traceparent and baggage from incoming HTTP requests.
77
+
***AddHttpClientInstrumentation** injects traceparent and baggage into outgoing HTTP requests.
78
+
79
+
56
80
```csharp
57
-
builder.Services.AddOpenTelemetry()
81
+
builder.Services.AddOpenTelemetry()
58
82
.UseOtlpExporter()
59
83
.WithTracing(tracing=>
60
-
{
61
-
tracing
84
+
{
85
+
tracing
62
86
.AddSource(builder.Environment.ApplicationName)
63
87
.AddAspNetCoreInstrumentation()
64
-
.AddHttpClientInstrumentation()
65
-
});
66
-
67
-
88
+
.AddHttpClientInstrumentation();
89
+
});
68
90
```
69
91
70
-
If you are interested how the HTTP Propagation works - [Incomming HTTP](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/blob/main/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/HttpInListener.cs#L109), [Outgoing HTTP](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/blob/main/src/OpenTelemetry.Instrumentation.Http/Implementation/HttpHandlerDiagnosticListener.cs#L94).
71
92
72
93
94
+
If you want to see how this works under the hood, check out the OpenTelemetry .NET code for [incoming HTTP instrumentation](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/blob/main/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/HttpInListener.cs#L109) and [outgoing HTTP instrumentation]((https://github.com/open-telemetry/opentelemetry-dotnet-contrib/blob/main/src/OpenTelemetry.Instrumentation.Http/Implementation/HttpHandlerDiagnosticListener.cs#L94)).
95
+
73
96
74
-
Propagate contexts over other protocols - messaging, database, etc. You need to do it manually with the `Inject` and then `Extract` methods through transfer attributes by `Propagators.DefaultTextMapPropagator` that provides registered propagators.
97
+
## Manual Propagation
98
+
99
+
For protocols not natively instrumented (e.g., messaging systems or databases), you'll need to manually propagate the context by calling the `Inject` and `Extract` methods on the `Propagators.DefaultTextMapPropagator`.
0 commit comments