-
Notifications
You must be signed in to change notification settings - Fork 10.4k
Add System.Diagnostics.TextMapPropagator support #33777
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
f517ec5
4703a76
4465c89
7a4d031
511e8ef
c0da476
196a45b
abcc758
df05c15
ce58b19
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,12 +7,9 @@ | |
using System.Diagnostics; | ||
using System.Globalization; | ||
using System.Runtime.CompilerServices; | ||
using System.Web; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Http.Features; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Primitives; | ||
using Microsoft.Net.Http.Headers; | ||
|
||
namespace Microsoft.AspNetCore.Hosting | ||
{ | ||
|
@@ -31,13 +28,19 @@ internal class HostingApplicationDiagnostics | |
|
||
private readonly ActivitySource _activitySource; | ||
private readonly DiagnosticListener _diagnosticListener; | ||
private readonly DistributedContextPropagator _propagator; | ||
private readonly ILogger _logger; | ||
|
||
public HostingApplicationDiagnostics(ILogger logger, DiagnosticListener diagnosticListener, ActivitySource activitySource) | ||
public HostingApplicationDiagnostics( | ||
ILogger logger, | ||
DiagnosticListener diagnosticListener, | ||
ActivitySource activitySource, | ||
DistributedContextPropagator propagator) | ||
{ | ||
_logger = logger; | ||
_diagnosticListener = diagnosticListener; | ||
_activitySource = activitySource; | ||
_propagator = propagator; | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
|
@@ -279,38 +282,37 @@ private static void RecordRequestStartEventLog(HttpContext httpContext) | |
{ | ||
return null; | ||
} | ||
|
||
var headers = httpContext.Request.Headers; | ||
var requestId = headers.TraceParent; | ||
if (requestId.Count == 0) | ||
{ | ||
requestId = headers.RequestId; | ||
} | ||
|
||
if (!StringValues.IsNullOrEmpty(requestId)) | ||
_propagator.ExtractTraceIdAndState(headers, | ||
static (object? carrier, string fieldName, out string? fieldValue, out IEnumerable<string>? fieldValues) => | ||
{ | ||
fieldValues = default; | ||
var headers = (IHeaderDictionary)carrier!; | ||
fieldValue = headers[fieldName]; | ||
}, | ||
out var requestId, | ||
out var traceState); | ||
|
||
if (!string.IsNullOrEmpty(requestId)) | ||
{ | ||
activity.SetParentId(requestId); | ||
var traceState = headers.TraceState; | ||
if (traceState.Count > 0) | ||
if (!string.IsNullOrEmpty(traceState)) | ||
{ | ||
activity.TraceStateString = traceState; | ||
} | ||
|
||
// We expect baggage to be empty by default | ||
// Only very advanced users will be using it in near future, we encourage them to keep baggage small (few items) | ||
var baggage = headers.GetCommaSeparatedValues(HeaderNames.Baggage); | ||
if (baggage.Length == 0) | ||
var baggage = _propagator.ExtractBaggage(headers, static (object? carrier, string fieldName, out string? fieldValue, out IEnumerable<string>? fieldValues) => | ||
{ | ||
baggage = headers.GetCommaSeparatedValues(HeaderNames.CorrelationContext); | ||
} | ||
fieldValues = default; | ||
var headers = (IHeaderDictionary)carrier!; | ||
fieldValue = headers[fieldName]; | ||
}); | ||
|
||
// AddBaggage adds items at the beginning of the list, so we need to add them in reverse to keep the same order as the client | ||
// An order could be important if baggage has two items with the same key (that is allowed by the contract) | ||
for (var i = baggage.Length - 1; i >= 0; i--) | ||
// Order could be important if baggage has two items with the same key (that is allowed by the contract) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You used to add these in reverse, does ExtractBaggage handle the reversal? If so then include that in the comment. Otherwise this comment about order sensitivity doesn't make sense. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I’ll send a follow-up PR to fix the comment. I want to merge this when the PR checks are green in case AzDo has a bad day again |
||
if (baggage is not null) | ||
{ | ||
if (NameValueHeaderValue.TryParse(baggage[i], out var baggageItem)) | ||
foreach (var baggageItem in baggage) | ||
{ | ||
activity.AddBaggage(baggageItem.Name.ToString(), HttpUtility.UrlDecode(baggageItem.Value.ToString())); | ||
activity.AddBaggage(baggageItem.Key, baggageItem.Value); | ||
} | ||
} | ||
} | ||
|
Uh oh!
There was an error while loading. Please reload this page.