|
| 1 | +--- |
| 2 | +layout: single |
| 3 | +title: "Trace & Baggage Context Propagation .NET with OpenTelemetry" |
| 4 | +--- |
| 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. |
| 7 | + |
| 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. |
| 10 | + |
| 11 | +## TraceContext |
| 12 | + |
| 13 | +The `traceparent` header represents incomming information about previous trace context. |
| 14 | + |
| 15 | +`traceparent: 00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01` |
| 16 | + |
| 17 | +Format |
| 18 | +* version |
| 19 | +* trace-id |
| 20 | +* parent-id |
| 21 | +* trace-flags |
| 22 | + |
| 23 | +## BaggageContext |
| 24 | + |
| 25 | +The `baggage `header represents incomming baggage context. Its `key=value` representation. |
| 26 | + |
| 27 | +`baggage: key1=value1` |
| 28 | + |
| 29 | +The information is transferred in plaintext. Be aware of personally sensitive information. |
| 30 | + |
| 31 | +# .NET Usage |
| 32 | + |
| 33 | +Tracing configuration registers `TraceContextPropagator` and `BaggageContextPropagator` out of the box. |
| 34 | + |
| 35 | + |
| 36 | + ```csharp |
| 37 | +builder.Services.AddOpenTelemetry() |
| 38 | + .UseOtlpExporter() |
| 39 | + .WithTracing(tracing => |
| 40 | + { |
| 41 | + tracing.AddSource(builder.Environment.ApplicationName); |
| 42 | + }); |
| 43 | + ``` |
| 44 | + |
| 45 | +You can use your own propagators (zipking,...) |
| 46 | + ```csharp |
| 47 | +Sdk.SetDefaultTextMapPropagator(new CompositeTextMapPropagator( |
| 48 | + new List<TextMapPropagator>() { |
| 49 | + new MyCustomPropagator() |
| 50 | + })); |
| 51 | + ``` |
| 52 | + |
| 53 | +To enable propagation (trace & baggege) over HTTP register |
| 54 | +`AddAspNetCoreInstrumentation` and |
| 55 | +`AddHttpClientInstrumentation` to do it by default. |
| 56 | + ```csharp |
| 57 | +builder.Services.AddOpenTelemetry() |
| 58 | + .UseOtlpExporter() |
| 59 | + .WithTracing(tracing => |
| 60 | + { |
| 61 | + tracing |
| 62 | + .AddSource(builder.Environment.ApplicationName) |
| 63 | + .AddAspNetCoreInstrumentation() |
| 64 | + .AddHttpClientInstrumentation() |
| 65 | + }); |
| 66 | + |
| 67 | + |
| 68 | + ``` |
| 69 | + |
| 70 | + If you are interested how the HTTP Propagation is working - [Incomming HTTP](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/blob/main/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/HttpInListener.cs#L109), [OutComming HTTP](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/blob/main/src/OpenTelemetry.Instrumentation.Http/Implementation/HttpHandlerDiagnosticListener.cs#L94). |
| 71 | + |
| 72 | + |
| 73 | + |
| 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. |
0 commit comments