Skip to content

Commit a234e37

Browse files
authored
Merge branch 'develop' into idempotency-inprogressexpiration
2 parents 5729fd2 + 1378d75 commit a234e37

File tree

38 files changed

+952
-96
lines changed

38 files changed

+952
-96
lines changed

.github/dependabot.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,11 @@ updates:
1313
commit-message:
1414
prefix: chore
1515
include: scope
16+
- package-ecosystem: "nuget" # See documentation for possible values
17+
directory: "/examples/" # Location of package manifests
18+
schedule:
19+
interval: "weekly"
20+
target-branch: "develop"
21+
commit-message:
22+
prefix: chore
23+
include: scope

docs/core/logging.md

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,3 +515,95 @@ Below are some output examples for different casing.
515515
"function_request_id": "52fdfc07-2182-154f-163f-5f0f9a621d72"
516516
}
517517
```
518+
519+
## Custom Log formatter (Bring Your Own Formatter)
520+
521+
You can customize the structure (keys and values) of your log entries by implementing a custom log formatter and override default log formatter using ``Logger.UseFormatter`` method. You can implement a custom log formatter by inheriting the ``ILogFormatter`` class and implementing the ``object FormatLogEntry(LogEntry logEntry)`` method.
522+
523+
=== "Function.cs"
524+
525+
```c# hl_lines="11"
526+
/**
527+
* Handler for requests to Lambda function.
528+
*/
529+
public class Function
530+
{
531+
/// <summary>
532+
/// Function constructor
533+
/// </summary>
534+
public Function()
535+
{
536+
Logger.UseFormatter(new CustomLogFormatter());
537+
}
538+
539+
[Logging(CorrelationIdPath = "/headers/my_request_id_header", SamplingRate = 0.7)]
540+
public async Task<APIGatewayProxyResponse> FunctionHandler
541+
(APIGatewayProxyRequest apigProxyEvent, ILambdaContext context)
542+
{
543+
...
544+
}
545+
}
546+
```
547+
=== "CustomLogFormatter.cs"
548+
549+
```c#
550+
public class CustomLogFormatter : ILogFormatter
551+
{
552+
public object FormatLogEntry(LogEntry logEntry)
553+
{
554+
return new
555+
{
556+
Message = logEntry.Message,
557+
Service = logEntry.Service,
558+
CorrelationIds = new
559+
{
560+
AwsRequestId = logEntry.LambdaContext?.AwsRequestId,
561+
XRayTraceId = logEntry.XRayTraceId,
562+
CorrelationId = logEntry.CorrelationId
563+
},
564+
LambdaFunction = new
565+
{
566+
Name = logEntry.LambdaContext?.FunctionName,
567+
Arn = logEntry.LambdaContext?.InvokedFunctionArn,
568+
MemoryLimitInMB = logEntry.LambdaContext?.MemoryLimitInMB,
569+
Version = logEntry.LambdaContext?.FunctionVersion,
570+
ColdStart = logEntry.ColdStart,
571+
},
572+
Level = logEntry.Level.ToString(),
573+
Timestamp = logEntry.Timestamp.ToString("o"),
574+
Logger = new
575+
{
576+
Name = logEntry.Name,
577+
SampleRate = logEntry.SamplingRate
578+
},
579+
};
580+
}
581+
}
582+
```
583+
584+
=== "Example CloudWatch Logs excerpt"
585+
586+
```json
587+
{
588+
"Message": "Test Message",
589+
"Service": "lambda-example",
590+
"CorrelationIds": {
591+
"AwsRequestId": "52fdfc07-2182-154f-163f-5f0f9a621d72",
592+
"XRayTraceId": "1-61b7add4-66532bb81441e1b060389429",
593+
"CorrelationId": "correlation_id_value"
594+
},
595+
"LambdaFunction": {
596+
"Name": "test",
597+
"Arn": "arn:aws:lambda:eu-west-1:12345678910:function:test",
598+
"MemorySize": 128,
599+
"Version": "$LATEST",
600+
"ColdStart": true
601+
},
602+
"Level": "Information",
603+
"Timestamp": "2021-12-13T20:32:22.5774262Z",
604+
"Logger": {
605+
"Name": "AWS.Lambda.Powertools.Logging.Logger",
606+
"SampleRate": 0.7
607+
}
608+
}
609+
```

examples/Idempotency/src/HelloWorld/HelloWorld.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
<PackageReference Include="Amazon.Lambda.Core" Version="2.1.0" />
99
<PackageReference Include="Amazon.Lambda.APIGatewayEvents" Version="2.6.0" />
1010
<PackageReference Include="Amazon.Lambda.Serialization.SystemTextJson" Version="2.3.1" />
11-
<PackageReference Include="AWS.Lambda.Powertools.Idempotency" Version="0.0.1-preview" />
12-
<PackageReference Include="AWS.Lambda.Powertools.Logging" Version="1.1.1" />
13-
<PackageReference Include="AWSSDK.DynamoDBv2" Version="3.7.105.1" />
11+
<PackageReference Include="AWS.Lambda.Powertools.Idempotency" Version="0.0.2-preview" />
12+
<PackageReference Include="AWS.Lambda.Powertools.Logging" Version="1.2.0" />
13+
<PackageReference Include="AWSSDK.DynamoDBv2" Version="3.7.200.23" />
1414
</ItemGroup>
1515
</Project>

examples/Idempotency/test/HelloWorld.Test/HelloWorld.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<PackageReference Include="Amazon.Lambda.Serialization.SystemTextJson" Version="2.3.1" />
99
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.0" />
1010
<PackageReference Include="xunit" Version="2.5.0" />
11-
<PackageReference Include="Testcontainers" Version="3.3.0" />
11+
<PackageReference Include="Testcontainers" Version="3.4.0" />
1212
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.0">
1313
<PrivateAssets>all</PrivateAssets>
1414
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>

examples/Logging/src/HelloWorld/HelloWorld.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
<ItemGroup>
88
<PackageReference Include="Amazon.Lambda.Core" Version="2.1.0" />
99
<PackageReference Include="Amazon.Lambda.APIGatewayEvents" Version="2.6.0" />
10-
<PackageReference Include="Amazon.Lambda.Serialization.SystemTextJson" Version="2.3.0" />
11-
<PackageReference Include="AWS.Lambda.Powertools.Logging" Version="1.1.1" />
12-
<PackageReference Include="AWSSDK.DynamoDBv2" Version="3.7.101.14" />
10+
<PackageReference Include="Amazon.Lambda.Serialization.SystemTextJson" Version="2.3.1" />
11+
<PackageReference Include="AWS.Lambda.Powertools.Logging" Version="1.2.0" />
12+
<PackageReference Include="AWSSDK.DynamoDBv2" Version="3.7.200.23" />
1313
</ItemGroup>
1414
</Project>

examples/Logging/test/HelloWorld.Test/HelloWorld.Tests.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
<ItemGroup>
66
<PackageReference Include="Amazon.Lambda.Core" Version="2.1.0" />
77
<PackageReference Include="Amazon.Lambda.TestUtilities" Version="2.0.0" />
8-
<PackageReference Include="Amazon.Lambda.Serialization.SystemTextJson" Version="2.3.0" />
9-
<PackageReference Include="AWSSDK.DynamoDBv2" Version="3.7.101.14" />
10-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.0" />
8+
<PackageReference Include="Amazon.Lambda.Serialization.SystemTextJson" Version="2.3.1" />
9+
<PackageReference Include="AWSSDK.DynamoDBv2" Version="3.7.200.23" />
10+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.1" />
1111
<PackageReference Include="NSubstitute" Version="5.0.0" />
1212
<PackageReference Include="xunit" Version="2.5.0" />
1313
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.0">

examples/Metrics/src/HelloWorld/HelloWorld.csproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
<ItemGroup>
88
<PackageReference Include="Amazon.Lambda.Core" Version="2.1.0" />
99
<PackageReference Include="Amazon.Lambda.APIGatewayEvents" Version="2.6.0" />
10-
<PackageReference Include="Amazon.Lambda.Serialization.SystemTextJson" Version="2.3.0" />
11-
<PackageReference Include="AWS.Lambda.Powertools.Logging" Version="1.1.1" />
12-
<PackageReference Include="AWS.Lambda.Powertools.Metrics" Version="1.3.2" />
13-
<PackageReference Include="AWSSDK.DynamoDBv2" Version="3.7.101.14" />
10+
<PackageReference Include="Amazon.Lambda.Serialization.SystemTextJson" Version="2.3.1" />
11+
<PackageReference Include="AWS.Lambda.Powertools.Logging" Version="1.2.0" />
12+
<PackageReference Include="AWS.Lambda.Powertools.Metrics" Version="1.3.3" />
13+
<PackageReference Include="AWSSDK.DynamoDBv2" Version="3.7.200.23" />
1414
</ItemGroup>
1515
</Project>

examples/Metrics/test/HelloWorld.Test/HelloWorld.Tests.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
<ItemGroup>
66
<PackageReference Include="Amazon.Lambda.Core" Version="2.1.0" />
77
<PackageReference Include="Amazon.Lambda.TestUtilities" Version="2.0.0" />
8-
<PackageReference Include="Amazon.Lambda.Serialization.SystemTextJson" Version="2.3.0" />
9-
<PackageReference Include="AWSSDK.DynamoDBv2" Version="3.7.101.14" />
10-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.0" />
8+
<PackageReference Include="Amazon.Lambda.Serialization.SystemTextJson" Version="2.3.1" />
9+
<PackageReference Include="AWSSDK.DynamoDBv2" Version="3.7.200.23" />
10+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.1" />
1111
<PackageReference Include="NSubstitute" Version="5.0.0" />
1212
<PackageReference Include="xunit" Version="2.5.0" />
1313
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.0">

examples/Parameters/cfn/HelloWorld.Cfn/HelloWorld.Cfn.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<ItemGroup>
99
<PackageReference Include="Amazon.Lambda.Core" Version="2.1.0" />
1010
<PackageReference Include="Amazon.Lambda.Serialization.SystemTextJson" Version="2.3.0" />
11-
<PackageReference Include="AWSSDK.DynamoDBv2" Version="3.7.103.1" />
11+
<PackageReference Include="AWSSDK.DynamoDBv2" Version="3.7.200.23" />
1212
</ItemGroup>
1313
<ItemGroup>
1414
<ProjectReference Include="..\..\src\HelloWorld\HelloWorld.csproj" />

examples/Parameters/src/HelloWorld/HelloWorld.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@
88
<PackageReference Include="Amazon.Lambda.Core" Version="2.1.0" />
99
<PackageReference Include="Amazon.Lambda.APIGatewayEvents" Version="2.6.0" />
1010
<PackageReference Include="Amazon.Lambda.Serialization.SystemTextJson" Version="2.3.0" />
11-
<PackageReference Include="AWS.Lambda.Powertools.Parameters" Version="0.0.2-preview" />
11+
<PackageReference Include="AWS.Lambda.Powertools.Parameters" Version="0.0.3-preview" />
1212
</ItemGroup>
1313
</Project>

examples/Parameters/test/HelloWorld.Test/HelloWorld.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<PackageReference Include="Amazon.Lambda.Core" Version="2.1.0" />
77
<PackageReference Include="Amazon.Lambda.TestUtilities" Version="2.0.0" />
88
<PackageReference Include="Amazon.Lambda.Serialization.SystemTextJson" Version="2.3.1" />
9-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.0" />
9+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.1" />
1010
<PackageReference Include="NSubstitute" Version="5.0.0" />
1111
<PackageReference Include="xunit" Version="2.5.0" />
1212
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.0">

examples/ServerlessApi/src/LambdaPowertoolsAPI/LambdaPowertoolsAPI.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
</PropertyGroup>
1414
<ItemGroup>
1515
<PackageReference Include="Amazon.Lambda.AspNetCoreServer" Version="8.1.0" />
16-
<PackageReference Include="AWS.Lambda.Powertools.Logging" Version="1.1.1" />
17-
<PackageReference Include="AWS.Lambda.Powertools.Metrics" Version="1.3.2" />
18-
<PackageReference Include="AWS.Lambda.Powertools.Tracing" Version="1.1.1" />
16+
<PackageReference Include="AWS.Lambda.Powertools.Logging" Version="1.2.0" />
17+
<PackageReference Include="AWS.Lambda.Powertools.Metrics" Version="1.3.3" />
18+
<PackageReference Include="AWS.Lambda.Powertools.Tracing" Version="1.1.2" />
1919
</ItemGroup>
2020
</Project>

examples/ServerlessApi/test/LambdaPowertoolsAPI.Tests/LambdaPowertoolsAPI.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
<PackageReference Include="Amazon.Lambda.TestUtilities" Version="2.0.0" />
2121
<PackageReference Include="Amazon.Lambda.APIGatewayEvents" Version="2.6.0" />
2222
<PackageReference Include="AWSSDK.Extensions.NETCore.Setup" Version="3.7.7" />
23-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.0" />
23+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.1" />
2424
<PackageReference Include="xunit" Version="2.5.0" />
2525
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.0">
2626
<PrivateAssets>all</PrivateAssets>

examples/Tracing/src/HelloWorld/HelloWorld.csproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
<ItemGroup>
88
<PackageReference Include="Amazon.Lambda.Core" Version="2.1.0" />
99
<PackageReference Include="Amazon.Lambda.APIGatewayEvents" Version="2.6.0" />
10-
<PackageReference Include="Amazon.Lambda.Serialization.SystemTextJson" Version="2.3.0" />
11-
<PackageReference Include="AWS.Lambda.Powertools.Logging" Version="1.1.1" />
12-
<PackageReference Include="AWS.Lambda.Powertools.Tracing" Version="1.1.1" />
13-
<PackageReference Include="AWSSDK.DynamoDBv2" Version="3.7.101.14" />
10+
<PackageReference Include="Amazon.Lambda.Serialization.SystemTextJson" Version="2.3.1" />
11+
<PackageReference Include="AWS.Lambda.Powertools.Logging" Version="1.2.0" />
12+
<PackageReference Include="AWS.Lambda.Powertools.Tracing" Version="1.1.2" />
13+
<PackageReference Include="AWSSDK.DynamoDBv2" Version="3.7.200.23" />
1414
<PackageReference Include="AWSXRayRecorder.Handlers.AwsSdk" Version="2.12.0" />
1515
</ItemGroup>
1616
</Project>

examples/Tracing/test/HelloWorld.Test/HelloWorld.Tests.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
<ItemGroup>
66
<PackageReference Include="Amazon.Lambda.Core" Version="2.1.0" />
77
<PackageReference Include="Amazon.Lambda.TestUtilities" Version="2.0.0" />
8-
<PackageReference Include="Amazon.Lambda.Serialization.SystemTextJson" Version="2.3.0" />
9-
<PackageReference Include="AWSSDK.DynamoDBv2" Version="3.7.101.14" />
10-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.0" />
8+
<PackageReference Include="Amazon.Lambda.Serialization.SystemTextJson" Version="2.3.1" />
9+
<PackageReference Include="AWSSDK.DynamoDBv2" Version="3.7.200.23" />
10+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.1" />
1111
<PackageReference Include="NSubstitute" Version="5.0.0" />
1212
<PackageReference Include="xunit" Version="2.5.0" />
1313
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.0">

libraries/src/AWS.Lambda.Powertools.Common/Aspects/IMethodAspectHandler.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,10 @@ public interface IMethodAspectHandler
3838
/// <summary>
3939
/// Called when [exception].
4040
/// </summary>
41-
/// <typeparam name="T"></typeparam>
4241
/// <param name="eventArgs">The <see cref="AspectEventArgs" /> instance containing the event data.</param>
4342
/// <param name="exception">The exception.</param>
4443
/// <returns>T.</returns>
45-
T OnException<T>(AspectEventArgs eventArgs, Exception exception);
44+
void OnException(AspectEventArgs eventArgs, Exception exception);
4645

4746
/// <summary>
4847
/// Handles the <see cref="E:Exit" /> event.

libraries/src/AWS.Lambda.Powertools.Common/Aspects/MethodAspectAttribute.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ protected internal sealed override T WrapSync<T>(Func<object[], T> target, objec
6464
}
6565
catch (Exception exception)
6666
{
67-
return AspectHandler.OnException<T>(eventArgs, exception);
67+
AspectHandler.OnException(eventArgs, exception);
68+
throw;
6869
}
6970
finally
7071
{
@@ -92,7 +93,8 @@ protected internal sealed override async Task<T> WrapAsync<T>(Func<object[], Tas
9293
}
9394
catch (Exception exception)
9495
{
95-
return AspectHandler.OnException<T>(eventArgs, exception);
96+
AspectHandler.OnException(eventArgs, exception);
97+
throw;
9698
}
9799
finally
98100
{
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
namespace AWS.Lambda.Powertools.Logging;
17+
18+
/// <summary>
19+
/// Represents a type used to format Powertools log entries.
20+
/// </summary>
21+
public interface ILogFormatter
22+
{
23+
/// <summary>
24+
/// Formats a log entry
25+
/// </summary>
26+
/// <param name="logEntry">The log entry.</param>
27+
/// <returns>Formatted log entry as object.</returns>
28+
object FormatLogEntry(LogEntry logEntry);
29+
}

libraries/src/AWS.Lambda.Powertools.Logging/Internal/LoggingAspectHandler.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
using System;
1717
using System.IO;
1818
using System.Linq;
19+
using System.Runtime.ExceptionServices;
1920
using System.Text.Json;
2021
using System.Text.Json.Serialization;
2122
using AWS.Lambda.Powertools.Common;
@@ -203,16 +204,16 @@ public void OnSuccess(AspectEventArgs eventArgs, object result)
203204
/// <summary>
204205
/// Called when [exception].
205206
/// </summary>
206-
/// <typeparam name="T"></typeparam>
207207
/// <param name="eventArgs">
208208
/// The <see cref="T:AWS.Lambda.Powertools.Aspects.AspectEventArgs" /> instance containing the
209209
/// event data.
210210
/// </param>
211211
/// <param name="exception">The exception.</param>
212-
/// <returns>T.</returns>
213-
public T OnException<T>(AspectEventArgs eventArgs, Exception exception)
212+
public void OnException(AspectEventArgs eventArgs, Exception exception)
214213
{
215-
throw exception;
214+
// The purpose of ExceptionDispatchInfo.Capture is to capture a potentially mutating exception's StackTrace at a point in time:
215+
// https://learn.microsoft.com/en-us/dotnet/standard/exceptions/best-practices-for-exceptions#capture-exceptions-to-rethrow-later
216+
ExceptionDispatchInfo.Capture(exception).Throw();
216217
}
217218

218219
/// <summary>

0 commit comments

Comments
 (0)