Skip to content
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

Support W3C Baggage header #28328

Merged
merged 4 commits into from
Dec 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,11 @@ private Activity StartActivity(HttpContext httpContext, out bool hasDiagnosticLi

// 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)
string[] baggage = headers.GetCommaSeparatedValues(HeaderNames.CorrelationContext);
var baggage = headers.GetCommaSeparatedValues(HeaderNames.Baggage);
if (baggage.Length == 0)
{
baggage = headers.GetCommaSeparatedValues(HeaderNames.CorrelationContext);
}

// 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)
Expand Down
71 changes: 66 additions & 5 deletions src/Hosting/Hosting/test/HostingApplicationDiagnosticsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ public void ActivityIsAvailibleDuringRequest()
}

[Fact]
public void ActivityParentIdAndBaggeReadFromHeaders()
public void ActivityParentIdAndBaggageReadFromHeaders()
{
var diagnosticListener = new DiagnosticListener("DummySource");
var hostingApplication = CreateApplication(out var features, diagnosticListener: diagnosticListener);
Expand All @@ -291,7 +291,7 @@ public void ActivityParentIdAndBaggeReadFromHeaders()
Headers = new HeaderDictionary()
{
{"Request-Id", "ParentId1"},
{"Correlation-Context", "Key1=value1, Key2=value2"}
{"baggage", "Key1=value1, Key2=value2"}
}
});
hostingApplication.CreateContext(features);
Expand All @@ -301,6 +301,67 @@ public void ActivityParentIdAndBaggeReadFromHeaders()
Assert.Contains(Activity.Current.Baggage, pair => pair.Key == "Key2" && pair.Value == "value2");
}

[Fact]
public void ActivityBaggageReadFromLegacyHeaders()
{
var diagnosticListener = new DiagnosticListener("DummySource");
var hostingApplication = CreateApplication(out var features, diagnosticListener: diagnosticListener);

diagnosticListener.Subscribe(new CallbackDiagnosticListener(pair => { }),
s =>
{
if (s.StartsWith("Microsoft.AspNetCore.Hosting.HttpRequestIn", StringComparison.Ordinal))
{
return true;
}
return false;
});

features.Set<IHttpRequestFeature>(new HttpRequestFeature()
{
Headers = new HeaderDictionary()
{
{"Request-Id", "ParentId1"},
{"Correlation-Context", "Key1=value1, Key2=value2"}
}
});
hostingApplication.CreateContext(features);
Assert.Equal("Microsoft.AspNetCore.Hosting.HttpRequestIn", Activity.Current.OperationName);
Assert.Contains(Activity.Current.Baggage, pair => pair.Key == "Key1" && pair.Value == "value1");
Assert.Contains(Activity.Current.Baggage, pair => pair.Key == "Key2" && pair.Value == "value2");
}

[Fact]
public void ActivityBaggagePrefersW3CBaggageHeaderName()
{
var diagnosticListener = new DiagnosticListener("DummySource");
var hostingApplication = CreateApplication(out var features, diagnosticListener: diagnosticListener);

diagnosticListener.Subscribe(new CallbackDiagnosticListener(pair => { }),
s =>
{
if (s.StartsWith("Microsoft.AspNetCore.Hosting.HttpRequestIn", StringComparison.Ordinal))
{
return true;
}
return false;
});

features.Set<IHttpRequestFeature>(new HttpRequestFeature()
{
Headers = new HeaderDictionary()
{
{"Request-Id", "ParentId1"},
{"Correlation-Context", "Key1=value1, Key2=value2"},
{"baggage", "Key1=value3, Key2=value4"}
}
});
hostingApplication.CreateContext(features);
Assert.Equal("Microsoft.AspNetCore.Hosting.HttpRequestIn", Activity.Current.OperationName);
Assert.Contains(Activity.Current.Baggage, pair => pair.Key == "Key1" && pair.Value == "value3");
Assert.Contains(Activity.Current.Baggage, pair => pair.Key == "Key2" && pair.Value == "value4");
}

[Fact]
public void ActivityBaggagePreservesItemsOrder()
{
Expand All @@ -322,7 +383,7 @@ public void ActivityBaggagePreservesItemsOrder()
Headers = new HeaderDictionary()
{
{"Request-Id", "ParentId1"},
{"Correlation-Context", "Key1=value1, Key2=value2, Key1=value3"} // duplicated keys allowed by the contract
{"baggage", "Key1=value1, Key2=value2, Key1=value3"} // duplicated keys allowed by the contract
}
});
hostingApplication.CreateContext(features);
Expand Down Expand Up @@ -359,7 +420,7 @@ public void ActivityBaggageValuesAreUrlDecodedFromHeaders()
Headers = new HeaderDictionary()
{
{"Request-Id", "ParentId1"},
{"Correlation-Context", "Key1=value1%2F1"}
{"baggage", "Key1=value1%2F1"}
}
});
hostingApplication.CreateContext(features);
Expand Down Expand Up @@ -389,7 +450,7 @@ public void ActivityTraceParentAndTraceStateFromHeaders()
{
{"traceparent", "00-0123456789abcdef0123456789abcdef-0123456789abcdef-01"},
{"tracestate", "TraceState1"},
{"Correlation-Context", "Key1=value1, Key2=value2"}
{"baggage", "Key1=value1, Key2=value2"}
}
});
hostingApplication.CreateContext(features);
Expand Down
3 changes: 3 additions & 0 deletions src/Http/Headers/src/HeaderNames.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ public static class HeaderNames
/// <summary>Gets the <c>Authorization</c> HTTP header name.</summary>
public static readonly string Authorization = "Authorization";

/// <summary>Gets the <c>baggage</c> HTTP header name.</summary>
public static readonly string Baggage = "baggage";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All other headers are pascal case. This won't matter when getting the value from the headers collection - it is case-insensitive.

Suggested change
public static readonly string Baggage = "baggage";
public static readonly string Baggage = "Baggage";

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I was curious about that. I kept it consistent with traceparent and tracestate, the other two headers related to this standard (W3C Trace Context) that are lowercase in this class.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. Perhaps better as is then.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since HTTP/2 all header names are officially lower case so it might make sense to start doing that with new entries. You're right though that the collections are all ignore-case so it shouldn't make a difference.


/// <summary>Gets the <c>Cache-Control</c> HTTP header name.</summary>
public static readonly string CacheControl = "Cache-Control";

Expand Down
1 change: 1 addition & 0 deletions src/Http/Headers/src/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#nullable enable
Microsoft.Net.Http.Headers.MediaTypeHeaderValue.MatchesMediaType(Microsoft.Extensions.Primitives.StringSegment otherMediaType) -> bool
Microsoft.Net.Http.Headers.RangeConditionHeaderValue.RangeConditionHeaderValue(Microsoft.Net.Http.Headers.EntityTagHeaderValue! entityTag) -> void
static readonly Microsoft.Net.Http.Headers.HeaderNames.Baggage -> string!
static readonly Microsoft.Net.Http.Headers.HeaderNames.ProxyConnection -> string!