Skip to content

Commit f64386b

Browse files
committed
refactor to lower code duplication
1 parent 4b2a142 commit f64386b

File tree

14 files changed

+126
-141
lines changed

14 files changed

+126
-141
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using Amazon.CDK.AWS.Lambda;
2+
3+
namespace TestUtils;
4+
5+
public class FunctionConstructProps
6+
{
7+
public Architecture Architecture;
8+
public Runtime Runtime;
9+
public string Name;
10+
public string Handler;
11+
public string SourcePath;
12+
public string DistPath;
13+
}

libraries/tests/e2e/functions/TestUtils/TestUtils.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10+
<PackageReference Include="Amazon.CDK.Lib" Version="2.174.0" />
1011
<PackageReference Include="xunit.assert" Version="2.9.2" />
1112
</ItemGroup>
1213

libraries/tests/e2e/functions/core/logging/AOT-Function/src/AOT-Function/AOT-Function.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,9 @@
2525
<ItemGroup>
2626
<ProjectReference Include="..\..\..\..\..\..\..\..\src\AWS.Lambda.Powertools.Logging\AWS.Lambda.Powertools.Logging.csproj" />
2727
</ItemGroup>
28+
<ItemGroup>
29+
<Compile Include="..\..\..\Function\src\Function\TestHelper.cs">
30+
<Link>TestHelper.cs</Link>
31+
</Compile>
32+
</ItemGroup>
2833
</Project>

libraries/tests/e2e/functions/core/logging/AOT-Function/src/AOT-Function/Function.cs

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
using Amazon.Lambda.Core;
22
using Amazon.Lambda.RuntimeSupport;
3-
using Amazon.Lambda.Serialization.SystemTextJson;
43
using System.Text.Json.Serialization;
54
using Amazon.Lambda.APIGatewayEvents;
65
using AWS.Lambda.Powertools.Logging;
76
using AWS.Lambda.Powertools.Logging.Serializers;
7+
using Helpers;
88

99
namespace AOT_Function;
1010

@@ -23,31 +23,7 @@ await LambdaBootstrapBuilder.Create(handler,
2323
CorrelationIdPath = CorrelationIdPaths.ApiGatewayRest)]
2424
public static APIGatewayProxyResponse FunctionHandler(APIGatewayProxyRequest apigwProxyEvent, ILambdaContext context)
2525
{
26-
Logger.LogInformation("Processing request started");
27-
28-
var requestContextRequestId = apigwProxyEvent.RequestContext.RequestId;
29-
var lookupInfo = new Dictionary<string, object>()
30-
{
31-
{"LookupInfo", new Dictionary<string, object>{{ "LookupId", requestContextRequestId }}}
32-
};
33-
34-
var customKeys = new Dictionary<string, string>
35-
{
36-
{"test1", "value1"},
37-
{"test2", "value2"}
38-
};
39-
40-
Logger.AppendKeys(lookupInfo);
41-
Logger.AppendKeys(customKeys);
42-
43-
Logger.LogWarning("Warn with additional keys");
44-
45-
Logger.RemoveKeys("test1", "test2");
46-
47-
var error = new InvalidOperationException("Parent exception message",
48-
new ArgumentNullException(nameof(apigwProxyEvent),
49-
new Exception("Very important nested inner exception message")));
50-
Logger.LogError(error, "Oops something went wrong");
26+
TestHelper.TestMethod(apigwProxyEvent);
5127

5228
return new APIGatewayProxyResponse()
5329
{

libraries/tests/e2e/functions/core/logging/Function/src/Function/Function.cs

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Amazon.Lambda.APIGatewayEvents;
22
using Amazon.Lambda.Core;
33
using AWS.Lambda.Powertools.Logging;
4+
using Helpers;
45

56
// Assembly attribute to enable the Lambda function's JSON input to be converted into a .NET class.
67
[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer))]
@@ -13,32 +14,8 @@ public class Function
1314
CorrelationIdPath = CorrelationIdPaths.ApiGatewayRest)]
1415
public APIGatewayProxyResponse FunctionHandler(APIGatewayProxyRequest apigwProxyEvent, ILambdaContext context)
1516
{
16-
Logger.LogInformation("Processing request started");
17-
18-
var requestContextRequestId = apigwProxyEvent.RequestContext.RequestId;
19-
var lookupInfo = new Dictionary<string, object>()
20-
{
21-
{"LookupInfo", new Dictionary<string, object>{{ "LookupId", requestContextRequestId }}}
22-
};
23-
24-
var customKeys = new Dictionary<string, string>
25-
{
26-
{"test1", "value1"},
27-
{"test2", "value2"}
28-
};
29-
30-
Logger.AppendKeys(lookupInfo);
31-
Logger.AppendKeys(customKeys);
32-
33-
Logger.LogWarning("Warn with additional keys");
34-
35-
Logger.RemoveKeys("test1", "test2");
36-
37-
var error = new InvalidOperationException("Parent exception message",
38-
new ArgumentNullException(nameof(apigwProxyEvent),
39-
new Exception("Very important nested inner exception message")));
40-
Logger.LogError(error, "Oops something went wrong");
41-
17+
TestHelper.TestMethod(apigwProxyEvent);
18+
4219
return new APIGatewayProxyResponse()
4320
{
4421
StatusCode = 200,
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using Amazon.Lambda.APIGatewayEvents;
2+
using AWS.Lambda.Powertools.Logging;
3+
4+
namespace Helpers;
5+
6+
public static class TestHelper
7+
{
8+
public static void TestMethod(APIGatewayProxyRequest apigwProxyEvent)
9+
{
10+
Logger.LogInformation("Processing request started");
11+
12+
var requestContextRequestId = apigwProxyEvent.RequestContext.RequestId;
13+
var lookupInfo = new Dictionary<string, object>()
14+
{
15+
{"LookupInfo", new Dictionary<string, object>{{ "LookupId", requestContextRequestId }}}
16+
};
17+
18+
var customKeys = new Dictionary<string, string>
19+
{
20+
{"test1", "value1"},
21+
{"test2", "value2"}
22+
};
23+
24+
Logger.AppendKeys(lookupInfo);
25+
Logger.AppendKeys(customKeys);
26+
27+
Logger.LogWarning("Warn with additional keys");
28+
29+
Logger.RemoveKeys("test1", "test2");
30+
31+
var error = new InvalidOperationException("Parent exception message",
32+
new ArgumentNullException(nameof(apigwProxyEvent),
33+
new Exception("Very important nested inner exception message")));
34+
Logger.LogError(error, "Oops something went wrong");
35+
}
36+
}

libraries/tests/e2e/functions/core/metrics/AOT-Function/src/AOT-Function/AOT-Function.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,9 @@
2525
<ItemGroup>
2626
<ProjectReference Include="..\..\..\..\..\..\..\..\src\AWS.Lambda.Powertools.Metrics\AWS.Lambda.Powertools.Metrics.csproj" />
2727
</ItemGroup>
28+
<ItemGroup>
29+
<Compile Include="..\..\..\Function\src\Function\TestHelper.cs">
30+
<Link>TestHelper.cs</Link>
31+
</Compile>
32+
</ItemGroup>
2833
</Project>

libraries/tests/e2e/functions/core/metrics/AOT-Function/src/AOT-Function/Function.cs

Lines changed: 2 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,12 @@
44
using System.Text.Json.Serialization;
55
using Amazon.Lambda.APIGatewayEvents;
66
using AWS.Lambda.Powertools.Metrics;
7+
using Helpers;
78

89
namespace AOT_Function;
910

1011
public static class Function
1112
{
12-
private static readonly Dictionary<string, string> DefaultDimensions = new()
13-
{
14-
{"Environment", "Prod"},
15-
{"Another", "One"}
16-
};
17-
1813
private static async Task Main()
1914
{
2015
Func<APIGatewayProxyRequest, ILambdaContext, APIGatewayProxyResponse> handler = FunctionHandler;
@@ -27,33 +22,7 @@ await LambdaBootstrapBuilder.Create(handler,
2722
[Metrics(Namespace = "Test", Service = "Test", CaptureColdStart = true)]
2823
public static APIGatewayProxyResponse FunctionHandler(APIGatewayProxyRequest apigwProxyEvent, ILambdaContext context)
2924
{
30-
Metrics.SetDefaultDimensions(DefaultDimensions);
31-
Metrics.AddMetric("Invocation", 1, MetricUnit.Count);
32-
33-
Metrics.AddDimension("Memory","MemoryLimitInMB");
34-
Metrics.AddMetric("Memory with Environment dimension", context.MemoryLimitInMB, MetricUnit.Megabytes);
35-
36-
// Publish a metric with standard resolution i.e. StorageResolution = 60
37-
Metrics.AddMetric("Standard resolution", 1, MetricUnit.Count, MetricResolution.Standard);
38-
39-
// Publish a metric with high resolution i.e. StorageResolution = 1
40-
Metrics.AddMetric("High resolution", 1, MetricUnit.Count, MetricResolution.High);
41-
42-
// The last parameter (storage resolution) is optional
43-
Metrics.AddMetric("Default resolution", 1, MetricUnit.Count);
44-
45-
Metrics.AddMetadata("RequestId", apigwProxyEvent.RequestContext.RequestId);
46-
47-
Metrics.PushSingleMetric(
48-
metricName: "SingleMetric",
49-
value: 1,
50-
unit: MetricUnit.Count,
51-
nameSpace: "Test",
52-
service: "Test",
53-
defaultDimensions: new Dictionary<string, string>
54-
{
55-
{"FunctionContext", "$LATEST"}
56-
});
25+
TestHelper.TestMethod(apigwProxyEvent, context);
5726

5827
return new APIGatewayProxyResponse()
5928
{

libraries/tests/e2e/functions/core/metrics/Function/src/Function/Function.cs

Lines changed: 2 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -5,47 +5,15 @@
55
// Assembly attribute to enable the Lambda function's JSON input to be converted into a .NET class.
66
[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer))]
77

8-
namespace Function;
8+
namespace Helpers;
99

1010
public class Function
1111
{
12-
private Dictionary<string, string> _defaultDimensions = new()
13-
{
14-
{"Environment", "Prod"},
15-
{"Another", "One"}
16-
};
17-
1812
[Metrics(Namespace = "Test", Service = "Test", CaptureColdStart = true)]
1913
public APIGatewayProxyResponse FunctionHandler(APIGatewayProxyRequest apigwProxyEvent, ILambdaContext context)
2014
{
21-
Metrics.SetDefaultDimensions(_defaultDimensions);
22-
Metrics.AddMetric("Invocation", 1, MetricUnit.Count);
23-
24-
Metrics.AddDimension("Memory","MemoryLimitInMB");
25-
Metrics.AddMetric("Memory with Environment dimension", context.MemoryLimitInMB, MetricUnit.Megabytes);
26-
27-
// Publish a metric with standard resolution i.e. StorageResolution = 60
28-
Metrics.AddMetric("Standard resolution", 1, MetricUnit.Count, MetricResolution.Standard);
29-
30-
// Publish a metric with high resolution i.e. StorageResolution = 1
31-
Metrics.AddMetric("High resolution", 1, MetricUnit.Count, MetricResolution.High);
15+
TestHelper.TestMethod(apigwProxyEvent, context);
3216

33-
// The last parameter (storage resolution) is optional
34-
Metrics.AddMetric("Default resolution", 1, MetricUnit.Count);
35-
36-
Metrics.AddMetadata("RequestId", apigwProxyEvent.RequestContext.RequestId);
37-
38-
Metrics.PushSingleMetric(
39-
metricName: "SingleMetric",
40-
value: 1,
41-
unit: MetricUnit.Count,
42-
nameSpace: "Test",
43-
service: "Test",
44-
defaultDimensions: new Dictionary<string, string>
45-
{
46-
{"FunctionContext", "$LATEST"}
47-
});
48-
4917
return new APIGatewayProxyResponse()
5018
{
5119
StatusCode = 200,
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using Amazon.Lambda.APIGatewayEvents;
2+
using Amazon.Lambda.Core;
3+
using AWS.Lambda.Powertools.Metrics;
4+
5+
namespace Helpers;
6+
7+
public class TestHelper
8+
{
9+
private static readonly Dictionary<string, string> DefaultDimensions = new()
10+
{
11+
{"Environment", "Prod"},
12+
{"Another", "One"}
13+
};
14+
15+
public static void TestMethod(APIGatewayProxyRequest apigwProxyEvent, ILambdaContext context)
16+
{
17+
Metrics.SetDefaultDimensions(DefaultDimensions);
18+
Metrics.AddMetric("Invocation", 1, MetricUnit.Count);
19+
20+
Metrics.AddDimension("Memory","MemoryLimitInMB");
21+
Metrics.AddMetric("Memory with Environment dimension", context.MemoryLimitInMB, MetricUnit.Megabytes);
22+
23+
// Publish a metric with standard resolution i.e. StorageResolution = 60
24+
Metrics.AddMetric("Standard resolution", 1, MetricUnit.Count, MetricResolution.Standard);
25+
26+
// Publish a metric with high resolution i.e. StorageResolution = 1
27+
Metrics.AddMetric("High resolution", 1, MetricUnit.Count, MetricResolution.High);
28+
29+
// The last parameter (storage resolution) is optional
30+
Metrics.AddMetric("Default resolution", 1, MetricUnit.Count);
31+
32+
Metrics.AddMetadata("RequestId", apigwProxyEvent.RequestContext.RequestId);
33+
34+
Metrics.PushSingleMetric(
35+
metricName: "SingleMetric",
36+
value: 1,
37+
unit: MetricUnit.Count,
38+
nameSpace: "Test",
39+
service: "Test",
40+
defaultDimensions: new Dictionary<string, string>
41+
{
42+
{"FunctionContext", "$LATEST"}
43+
});
44+
}
45+
}

libraries/tests/e2e/infra-aot/FunctionConstruct.cs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,17 @@
11
using System.Collections.Generic;
22
using Amazon.CDK.AWS.Lambda;
33
using Constructs;
4+
using TestUtils;
45

56
namespace InfraAot;
67

7-
public class FunctionConstructProps
8-
{
9-
public Architecture Architecture;
10-
public Runtime Runtime;
11-
public string Name;
12-
public string Handler;
13-
public string SourcePath;
14-
public string DistPath;
15-
}
16-
178
public class FunctionConstruct : Construct
189
{
1910
public FunctionConstruct(Construct scope, string id, FunctionConstructProps props) : base(scope, id)
2011
{
2112
var framework = props.Runtime == Runtime.DOTNET_6 ? "net6.0" : "net8.0";
2213
var distPath = $"{props.DistPath}/deploy_{props.Architecture.Name}_{props.Runtime.Name}.zip";
23-
var lambda = new Function(this, id, new FunctionProps
14+
_ = new Function(this, id, new FunctionProps
2415
{
2516
Runtime = props.Runtime,
2617
Architecture = props.Architecture,

libraries/tests/e2e/infra-aot/InfraAot.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,8 @@
1515
<PackageReference Include="Amazon.Jsii.Analyzers" Version="*" PrivateAssets="all" />
1616
-->
1717
</ItemGroup>
18+
19+
<ItemGroup>
20+
<ProjectReference Include="..\functions\TestUtils\TestUtils.csproj" />
21+
</ItemGroup>
1822
</Project>

libraries/tests/e2e/infra/FunctionConstruct.cs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,17 @@
11
using System.Collections.Generic;
22
using Amazon.CDK.AWS.Lambda;
33
using Constructs;
4+
using TestUtils;
45

56
namespace Infra;
67

7-
public class FunctionConstructProps
8-
{
9-
public Architecture Architecture;
10-
public Runtime Runtime;
11-
public string Name;
12-
public string Handler;
13-
public string SourcePath;
14-
public string DistPath;
15-
}
16-
178
public class FunctionConstruct : Construct
189
{
1910
public FunctionConstruct(Construct scope, string id, FunctionConstructProps props) : base(scope, id)
2011
{
2112
var framework = props.Runtime == Runtime.DOTNET_6 ? "net6.0" : "net8.0";
2213
var distPath = $"{props.DistPath}/deploy_{props.Architecture.Name}_{props.Runtime.Name}.zip";
23-
var lambda = new Function(this, id, new FunctionProps
14+
_ = new Function(this, id, new FunctionProps
2415
{
2516
Runtime = props.Runtime,
2617
Architecture = props.Architecture,

libraries/tests/e2e/infra/Infra.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,8 @@
1515
<PackageReference Include="Amazon.Jsii.Analyzers" Version="*" PrivateAssets="all" />
1616
-->
1717
</ItemGroup>
18+
19+
<ItemGroup>
20+
<ProjectReference Include="..\functions\TestUtils\TestUtils.csproj" />
21+
</ItemGroup>
1822
</Project>

0 commit comments

Comments
 (0)