Skip to content

Commit

Permalink
Remove Moq from Instrumentation.AWSLambda.Tests (#1476)
Browse files Browse the repository at this point in the history
  • Loading branch information
ngruson authored Dec 9, 2023
1 parent 7567a9f commit 03ba33e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
// SPDX-License-Identifier: Apache-2.0

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Amazon.Lambda.Core;
using Moq;
using OpenTelemetry.Instrumentation.AWSLambda.Implementation;
using OpenTelemetry.Resources;
using OpenTelemetry.Tests;
Expand Down Expand Up @@ -40,11 +39,11 @@ public AWSLambdaWrapperTests()
[InlineData(true)]
public void TraceSyncWithInputAndReturn(bool setCustomParent)
{
var processor = new Mock<BaseProcessor<Activity>>();
var exportedItems = new List<Activity>();

using (var tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddAWSLambdaConfigurations()
.AddProcessor(processor.Object)
.AddInMemoryExporter(exportedItems)
.Build()!)
{
var parentContext = setCustomParent ? CreateParentContext() : default;
Expand All @@ -53,10 +52,9 @@ public void TraceSyncWithInputAndReturn(bool setCustomParent)
this.AssertResourceAttributes(resource);
}

// SetParentProvider -> OnStart -> OnEnd -> OnForceFlush -> OnShutdown -> Dispose
Assert.Equal(6, processor.Invocations.Count);
Assert.Single(exportedItems);

var activity = (Activity)processor.Invocations[1].Arguments[0];
var activity = exportedItems[0];
this.AssertSpanProperties(activity, setCustomParent ? CustomParentId : XRayParentId);
this.AssertSpanAttributes(activity);
}
Expand All @@ -66,11 +64,11 @@ public void TraceSyncWithInputAndReturn(bool setCustomParent)
[InlineData(true)]
public void TraceSyncWithInputAndNoReturn(bool setCustomParent)
{
var processor = new Mock<BaseProcessor<Activity>>();
var exportedItems = new List<Activity>();

using (var tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddAWSLambdaConfigurations()
.AddProcessor(processor.Object)
.AddInMemoryExporter(exportedItems)
.Build()!)
{
var parentContext = setCustomParent ? CreateParentContext() : default;
Expand All @@ -79,10 +77,9 @@ public void TraceSyncWithInputAndNoReturn(bool setCustomParent)
this.AssertResourceAttributes(resource);
}

// SetParentProvider -> OnStart -> OnEnd -> OnForceFlush -> OnShutdown -> Dispose
Assert.Equal(6, processor.Invocations.Count);
Assert.Single(exportedItems);

var activity = (Activity)processor.Invocations[1].Arguments[0];
var activity = exportedItems[0];
this.AssertSpanProperties(activity, setCustomParent ? CustomParentId : XRayParentId);
this.AssertSpanAttributes(activity);
}
Expand All @@ -92,11 +89,11 @@ public void TraceSyncWithInputAndNoReturn(bool setCustomParent)
[InlineData(true)]
public async Task TraceAsyncWithInputAndReturn(bool setCustomParent)
{
var processor = new Mock<BaseProcessor<Activity>>();
var exportedItems = new List<Activity>();

using (var tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddAWSLambdaConfigurations()
.AddProcessor(processor.Object)
.AddInMemoryExporter(exportedItems)
.Build()!)
{
var parentContext = setCustomParent ? CreateParentContext() : default;
Expand All @@ -105,10 +102,9 @@ public async Task TraceAsyncWithInputAndReturn(bool setCustomParent)
this.AssertResourceAttributes(resource);
}

// SetParentProvider -> OnStart -> OnEnd -> OnForceFlush -> OnShutdown -> Dispose
Assert.Equal(6, processor.Invocations.Count);
Assert.Single(exportedItems);

var activity = (Activity)processor.Invocations[1].Arguments[0];
var activity = exportedItems[0];
this.AssertSpanProperties(activity, setCustomParent ? CustomParentId : XRayParentId);
this.AssertSpanAttributes(activity);
}
Expand All @@ -118,11 +114,11 @@ public async Task TraceAsyncWithInputAndReturn(bool setCustomParent)
[InlineData(true)]
public async Task TraceAsyncWithInputAndNoReturn(bool setCustomParent)
{
var processor = new Mock<BaseProcessor<Activity>>();
var exportedItems = new List<Activity>();

using (var tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddAWSLambdaConfigurations()
.AddProcessor(processor.Object)
.AddInMemoryExporter(exportedItems)
.Build()!)
{
var parentContext = setCustomParent ? CreateParentContext() : default;
Expand All @@ -131,10 +127,9 @@ public async Task TraceAsyncWithInputAndNoReturn(bool setCustomParent)
this.AssertResourceAttributes(resource);
}

// SetParentProvider -> OnStart -> OnEnd -> OnForceFlush -> OnShutdown -> Dispose
Assert.Equal(6, processor.Invocations.Count);
Assert.Single(exportedItems);

var activity = (Activity)processor.Invocations[1].Arguments[0];
var activity = exportedItems[0];
this.AssertSpanProperties(activity, setCustomParent ? CustomParentId : XRayParentId);
this.AssertSpanAttributes(activity);
}
Expand All @@ -144,11 +139,11 @@ public async Task TraceAsyncWithInputAndNoReturn(bool setCustomParent)
[InlineData(true)]
public void TestLambdaHandlerException(bool setCustomParent)
{
var processor = new Mock<BaseProcessor<Activity>>();
var exportedItems = new List<Activity>();

using (var tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddAWSLambdaConfigurations()
.AddProcessor(processor.Object)
.AddInMemoryExporter(exportedItems)
.Build()!)
{
try
Expand All @@ -163,10 +158,9 @@ public void TestLambdaHandlerException(bool setCustomParent)
}
}

// SetParentProvider -> OnStart -> OnEnd -> OnForceFlush -> OnShutdown -> Dispose
Assert.Equal(6, processor.Invocations.Count);
Assert.Single(exportedItems);

var activity = (Activity)processor.Invocations[1].Arguments[0];
var activity = exportedItems[0];
this.AssertSpanProperties(activity, setCustomParent ? CustomParentId : XRayParentId);
this.AssertSpanAttributes(activity);
this.AssertSpanException(activity);
Expand All @@ -177,23 +171,19 @@ public void TestLambdaHandlerNotSampled()
{
Environment.SetEnvironmentVariable("_X_AMZN_TRACE_ID", "Root=1-5759e988-bd862e3fe1be46a994272793;Parent=53995c3f42cd8ad8;Sampled=0");

var processor = new Mock<BaseProcessor<Activity>>();
var exportedItems = new List<Activity>();

using (var tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddAWSLambdaConfigurations()
.AddProcessor(processor.Object)
.AddInMemoryExporter(exportedItems)
.Build()!)
{
var result = AWSLambdaWrapper.Trace(tracerProvider, this.sampleHandlers.SampleHandlerSyncInputAndReturn, "TestStream", this.sampleLambdaContext);
var resource = tracerProvider.GetResource();
this.AssertResourceAttributes(resource);
}

// SetParentProvider -> OnForceFlush -> OnShutdown -> Dispose
Assert.Equal(4, processor.Invocations.Count);

var activities = processor.Invocations.Where(i => i.Method.Name == "OnEnd").Select(i => i.Arguments[0]).Cast<Activity>().ToArray();
Assert.True(activities.Length == 0);
Assert.Empty(exportedItems);
}

[Fact]
Expand All @@ -206,7 +196,7 @@ public void OnFunctionStart_NoParent_ActivityCreated()
.AddAWSLambdaConfigurations()
.Build())
{
activity = AWSLambdaWrapper.OnFunctionStart("test-input", new Mock<ILambdaContext>().Object);
activity = AWSLambdaWrapper.OnFunctionStart("test-input", new SampleLambdaContext());
}

Assert.NotNull(activity);
Expand All @@ -222,7 +212,7 @@ public void OnFunctionStart_NoSampledAndAwsXRayContextExtractionDisabled_Activit
.AddAWSLambdaConfigurations(c => c.DisableAwsXRayContextExtraction = true)
.Build())
{
activity = AWSLambdaWrapper.OnFunctionStart("test-input", new Mock<ILambdaContext>().Object);
activity = AWSLambdaWrapper.OnFunctionStart("test-input", new SampleLambdaContext());
}

Assert.NotNull(activity);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System.Diagnostics;
using System.Linq;
using Amazon.Lambda.APIGatewayEvents;
using Moq;
using OpenTelemetry.Instrumentation.AWSLambda.Implementation;
using OpenTelemetry.Trace;
using Xunit;
Expand Down Expand Up @@ -170,10 +169,8 @@ public void SetHttpTagsFromResult_APIGatewayProxyResponse_SetsCorrectTags()
{
StatusCode = 200,
};
var activityProcessor = new Mock<BaseProcessor<Activity>>();

using var tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddProcessor(activityProcessor.Object)
.AddSource("TestActivitySource")
.Build();

Expand Down Expand Up @@ -201,10 +198,8 @@ public void SetHttpTagsFromResult_APIGatewayHttpApiV2ProxyResponse_SetsCorrectTa
{
StatusCode = 200,
};
var activityProcessor = new Mock<BaseProcessor<Activity>>();

using var tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddProcessor(activityProcessor.Object)
.AddSource("TestActivitySource")
.Build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Moq" Version="$(MoqPkgVer)" />
<ProjectReference Include="$(RepoRoot)\src\OpenTelemetry.Instrumentation.AWSLambda\OpenTelemetry.Instrumentation.AWSLambda.csproj" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="$(RepoRoot)\src\OpenTelemetry.Instrumentation.AWSLambda\OpenTelemetry.Instrumentation.AWSLambda.csproj" />
<PackageReference Include="OpenTelemetry.Exporter.InMemory" Version="$(OpenTelemetryExporterInMemoryPkgVer)" />
</ItemGroup>

<ItemGroup>
Expand Down

0 comments on commit 03ba33e

Please sign in to comment.