Skip to content

Commit

Permalink
Prevent X-Amzn-Trace-Id from Being Added More Than Once (#40)
Browse files Browse the repository at this point in the history
* Prevent X-Amzn-TraceId from Being Added More Than Once

Under some circumstances, the chain of calls through
`DelegatingHandler` instances can reach a particular handler more than
once. When that handler is `HttpClientXRayTracingHandler`, it produces
an X-Amzn-Trace-Id header that cannot be parsed because it is
_multiple_ headers. (As represented in a string by separating them
with ", ".)

This patch checks whether X-Amzn-Trace-Id has already been added, and
skips adding another if it already has been.

* Replaces Existing X-Amzn-Trace-Id Header, If Present
  • Loading branch information
chrisoverzero authored and Yogiraj Awati committed Oct 3, 2018
1 parent 994c27f commit 9948885
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
8 changes: 7 additions & 1 deletion sdk/src/Handlers/System.Net/Utils/RequestUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,13 @@ internal static void ProcessRequest(WebRequest request)
/// <param name="request">An instance of <see cref="HttpRequestMessage"/></param>
internal static void ProcessRequest(HttpRequestMessage request)
{
ProcessRequest(request.RequestUri, request.Method.Method, header => request.Headers.Add(TraceHeader.HeaderKey, header));
ProcessRequest(request.RequestUri, request.Method.Method, AddOrReplaceHeader);

void AddOrReplaceHeader(string header)
{
request.Headers.Remove(TraceHeader.HeaderKey);
request.Headers.Add(TraceHeader.HeaderKey, header);
}
}

/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions sdk/test/UnitTests/HttpClientXRayTracingHandlerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public async Task TestSendAsync()
var segment = TraceContext.GetEntity();
AWSXRayRecorder.Instance.EndSegment();

var traceHeader = request.Headers.GetValues(TraceHeader.HeaderKey).FirstOrDefault();
var traceHeader = request.Headers.GetValues(TraceHeader.HeaderKey).SingleOrDefault();
Assert.IsNotNull(traceHeader);

var requestInfo = segment.Subsegments[0].Http["request"] as Dictionary<string, object>;
Expand All @@ -77,7 +77,7 @@ public async Task Test404SendAsync()
var segment = TraceContext.GetEntity();
AWSXRayRecorder.Instance.EndSegment();

var traceHeader = request.Headers.GetValues(TraceHeader.HeaderKey).FirstOrDefault();
var traceHeader = request.Headers.GetValues(TraceHeader.HeaderKey).SingleOrDefault();
Assert.IsNotNull(traceHeader);

var requestInfo = segment.Subsegments[0].Http["request"] as Dictionary<string, object>;
Expand Down

0 comments on commit 9948885

Please sign in to comment.