From a59fe9349a1abfdd3e27a4f4e0d2ea4c298ad61e Mon Sep 17 00:00:00 2001
From: Henrique Graca <999396+hjgraca@users.noreply.github.com>
Date: Wed, 21 Feb 2024 16:17:34 +0000
Subject: [PATCH 1/9] Metrics AOT ready, New SerializationContext
---
.../AWS.Lambda.Powertools.Metrics.csproj | 6 +--
.../Model/MetricResolution.cs | 1 -
.../Model/MetricUnit.cs | 4 ++
.../Model/RootNode.cs | 5 +++
.../Serializer/MetricsSerializationContext.cs | 40 +++++++++++++++++++
.../Serializer/StringEnumConverter.cs | 6 ++-
libraries/src/Directory.Build.props | 8 ++++
7 files changed, 64 insertions(+), 6 deletions(-)
create mode 100644 libraries/src/AWS.Lambda.Powertools.Metrics/Serializer/MetricsSerializationContext.cs
diff --git a/libraries/src/AWS.Lambda.Powertools.Metrics/AWS.Lambda.Powertools.Metrics.csproj b/libraries/src/AWS.Lambda.Powertools.Metrics/AWS.Lambda.Powertools.Metrics.csproj
index 77110d09..128169c8 100644
--- a/libraries/src/AWS.Lambda.Powertools.Metrics/AWS.Lambda.Powertools.Metrics.csproj
+++ b/libraries/src/AWS.Lambda.Powertools.Metrics/AWS.Lambda.Powertools.Metrics.csproj
@@ -5,14 +5,12 @@
Powertools for AWS Lambda (.NET) - Metrics package.
AWS.Lambda.Powertools.Metrics
AWS.Lambda.Powertools.Metrics
+ 1.9.3
-
-
-
-
+
diff --git a/libraries/src/AWS.Lambda.Powertools.Metrics/Model/MetricResolution.cs b/libraries/src/AWS.Lambda.Powertools.Metrics/Model/MetricResolution.cs
index 3c6536ff..2112c1c5 100644
--- a/libraries/src/AWS.Lambda.Powertools.Metrics/Model/MetricResolution.cs
+++ b/libraries/src/AWS.Lambda.Powertools.Metrics/Model/MetricResolution.cs
@@ -6,7 +6,6 @@ namespace AWS.Lambda.Powertools.Metrics;
///
/// Enum MetricResolution
///
-// [JsonConverter(typeof(StringEnumConverter))]
public enum MetricResolution
{
///
diff --git a/libraries/src/AWS.Lambda.Powertools.Metrics/Model/MetricUnit.cs b/libraries/src/AWS.Lambda.Powertools.Metrics/Model/MetricUnit.cs
index fd4b9760..132b10f3 100644
--- a/libraries/src/AWS.Lambda.Powertools.Metrics/Model/MetricUnit.cs
+++ b/libraries/src/AWS.Lambda.Powertools.Metrics/Model/MetricUnit.cs
@@ -21,7 +21,11 @@ namespace AWS.Lambda.Powertools.Metrics;
///
/// Enum MetricUnit
///
+#if NET8_0_OR_GREATER
+[JsonConverter(typeof(JsonStringEnumConverter))]
+#else
[JsonConverter(typeof(StringEnumConverter))]
+#endif
public enum MetricUnit
{
///
diff --git a/libraries/src/AWS.Lambda.Powertools.Metrics/Model/RootNode.cs b/libraries/src/AWS.Lambda.Powertools.Metrics/Model/RootNode.cs
index a7783819..496606cd 100644
--- a/libraries/src/AWS.Lambda.Powertools.Metrics/Model/RootNode.cs
+++ b/libraries/src/AWS.Lambda.Powertools.Metrics/Model/RootNode.cs
@@ -65,6 +65,11 @@ public string Serialize()
{
if (string.IsNullOrWhiteSpace(AWS.GetNamespace())) throw new SchemaValidationException("namespace");
+#if NET8_0_OR_GREATER
+
+ return JsonSerializer.Serialize(this, typeof(RootNode), MetricsSerializationContext.Default);
+#else
return JsonSerializer.Serialize(this);
+#endif
}
}
\ No newline at end of file
diff --git a/libraries/src/AWS.Lambda.Powertools.Metrics/Serializer/MetricsSerializationContext.cs b/libraries/src/AWS.Lambda.Powertools.Metrics/Serializer/MetricsSerializationContext.cs
new file mode 100644
index 00000000..e8a421ac
--- /dev/null
+++ b/libraries/src/AWS.Lambda.Powertools.Metrics/Serializer/MetricsSerializationContext.cs
@@ -0,0 +1,40 @@
+/*
+ * 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
+ * permissions and limitations under the License.
+ */
+
+using System.Collections.Generic;
+using System.Text.Json.Serialization;
+
+namespace AWS.Lambda.Powertools.Metrics;
+
+#if NET8_0_OR_GREATER
+///
+/// Source generator for Metrics types
+///
+[JsonSerializable(typeof(string))]
+[JsonSerializable(typeof(double))]
+[JsonSerializable(typeof(List))]
+[JsonSerializable(typeof(MetricUnit))]
+[JsonSerializable(typeof(MetricDefinition))]
+[JsonSerializable(typeof(DimensionSet))]
+[JsonSerializable(typeof(Metadata))]
+[JsonSerializable(typeof(MetricDirective))]
+[JsonSerializable(typeof(MetricResolution))]
+[JsonSerializable(typeof(MetricsContext))]
+[JsonSerializable(typeof(RootNode))]
+public partial class MetricsSerializationContext : JsonSerializerContext
+{
+
+}
+#endif
\ No newline at end of file
diff --git a/libraries/src/AWS.Lambda.Powertools.Metrics/Serializer/StringEnumConverter.cs b/libraries/src/AWS.Lambda.Powertools.Metrics/Serializer/StringEnumConverter.cs
index 28c0d54b..242ee90b 100644
--- a/libraries/src/AWS.Lambda.Powertools.Metrics/Serializer/StringEnumConverter.cs
+++ b/libraries/src/AWS.Lambda.Powertools.Metrics/Serializer/StringEnumConverter.cs
@@ -22,9 +22,12 @@
namespace AWS.Lambda.Powertools.Metrics;
+#if NET6_0
+
///
/// Class StringEnumConverter.
/// Implements the
+/// .NET 6 only
///
///
public class StringEnumConverter : JsonConverterFactory
@@ -95,4 +98,5 @@ public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializer
_allowIntegerValues).CreateConverter(typeToConvert, options)
: _baseConverter.CreateConverter(typeToConvert, options);
}
-}
\ No newline at end of file
+}
+#endif
\ No newline at end of file
diff --git a/libraries/src/Directory.Build.props b/libraries/src/Directory.Build.props
index 0260344d..3a1d85f2 100644
--- a/libraries/src/Directory.Build.props
+++ b/libraries/src/Directory.Build.props
@@ -19,6 +19,14 @@
+
+
+
+ true
+ true
+ true
+
+
From 96239ea817ed12d3f3856f66cb9c9ab60640a712 Mon Sep 17 00:00:00 2001
From: Henrique <999396+hjgraca@users.noreply.github.com>
Date: Thu, 27 Jun 2024 00:34:16 +0100
Subject: [PATCH 2/9] Metrics AOT Support. REmove dependency from
UniversalWrapperAspect.cs. Refactor EMFValidationTests.cs no more mocks
---
.../Aspects/UniversalWrapperAspect.cs | 40 +-
.../Core/ISystemWrapper.cs | 13 +
.../Core/SystemWrapper.cs | 6 +
.../AWS.Lambda.Powertools.Metrics.csproj | 2 -
.../Internal/MetricsAspect.cs | 136 ++++
.../Internal/MetricsAspectHandler.cs | 145 ----
.../MetricsAttribute.cs | 42 +-
.../ClearDimensionsTests.cs | 33 +-
.../EMFValidationTests.cs | 637 +++---------------
.../Handlers/ExceptionFunctionHandlerTests.cs | 2 +-
.../Handlers/FunctionHandler.cs | 110 ++-
11 files changed, 418 insertions(+), 748 deletions(-)
create mode 100644 libraries/src/AWS.Lambda.Powertools.Metrics/Internal/MetricsAspect.cs
delete mode 100644 libraries/src/AWS.Lambda.Powertools.Metrics/Internal/MetricsAspectHandler.cs
diff --git a/libraries/src/AWS.Lambda.Powertools.Common/Aspects/UniversalWrapperAspect.cs b/libraries/src/AWS.Lambda.Powertools.Common/Aspects/UniversalWrapperAspect.cs
index 8061ef7d..c4b01468 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);
@@ -94,6 +95,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