Skip to content

Commit

Permalink
Remove Moq from Instrumentation.AWS.Tests (#1475)
Browse files Browse the repository at this point in the history
  • Loading branch information
ngruson authored Dec 9, 2023
1 parent 03ba33e commit d4b739a
Show file tree
Hide file tree
Showing 5 changed files with 239 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using System.Diagnostics;
using Amazon.Runtime;
using Amazon.Runtime.Internal;
using Moq;
using OpenTelemetry.Context.Propagation;
using OpenTelemetry.Instrumentation.AWS.Implementation;
using OpenTelemetry.Trace;
Expand Down Expand Up @@ -35,18 +34,12 @@ public void AddAttributes_ParametersCollectionSizeReachesLimit_TraceDataNotInjec
var parameters = new ParameterCollection();
parameters.AddStringParameters(serviceType, originalRequest);

var request = new Mock<IRequest>();
request.Setup(x => x.ParameterCollection)
.Returns(parameters);
var request = new TestRequest(parameters);

var context = new Mock<IRequestContext>();
context.Setup(x => x.OriginalRequest)
.Returns(originalRequest);
context.Setup(x => x.Request)
.Returns(request.Object);
var context = new TestRequestContext(originalRequest, request);

var addAttributes = TestsHelper.CreateAddAttributesAction(serviceType, context.Object);
addAttributes?.Invoke(context.Object, AWSMessagingUtils.InjectIntoDictionary(CreatePropagationContext()));
var addAttributes = TestsHelper.CreateAddAttributesAction(serviceType, context);
addAttributes?.Invoke(context, AWSMessagingUtils.InjectIntoDictionary(CreatePropagationContext()));

Assert.Equal(30, parameters.Count);
}
Expand All @@ -58,25 +51,18 @@ public void AddAttributes_ParametersCollection_TraceDataInjected(string serviceT
{
var expectedParameters = new List<KeyValuePair<string, string>>()
{
new KeyValuePair<string, string>("traceparent", $"00-{TraceId}-{ParentId}-00"),
new KeyValuePair<string, string>("tracestate", "trace-state"),
new("traceparent", $"00-{TraceId}-{ParentId}-00"),
new("tracestate", "trace-state"),
};

AmazonWebServiceRequest originalRequest = TestsHelper.CreateOriginalRequest(serviceType, 0);
var parameters = new ParameterCollection();

var request = new Mock<IRequest>();
request.Setup(x => x.ParameterCollection)
.Returns(parameters);
var request = new TestRequest(parameters);
var context = new TestRequestContext(originalRequest, request);

var context = new Mock<IRequestContext>();
context.Setup(x => x.OriginalRequest)
.Returns(originalRequest);
context.Setup(x => x.Request)
.Returns(request.Object);

var addAttributes = TestsHelper.CreateAddAttributesAction(serviceType, context.Object);
addAttributes?.Invoke(context.Object, AWSMessagingUtils.InjectIntoDictionary(CreatePropagationContext()));
var addAttributes = TestsHelper.CreateAddAttributesAction(serviceType, context);
addAttributes?.Invoke(context, AWSMessagingUtils.InjectIntoDictionary(CreatePropagationContext()));

TestsHelper.AssertStringParameters(serviceType, expectedParameters, parameters);
}
Expand All @@ -88,27 +74,21 @@ public void AddAttributes_ParametersCollectionWithCustomParameter_TraceDataInjec
{
var expectedParameters = new List<KeyValuePair<string, string>>()
{
new KeyValuePair<string, string>("name1", "value1"),
new KeyValuePair<string, string>("traceparent", $"00-{TraceId}-{ParentId}-00"),
new KeyValuePair<string, string>("tracestate", "trace-state"),
new("name1", "value1"),
new("traceparent", $"00-{TraceId}-{ParentId}-00"),
new("tracestate", "trace-state"),
};

AmazonWebServiceRequest originalRequest = TestsHelper.CreateOriginalRequest(serviceType, 1);
var parameters = new ParameterCollection();
parameters.AddStringParameters(serviceType, originalRequest);

var request = new Mock<IRequest>();
request.Setup(x => x.ParameterCollection)
.Returns(parameters);
var request = new TestRequest(parameters);

var context = new Mock<IRequestContext>();
context.Setup(x => x.OriginalRequest)
.Returns(originalRequest);
context.Setup(x => x.Request)
.Returns(request.Object);
var context = new TestRequestContext(originalRequest, request);

var addAttributes = TestsHelper.CreateAddAttributesAction(serviceType, context.Object);
addAttributes?.Invoke(context.Object, AWSMessagingUtils.InjectIntoDictionary(CreatePropagationContext()));
var addAttributes = TestsHelper.CreateAddAttributesAction(serviceType, context);
addAttributes?.Invoke(context, AWSMessagingUtils.InjectIntoDictionary(CreatePropagationContext()));

TestsHelper.AssertStringParameters(serviceType, expectedParameters, parameters);
}
Expand All @@ -124,7 +104,7 @@ public void AddAttributes_ParametersCollectionWithTraceParent_TraceStateNotInjec

var expectedParameters = new List<KeyValuePair<string, string>>()
{
new KeyValuePair<string, string>("traceparent", $"00-{TraceId}-{ParentId}-00"),
new("traceparent", $"00-{TraceId}-{ParentId}-00"),
};

AmazonWebServiceRequest originalRequest = TestsHelper.CreateOriginalRequest(serviceType, 0);
Expand All @@ -133,18 +113,11 @@ public void AddAttributes_ParametersCollectionWithTraceParent_TraceStateNotInjec
var parameters = new ParameterCollection();
parameters.AddStringParameters(serviceType, originalRequest);

var request = new Mock<IRequest>();
request.Setup(x => x.ParameterCollection)
.Returns(parameters);

var context = new Mock<IRequestContext>();
context.Setup(x => x.OriginalRequest)
.Returns(originalRequest);
context.Setup(x => x.Request)
.Returns(request.Object);
var request = new TestRequest(parameters);
var context = new TestRequestContext(originalRequest, request);

var addAttributes = TestsHelper.CreateAddAttributesAction(serviceType, context.Object);
addAttributes?.Invoke(context.Object, AWSMessagingUtils.InjectIntoDictionary(CreatePropagationContext()));
var addAttributes = TestsHelper.CreateAddAttributesAction(serviceType, context);
addAttributes?.Invoke(context, AWSMessagingUtils.InjectIntoDictionary(CreatePropagationContext()));

TestsHelper.AssertStringParameters(serviceType, expectedParameters, parameters);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

<ItemGroup>
<PackageReference Include="AWSSDK.DynamoDBv2" Version="3.7.100" />
<PackageReference Include="Moq" Version="$(MoqPkgVer)" />
<PackageReference Include="OpenTelemetry.Exporter.InMemory" Version="$(OpenTelemetryCoreLatestVersion)" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
using Amazon.Runtime;
using Amazon.SQS;
using Amazon.SQS.Model;
using Moq;
using OpenTelemetry.Instrumentation.AWS.Tests.Tools;
using OpenTelemetry.Trace;
using Xunit;
Expand All @@ -25,34 +24,33 @@ public class TestAWSClientInstrumentation
[Fact]
public void TestDDBScanSuccessful()
{
var processor = new Mock<BaseProcessor<Activity>>();
var exportedItems = new List<Activity>();

var parent = new Activity("parent").Start();

using (Sdk.CreateTracerProviderBuilder()
.SetSampler(new AlwaysOnSampler())
.AddXRayTraceId()
.AddAWSInstrumentation()
.AddProcessor(processor.Object)
.AddInMemoryExporter(exportedItems)
.Build())
{
var ddb = new AmazonDynamoDBClient(new AnonymousAWSCredentials(), RegionEndpoint.USEast1);
string requestId = @"fakerequ-esti-dfak-ereq-uestidfakere";
CustomResponses.SetResponse(ddb, null, requestId, true);
var scan_request = new ScanRequest();

scan_request.TableName = "SampleProduct";
scan_request.AttributesToGet = new List<string>() { "Id", "Name" };
var scan_request = new ScanRequest
{
TableName = "SampleProduct",
AttributesToGet = new List<string> { "Id", "Name" },
};
#if NETFRAMEWORK
ddb.Scan(scan_request);
#else
ddb.ScanAsync(scan_request).Wait();
#endif
var count = processor.Invocations.Count;

Assert.Equal(3, count);
Assert.Single(exportedItems);

Activity awssdk_activity = (Activity)processor.Invocations[2].Arguments[0];
Activity awssdk_activity = exportedItems[0];

this.ValidateAWSActivity(awssdk_activity, parent);
this.ValidateDynamoActivityTags(awssdk_activity);
Expand All @@ -65,34 +63,33 @@ public void TestDDBScanSuccessful()
[Fact]
public void TestDDBSubtypeScanSuccessful()
{
var processor = new Mock<BaseProcessor<Activity>>();
var exportedItems = new List<Activity>();

var parent = new Activity("parent").Start();

using (Sdk.CreateTracerProviderBuilder()
.SetSampler(new AlwaysOnSampler())
.AddXRayTraceId()
.AddAWSInstrumentation()
.AddProcessor(processor.Object)
.AddInMemoryExporter(exportedItems)
.Build())
{
var ddb = new TestAmazonDynamoDBClient(new AnonymousAWSCredentials(), RegionEndpoint.USEast1);
string requestId = @"fakerequ-esti-dfak-ereq-uestidfakere";
CustomResponses.SetResponse(ddb, null, requestId, true);
var scan_request = new ScanRequest();

scan_request.TableName = "SampleProduct";
scan_request.AttributesToGet = new List<string>() { "Id", "Name" };
var scan_request = new ScanRequest
{
TableName = "SampleProduct",
AttributesToGet = new List<string>() { "Id", "Name" },
};
#if NETFRAMEWORK
ddb.Scan(scan_request);
#else
ddb.ScanAsync(scan_request).Wait();
#endif
var count = processor.Invocations.Count;
Assert.Single(exportedItems);

Assert.Equal(3, count);

Activity awssdk_activity = (Activity)processor.Invocations[2].Arguments[0];
Activity awssdk_activity = exportedItems[0];

this.ValidateAWSActivity(awssdk_activity, parent);
this.ValidateDynamoActivityTags(awssdk_activity);
Expand All @@ -109,15 +106,15 @@ public void TestDDBScanUnsuccessful()
public async Task TestDDBScanUnsuccessful()
#endif
{
var processor = new Mock<BaseProcessor<Activity>>();
var exportedItems = new List<Activity>();

var parent = new Activity("parent").Start();

using (Sdk.CreateTracerProviderBuilder()
.SetSampler(new AlwaysOnSampler())
.AddXRayTraceId()
.AddAWSInstrumentation()
.AddProcessor(processor.Object)
.AddInMemoryExporter(exportedItems)
.Build())
{
var ddb = new AmazonDynamoDBClient(new AnonymousAWSCredentials(), RegionEndpoint.USEast1);
Expand All @@ -126,10 +123,11 @@ public async Task TestDDBScanUnsuccessful()
amazonServiceException.StatusCode = System.Net.HttpStatusCode.NotFound;
amazonServiceException.RequestId = requestId;
CustomResponses.SetResponse(ddb, (request) => { throw amazonServiceException; });
var scan_request = new ScanRequest();

scan_request.TableName = "SampleProduct";
scan_request.AttributesToGet = new List<string>() { "Id", "Name" };
var scan_request = new ScanRequest
{
TableName = "SampleProduct",
AttributesToGet = new List<string>() { "Id", "Name" },
};

try
{
Expand All @@ -141,10 +139,9 @@ public async Task TestDDBScanUnsuccessful()
}
catch (AmazonServiceException)
{
var count = processor.Invocations.Count;
Assert.Equal(3, count);
Assert.Single(exportedItems);

Activity awssdk_activity = (Activity)processor.Invocations[2].Arguments[0];
Activity awssdk_activity = exportedItems[0];

this.ValidateAWSActivity(awssdk_activity, parent);
this.ValidateDynamoActivityTags(awssdk_activity);
Expand All @@ -159,15 +156,15 @@ public async Task TestDDBScanUnsuccessful()
[Fact]
public void TestSQSSendMessageSuccessful()
{
var processor = new Mock<BaseProcessor<Activity>>();
var exportedItems = new List<Activity>();

var parent = new Activity("parent").Start();

using (Sdk.CreateTracerProviderBuilder()
.AddXRayTraceId()
.SetSampler(new AlwaysOnSampler())
.AddAWSInstrumentation()
.AddProcessor(processor.Object)
.AddInMemoryExporter(exportedItems)
.Build())
{
var sqs = new AmazonSQSClient(new AnonymousAWSCredentials(), RegionEndpoint.USEast1);
Expand All @@ -183,10 +180,8 @@ public void TestSQSSendMessageSuccessful()
#else
sqs.SendMessageAsync(send_msg_req).Wait();
#endif

var count = processor.Invocations.Count;
Assert.Equal(3, count);
Activity awssdk_activity = (Activity)processor.Invocations[2].Arguments[0];
Assert.Single(exportedItems);
Activity awssdk_activity = exportedItems[0];

this.ValidateAWSActivity(awssdk_activity, parent);
this.ValidateSqsActivityTags(awssdk_activity);
Expand Down
Loading

0 comments on commit d4b739a

Please sign in to comment.