DiagnosticsHandler the class has a method InjectHeaders, which is executed when transferring activity fields to the request header
{
_propagator.Inject(currentActivity, request, static (carrier, key, value) =>
{
if (carrier is HttpRequestMessage request &&
key is not null &&
HeaderDescriptor.TryGet(key, out HeaderDescriptor descriptor) &&
!request.Headers.TryGetHeaderValue(descriptor, out _))
{
request.Headers.TryAddWithoutValidation(descriptor, value);
}
);
}
I have a custom SamplePropagator : DistributedContextPropagator which has overridden the InjectBaggage method
{
using var e = baggage.GetEnumerator();
if (e.MoveNext())
{
var baggageList = new StringBuilder();
do
{
KeyValuePair<string, string> item = e.Current;
baggageList.Append(WebUtility.UrlEncode(item.Key))
.Append('=')
.Append(WebUtility.UrlEncode(item.Value))
.Append(CommaWithSpace);
} while (e.MoveNext());
setter(carrier, HeaderNames.Baggage, baggageList.ToString(0, baggageList.Length - 2));
}
}
Thus the activity baggage is transferred to the request headers baggage
If at the time of calling DiagnosticsHandler.InjectHeaders there is already baggage in the request header (in my case I use the Sentry package Sentry.AspNetCore, which adds various values to the baggage header before this) then the DiagnosticsHandler code on line 474 request.Headers.TryAddWithoutValidation(descriptor, value); is not executed because the condition !request.Headers.TryGetHeaderValue(descriptor, out _) on line 372 is not satisfied.
However, the HttpRequestHeaders.TryAddWithoutValidation method handles the case of adding a new header to an existing value
HttpRequestMessage t = new HttpRequestMessage();
t.Headers.TryAddWithoutValidation(HeaderNames.Baggage, "sentry=123");
t.Headers.TryAddWithoutValidation(HeaderNames.Baggage, "id=123");
Description
Thus the activity baggage is transferred to the request headers baggage
If at the time of calling DiagnosticsHandler.InjectHeaders there is already baggage in the request header (in my case I use the Sentry package Sentry.AspNetCore, which adds various values to the baggage header before this) then the DiagnosticsHandler code on line 474
request.Headers.TryAddWithoutValidation(descriptor, value);
is not executed because the condition!request.Headers.TryGetHeaderValue(descriptor, out _)
on line 372 is not satisfied.However, the HttpRequestHeaders.TryAddWithoutValidation method handles the case of adding a new header to an existing value
result: "[baggage, System.String[]]";baggage;"[sentry=123, id=123]"
Problems I am losing baggage from the current activity because it is not added to the existing header in the request
Configuration .NET Core 8, on windows 10 Propagator SamplePropagator.txt DiagnosticSample DiagnosticSample.txt RequestIdController RequestIdController.txt Startup