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

Fixing bug in RequestUtil.ProcessRequest #58

Merged
merged 3 commits into from
Feb 27, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
10 changes: 5 additions & 5 deletions sdk/src/Handlers/System.Net/Utils/RequestUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@ private static void ProcessRequest(Uri uri, string method, Action<string> addHea
["method"] = method
};
AWSXRayRecorder.Instance.AddHttpInformation("request", requestInformation);
}

if (TraceHeader.TryParse(AWSXRayRecorder.Instance.TraceContext.GetEntity(), out var header))
{
addHeaderAction(header.ToString());
if (TraceHeader.TryParse(AWSXRayRecorder.Instance.TraceContext.GetEntity(), out var header))
{
addHeaderAction(header.ToString());
}
}
}

Expand Down
49 changes: 37 additions & 12 deletions sdk/test/UnitTests/HttpClientXRayTracingHandlerTests.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Amazon.XRay.Recorder.Core;
using Amazon.XRay.Recorder.Core.Internal.Entities;
using Amazon.XRay.Recorder.Core.Internal.Utils;
using Amazon.XRay.Recorder.Handlers.System.Net;
using Amazon.XRay.Recorder.UnitTests.Tools;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Amazon.XRay.Recorder.UnitTests
Expand All @@ -17,30 +19,30 @@ public class HttpClientXRayTracingHandlerTests : TestBase

private const string URL404 = "https://httpbin.org/404";

private readonly HttpClient _httpClient;
private readonly HttpClient _httpClient;

private static AWSXRayRecorder _recorder;

public HttpClientXRayTracingHandlerTests()
{
_httpClient = new HttpClient(new HttpClientXRayTracingHandler(new HttpClientHandler()));
}
}

[TestInitialize]
public void TestInitialize()
{
public void TestInitialize()
{
_recorder = new AWSXRayRecorder();
#if NET45
AWSXRayRecorder.InitializeInstance(_recorder);
#else
AWSXRayRecorder.InitializeInstance(recorder: _recorder);
# endif
# endif
}

[TestCleanup]
public new void TestCleanup()
{
base.TestCleanup();
base.TestCleanup();
_recorder.Dispose();
_recorder = null;
}
Expand All @@ -50,8 +52,8 @@ public async Task TestSendAsync()
{
AWSXRayRecorder.Instance.BeginSegment("parent", TraceId);
var request = new HttpRequestMessage(HttpMethod.Get, URL);
var response = await _httpClient.SendAsync(request);
using(await _httpClient.SendAsync(request)) {}

var segment = AWSXRayRecorder.Instance.TraceContext.GetEntity();
AWSXRayRecorder.Instance.EndSegment();

Expand All @@ -66,14 +68,37 @@ public async Task TestSendAsync()
Assert.AreEqual(200, responseInfo["status"]);
Assert.IsNotNull(responseInfo["content_length"]);
}

/// <summary>
/// Ensures that when tracing is disabled that HTTP requests can execute as normal. \
/// See https://github.com/aws/aws-xray-sdk-dotnet/issues/57 for more information.
/// </summary>
[TestMethod]
public async Task TestSendAsync_XrayDisabled()
{
_recorder = new MockAWSXRayRecorder() { IsTracingDisabledValue = true };
#if NET45
AWSXRayRecorder.InitializeInstance(_recorder);
#else
AWSXRayRecorder.InitializeInstance(recorder: _recorder);
# endif
Assert.IsTrue(AWSXRayRecorder.Instance.IsTracingDisabled());

var request = new HttpRequestMessage(HttpMethod.Get, URL);
using (var response = await _httpClient.SendAsync(request))
{
Assert.IsNotNull(response);
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
}
}

[TestMethod]
public async Task Test404SendAsync()
{
AWSXRayRecorder.Instance.BeginSegment("parent", TraceId);
var request = new HttpRequestMessage(HttpMethod.Get, URL404);
var response = await _httpClient.SendAsync(request);
using(await _httpClient.SendAsync(request)) {}

var segment = AWSXRayRecorder.Instance.TraceContext.GetEntity();
AWSXRayRecorder.Instance.EndSegment();

Expand Down
90 changes: 60 additions & 30 deletions sdk/test/UnitTests/HttpWebRequestTracingExtensionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@
// </copyright>
//-----------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Net;
using System.Threading.Tasks;
using Amazon.XRay.Recorder.Core;
using Amazon.XRay.Recorder.Core.Internal.Entities;
using Amazon.XRay.Recorder.Core.Internal.Utils;
using Amazon.XRay.Recorder.Handlers.System.Net;
using Amazon.XRay.Recorder.UnitTests.Tools;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Amazon.XRay.Recorder.UnitTests
Expand All @@ -31,25 +33,25 @@ public class HttpWebRequestTracingExtensionTests : TestBase
{
private const string URL = "https://httpbin.org/";

private const string URL404 = "https://httpbin.org/404";
private static AWSXRayRecorder _recorder;
private const string URL404 = "https://httpbin.org/404";

private static AWSXRayRecorder _recorder;

[TestInitialize]
public void TestInitialize()
{
public void TestInitialize()
{
_recorder = new AWSXRayRecorder();
#if NET45
AWSXRayRecorder.InitializeInstance(_recorder);
#else
AWSXRayRecorder.InitializeInstance(recorder: _recorder);
# endif
# endif
}

[TestCleanup]
public new void TestCleanup()
{
base.TestCleanup();
base.TestCleanup();
_recorder.Dispose();
_recorder = null;
}
Expand All @@ -60,19 +62,45 @@ public void TestGetResponseTraced()
var request = (HttpWebRequest)WebRequest.Create(URL);

AWSXRayRecorder.Instance.BeginSegment("parent", TraceId);
request.GetResponseTraced();
var segment = AWSXRayRecorder.Instance.TraceContext.GetEntity();
AWSXRayRecorder.Instance.EndSegment();
using (request.GetResponseTraced())
{
var segment = AWSXRayRecorder.Instance.TraceContext.GetEntity();
AWSXRayRecorder.Instance.EndSegment();

Assert.IsNotNull(request.Headers[TraceHeader.HeaderKey]);

Assert.IsNotNull(request.Headers[TraceHeader.HeaderKey]);
var requestInfo = segment.Subsegments[0].Http["request"] as Dictionary<string, object>;
Assert.AreEqual(URL, requestInfo["url"]);
Assert.AreEqual("GET", requestInfo["method"]);

var requestInfo = segment.Subsegments[0].Http["request"] as Dictionary<string, object>;
Assert.AreEqual(URL, requestInfo["url"]);
Assert.AreEqual("GET", requestInfo["method"]);
var responseInfo = segment.Subsegments[0].Http["response"] as Dictionary<string, object>;
Assert.AreEqual(200, responseInfo["status"]);
Assert.IsNotNull(responseInfo["content_length"]);
}
}

/// <summary>
/// Ensures that when tracing is disabled that HTTP requests can execute as normal. \
/// See https://github.com/aws/aws-xray-sdk-dotnet/issues/57 for more information.
/// </summary>
[TestMethod]
public void TestGetResponseTraced_XrayDisabled()
{
_recorder = new MockAWSXRayRecorder() { IsTracingDisabledValue = true };
#if NET45
AWSXRayRecorder.InitializeInstance(_recorder);
#else
AWSXRayRecorder.InitializeInstance(recorder: _recorder);
# endif
Assert.IsTrue(AWSXRayRecorder.Instance.IsTracingDisabled());

var responseInfo = segment.Subsegments[0].Http["response"] as Dictionary<string, object>;
Assert.AreEqual(200, responseInfo["status"]);
Assert.IsNotNull(responseInfo["content_length"]);
var request = (HttpWebRequest)WebRequest.Create(URL);

using (var response = request.GetResponseTraced() as HttpWebResponse)
yogiraj07 marked this conversation as resolved.
Show resolved Hide resolved
{
Assert.IsNotNull(response);
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
}
}

[TestMethod]
Expand All @@ -81,19 +109,21 @@ public async Task TestGetAsyncResponseTraced()
var request = (HttpWebRequest)WebRequest.Create(URL);

AWSXRayRecorder.Instance.BeginSegment("parent", TraceId);
await request.GetAsyncResponseTraced();
var segment = AWSXRayRecorder.Instance.TraceContext.GetEntity();
AWSXRayRecorder.Instance.EndSegment();
using (await request.GetAsyncResponseTraced())
{
var segment = AWSXRayRecorder.Instance.TraceContext.GetEntity();
AWSXRayRecorder.Instance.EndSegment();

Assert.IsNotNull(request.Headers[TraceHeader.HeaderKey]);
Assert.IsNotNull(request.Headers[TraceHeader.HeaderKey]);

var requestInfo = segment.Subsegments[0].Http["request"] as Dictionary<string, object>;
Assert.AreEqual(URL, requestInfo["url"]);
Assert.AreEqual("GET", requestInfo["method"]);
var requestInfo = segment.Subsegments[0].Http["request"] as Dictionary<string, object>;
Assert.AreEqual(URL, requestInfo["url"]);
Assert.AreEqual("GET", requestInfo["method"]);

var responseInfo = segment.Subsegments[0].Http["response"] as Dictionary<string, object>;
Assert.AreEqual(200, responseInfo["status"]);
Assert.IsNotNull(responseInfo["content_length"]);
var responseInfo = segment.Subsegments[0].Http["response"] as Dictionary<string, object>;
Assert.AreEqual(200, responseInfo["status"]);
Assert.IsNotNull(responseInfo["content_length"]);
}
}

#if !NET45
Expand All @@ -105,7 +135,7 @@ public void TestExceptionGetResponseTraced()
AWSXRayRecorder.Instance.BeginSegment("parent", TraceId);
try
{
request.GetResponseTraced();
using (request.GetResponseTraced()) {}
Assert.Fail();
}

Expand Down Expand Up @@ -138,7 +168,7 @@ public async Task TestExceptionGetAsyncResponseTraced()
AWSXRayRecorder.Instance.BeginSegment("parent", TraceId);
try
{
await request.GetAsyncResponseTraced();
using (await request.GetAsyncResponseTraced()) {}
Assert.Fail();
}

Expand Down
14 changes: 14 additions & 0 deletions sdk/test/UnitTests/Tools/MockAWSXRayRecorder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Amazon.XRay.Recorder.Core;

namespace Amazon.XRay.Recorder.UnitTests.Tools
{
public class MockAWSXRayRecorder : AWSXRayRecorder
{
public bool IsTracingDisabledValue { get; set; }

public override bool IsTracingDisabled()
{
return IsTracingDisabledValue;
}
}
}