From db8bac8ff924479fbf61c679bbc748d1923698d6 Mon Sep 17 00:00:00 2001
From: Henrique Graca <999396+hjgraca@users.noreply.github.com>
Date: Sat, 17 Feb 2024 20:05:26 +0000
Subject: [PATCH 1/4] first try to AOT for Logging
---
libraries/AWS.Lambda.Powertools.sln | 15 ++
...S.Lambda.Powertools.BatchProcessing.csproj | 1 +
.../AWS.Lambda.Powertools.Common.csproj | 1 +
.../Aspects/UniversalWrapperAspect.cs | 40 ++---
.../Core/PowertoolsSourceGenerationContext.cs | 43 +++++
.../AWS.Lambda.Powertools.Logging.csproj | 4 +
.../Internal/Converters/ExceptionConverter.cs | 6 +-
.../Internal/LoggingAspectHandler.cs | 27 +++-
.../Internal/PowertoolsLambdaContext.cs | 151 ++++++++++++++++++
.../Internal/PowertoolsLogger.cs | 86 +++++++---
.../LogEntryLambdaContext.cs | 22 ++-
.../AWS.Lambda.Powertools.Metrics.csproj | 2 +
.../Internal}/PowertoolsLambdaContext.cs | 112 +++++++------
.../AWS.Lambda.Powertools.Parameters.csproj | 1 +
.../AWS.Lambda.Powertools.Tracing.csproj | 1 +
libraries/src/Directory.Build.props | 2 +
libraries/src/Directory.Packages.props | 4 +-
....Lambda.Powertools.AotCompatibility.csproj | 43 +++++
.../GlobalUsings.cs | 1 +
.../Handlers/Handler.cs | 37 +++++
.../UnitTest1.cs | 28 ++++
...AWS.Lambda.Powertools.Logging.Tests.csproj | 1 +
.../Handlers/ExceptionFunctionHandler.cs | 2 +-
.../LogFormatterTest.cs | 1 +
.../PowertoolsLambdaContextTest.cs | 31 ++--
.../PowertoolsLoggerTest.cs | 3 +-
26 files changed, 549 insertions(+), 116 deletions(-)
create mode 100644 libraries/src/AWS.Lambda.Powertools.Common/Core/PowertoolsSourceGenerationContext.cs
create mode 100644 libraries/src/AWS.Lambda.Powertools.Logging/Internal/PowertoolsLambdaContext.cs
rename libraries/src/{AWS.Lambda.Powertools.Common/Core => AWS.Lambda.Powertools.Metrics/Internal}/PowertoolsLambdaContext.cs (50%)
create mode 100644 libraries/tests/AWS.Lambda.Powertools.AotCompatibility/AWS.Lambda.Powertools.AotCompatibility.csproj
create mode 100644 libraries/tests/AWS.Lambda.Powertools.AotCompatibility/GlobalUsings.cs
create mode 100644 libraries/tests/AWS.Lambda.Powertools.AotCompatibility/Handlers/Handler.cs
create mode 100644 libraries/tests/AWS.Lambda.Powertools.AotCompatibility/UnitTest1.cs
rename libraries/tests/{AWS.Lambda.Powertools.Common.Tests/Core => AWS.Lambda.Powertools.Logging.Tests}/PowertoolsLambdaContextTest.cs (78%)
diff --git a/libraries/AWS.Lambda.Powertools.sln b/libraries/AWS.Lambda.Powertools.sln
index 62c1cec1e..d3ed829b9 100644
--- a/libraries/AWS.Lambda.Powertools.sln
+++ b/libraries/AWS.Lambda.Powertools.sln
@@ -35,6 +35,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AWS.Lambda.Powertools.Param
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AWS.Lambda.Powertools.Parameters.Tests", "tests\AWS.Lambda.Powertools.Parameters.Tests\AWS.Lambda.Powertools.Parameters.Tests.csproj", "{386A9769-59BF-4BE3-99D4-A9603E300729}"
EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AWS.Lambda.Powertools.AotCompatibility", "tests\AWS.Lambda.Powertools.AotCompatibility\AWS.Lambda.Powertools.AotCompatibility.csproj", "{2B782D22-675D-42D4-A041-90A27275A403}"
+EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@@ -216,6 +218,18 @@ Global
{386A9769-59BF-4BE3-99D4-A9603E300729}.Release|x64.Build.0 = Release|Any CPU
{386A9769-59BF-4BE3-99D4-A9603E300729}.Release|x86.ActiveCfg = Release|Any CPU
{386A9769-59BF-4BE3-99D4-A9603E300729}.Release|x86.Build.0 = Release|Any CPU
+ {2B782D22-675D-42D4-A041-90A27275A403}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {2B782D22-675D-42D4-A041-90A27275A403}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {2B782D22-675D-42D4-A041-90A27275A403}.Debug|x64.ActiveCfg = Debug|Any CPU
+ {2B782D22-675D-42D4-A041-90A27275A403}.Debug|x64.Build.0 = Debug|Any CPU
+ {2B782D22-675D-42D4-A041-90A27275A403}.Debug|x86.ActiveCfg = Debug|Any CPU
+ {2B782D22-675D-42D4-A041-90A27275A403}.Debug|x86.Build.0 = Debug|Any CPU
+ {2B782D22-675D-42D4-A041-90A27275A403}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {2B782D22-675D-42D4-A041-90A27275A403}.Release|Any CPU.Build.0 = Release|Any CPU
+ {2B782D22-675D-42D4-A041-90A27275A403}.Release|x64.ActiveCfg = Release|Any CPU
+ {2B782D22-675D-42D4-A041-90A27275A403}.Release|x64.Build.0 = Release|Any CPU
+ {2B782D22-675D-42D4-A041-90A27275A403}.Release|x86.ActiveCfg = Release|Any CPU
+ {2B782D22-675D-42D4-A041-90A27275A403}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
@@ -233,5 +247,6 @@ Global
{F8B4100F-4014-4A1E-8130-D281453B79ED} = {73C9B1E5-3893-47E8-B373-17E5F5D7E6F5}
{12B940EF-A5D3-459D-BD36-A603834D1F7D} = {1CFF5568-8486-475F-81F6-06105C437528}
{3E1D77BD-70AF-4767-B00A-4A321D5AB2C3} = {1CFF5568-8486-475F-81F6-06105C437528}
+ {2B782D22-675D-42D4-A041-90A27275A403} = {1CFF5568-8486-475F-81F6-06105C437528}
EndGlobalSection
EndGlobal
diff --git a/libraries/src/AWS.Lambda.Powertools.BatchProcessing/AWS.Lambda.Powertools.BatchProcessing.csproj b/libraries/src/AWS.Lambda.Powertools.BatchProcessing/AWS.Lambda.Powertools.BatchProcessing.csproj
index ed58f6f69..c766cac7f 100644
--- a/libraries/src/AWS.Lambda.Powertools.BatchProcessing/AWS.Lambda.Powertools.BatchProcessing.csproj
+++ b/libraries/src/AWS.Lambda.Powertools.BatchProcessing/AWS.Lambda.Powertools.BatchProcessing.csproj
@@ -13,5 +13,6 @@
+
diff --git a/libraries/src/AWS.Lambda.Powertools.Common/AWS.Lambda.Powertools.Common.csproj b/libraries/src/AWS.Lambda.Powertools.Common/AWS.Lambda.Powertools.Common.csproj
index 9d3e66822..3084c61fe 100644
--- a/libraries/src/AWS.Lambda.Powertools.Common/AWS.Lambda.Powertools.Common.csproj
+++ b/libraries/src/AWS.Lambda.Powertools.Common/AWS.Lambda.Powertools.Common.csproj
@@ -12,6 +12,7 @@
+
diff --git a/libraries/src/AWS.Lambda.Powertools.Common/Aspects/UniversalWrapperAspect.cs b/libraries/src/AWS.Lambda.Powertools.Common/Aspects/UniversalWrapperAspect.cs
index fedf45031..d076909f8 100644
--- a/libraries/src/AWS.Lambda.Powertools.Common/Aspects/UniversalWrapperAspect.cs
+++ b/libraries/src/AWS.Lambda.Powertools.Common/Aspects/UniversalWrapperAspect.cs
@@ -1,12 +1,12 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
- *
+ *
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
- *
+ *
* http://aws.amazon.com/apache2.0
- *
+ *
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
@@ -15,6 +15,7 @@
using System;
using System.Collections.Generic;
+using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
@@ -32,19 +33,19 @@ public class UniversalWrapperAspect
///
/// The delegate cache
///
- private static readonly Dictionary _delegateCache = new();
+ private static readonly Dictionary DelegateCache = new();
///
/// The asynchronous generic handler
///
- private static readonly MethodInfo _asyncGenericHandler =
+ private static readonly MethodInfo AsyncGenericHandler =
typeof(UniversalWrapperAttribute).GetMethod(nameof(UniversalWrapperAttribute.WrapAsync),
BindingFlags.NonPublic | BindingFlags.Instance);
///
/// The synchronize generic handler
///
- private static readonly MethodInfo _syncGenericHandler =
+ private static readonly MethodInfo SyncGenericHandler =
typeof(UniversalWrapperAttribute).GetMethod(nameof(UniversalWrapperAttribute.WrapSync),
BindingFlags.NonPublic | BindingFlags.Instance);
@@ -93,6 +94,7 @@ public object Handle(
/// Type of the return.
/// The wrappers.
/// Handler.
+ [UnconditionalSuppressMessage("AOT", "IL3050:Calling members annotated with 'RequiresDynamicCodeAttribute' may break functionality when AOT compiling.", Justification = "")]
private static Handler CreateMethodHandler(Type returnType, IEnumerable wrappers)
{
var targetParam = Expression.Parameter(typeof(Func