Skip to content

Commit 57b91d2

Browse files
authored
Merge pull request #526 from aws-powertools/develop
chore: Sync main and develop
2 parents bcda204 + 4394755 commit 57b91d2

File tree

14 files changed

+182
-26
lines changed

14 files changed

+182
-26
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ Powertools for AWS Lambda (.NET) provides three core utilities:
2727

2828
* **[Parameters](https://docs.powertools.aws.dev/lambda/dotnet/utilities/parameters/)** - provides high-level functionality to retrieve one or multiple parameter values from [AWS Systems Manager Parameter Store](https://docs.aws.amazon.com/systems-manager/latest/userguide/systems-manager-parameter-store.html), [AWS Secrets Manager](https://aws.amazon.com/secrets-manager/), or [Amazon DynamoDB](https://aws.amazon.com/dynamodb/). We also provide extensibility to bring your own providers.
2929

30-
* **[Idempotency (developer preview)](https://docs.powertools.aws.dev/lambda/dotnet/utilities/idempotency/)** - The idempotency utility provides a simple solution to convert your Lambda functions into idempotent operations which are safe to retry.
30+
* **[Idempotency](https://docs.powertools.aws.dev/lambda/dotnet/utilities/idempotency/)** - The idempotency utility provides a simple solution to convert your Lambda functions into idempotent operations which are safe to retry.
3131

3232
* **[Batch Processing (developer preview)](https://docs.powertools.aws.dev/lambda/dotnet/utilities/batch-processing/)** - The batch processing utility handles partial failures when processing batches from Amazon SQS, Amazon Kinesis Data Streams, and Amazon DynamoDB Streams.
3333

docs/core/logging.md

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,19 @@ Here is an example using the AWS SAM [Globals section](https://docs.aws.amazon.c
7272

7373
### Using AWS Lambda Advanced Logging Controls (ALC)
7474

75-
With [AWS Lambda Advanced Logging Controls (ALC)](https://docs.aws.amazon.com/lambda/latest/dg/monitoring-cloudwatchlogs.html#monitoring-cloudwatchlogs-advanced), you can control the output format of your logs as either TEXT or JSON and specify the minimum accepted log level for your application. Regardless of the output format setting in Lambda, Powertools for AWS Lambda will always output JSON formatted logging messages.
75+
!!! question "When is it useful?"
76+
When you want to set a logging policy to drop informational or verbose logs for one or all AWS Lambda functions, regardless of runtime and logger used.
7677

77-
When you have this feature enabled, log messages that don’t meet the configured log level are discarded by Lambda. For example, if you set the minimum log level to WARN, you will only receive WARN and ERROR messages in your AWS CloudWatch Logs, all other log levels will be discarded by Lambda.
78+
With [AWS Lambda Advanced Logging Controls (ALC)](https://docs.aws.amazon.com/lambda/latest/dg/monitoring-cloudwatchlogs.html#monitoring-cloudwatchlogs-advanced){target="_blank"}, you can enforce a minimum log level that Lambda will accept from your application code.
79+
80+
When enabled, you should keep `Logger` and ALC log level in sync to avoid data loss.
7881

7982
!!! warning "When using AWS Lambda Advanced Logging Controls (ALC)"
8083
- When Powertools Logger output is set to `PascalCase` **`Level`** property name will be replaced by **`LogLevel`** as a property name.
8184
- ALC takes precedence over **`POWERTOOLS_LOG_LEVEL`** and when setting it in code using **`[Logging(LogLevel = )]`**
8285

86+
Here's a sequence diagram to demonstrate how ALC will drop both `Information` and `Debug` logs emitted from `Logger`, when ALC log level is stricter than `Logger`.
87+
8388
```mermaid
8489
sequenceDiagram
8590
title Lambda ALC allows WARN logs only
@@ -88,6 +93,7 @@ sequenceDiagram
8893
participant Application Logger
8994
9095
Note over Lambda service: AWS_LAMBDA_LOG_LEVEL="WARN"
96+
Note over Application Logger: POWERTOOLS_LOG_LEVEL="DEBUG"
9197
Lambda service->>Lambda function: Invoke (event)
9298
Lambda function->>Lambda function: Calls handler
9399
Lambda function->>Application Logger: Logger.Warning("Something happened")
@@ -99,20 +105,18 @@ sequenceDiagram
99105
Lambda service->>CloudWatch Logs: Ingest error logs
100106
```
101107

102-
Logger will automatically listen for the AWS_LAMBDA_LOG_FORMAT and AWS_LAMBDA_LOG_LEVEL environment variables, and change behaviour if they’re found to ensure as much compatibility as possible.
103-
104108
**Priority of log level settings in Powertools for AWS Lambda**
105109

106-
When the Advanced Logging Controls feature is enabled, we are unable to increase the minimum log level below the AWS_LAMBDA_LOG_LEVEL environment variable value, see [AWS Lambda service documentation](https://docs.aws.amazon.com/lambda/latest/dg/monitoring-cloudwatchlogs.html#monitoring-cloudwatchlogs-log-level) for more details.
107-
108110
We prioritise log level settings in this order:
109111

110112
1. AWS_LAMBDA_LOG_LEVEL environment variable
111113
2. Setting the log level in code using `[Logging(LogLevel = )]`
112114
3. POWERTOOLS_LOG_LEVEL environment variable
113115

114-
In the event you have set POWERTOOLS_LOG_LEVEL to a level lower than the ACL setting, Powertools for AWS Lambda will output a warning log message informing you that your messages will be discarded by Lambda.
116+
If you set `Logger` level lower than ALC, we will emit a warning informing you that your messages will be discarded by Lambda.
115117

118+
> **NOTE**
119+
> With ALC enabled, we are unable to increase the minimum log level below the `AWS_LAMBDA_LOG_LEVEL` environment variable value, see [AWS Lambda service documentation](https://docs.aws.amazon.com/lambda/latest/dg/monitoring-cloudwatchlogs.html#monitoring-cloudwatchlogs-log-level){target="_blank"} for more details.
116120
117121
## Standard structured keys
118122

docs/index.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ Core utilities such as Tracing, Logging, and Metrics will be available across al
3030
[Logger](./core/logging.md) | Structured logging made easier, and decorator to enrich structured logging with key Lambda context details
3131
[Metrics](./core/metrics.md) | Custom AWS metrics created asynchronously via CloudWatch Embedded Metric Format (EMF)
3232
[Parameters](./utilities/parameters/) | provides high-level functionality to retrieve one or multiple parameter values from [AWS Systems Manager Parameter Store](https://docs.aws.amazon.com/systems-manager/latest/userguide/systems-manager-parameter-store.html){target="_blank"}, [AWS Secrets Manager](https://aws.amazon.com/secrets-manager/){target="_blank"}, or [Amazon DynamoDB](https://aws.amazon.com/dynamodb/){target="_blank"}. We also provide extensibility to bring your own providers.
33-
[Idempotency (developer preview)](./utilities/idempotency/) | The idempotency utility provides a simple solution to convert your Lambda functions into idempotent operations which are safe to retry.
33+
[Idempotency](./utilities/idempotency/) | The idempotency utility provides a simple solution to convert your Lambda functions into idempotent operations which are safe to retry.
3434
[Batch Processing (developer preview)](./utilities/batch-processing/) | The batch processing utility handles partial failures when processing batches from Amazon SQS, Amazon Kinesis Data Streams, and Amazon DynamoDB Streams.
3535

3636
## Install
@@ -128,4 +128,4 @@ These are our core principles to guide our decision making.
128128
* **Keep it lean**. Additional dependencies are carefully considered for security and ease of maintenance, and prevent negatively impacting startup time.
129129
* **We strive for backwards compatibility**. New features and changes should keep backwards compatibility. If a breaking change cannot be avoided, the deprecation and migration process should be clearly defined.
130130
* **We work backwards from the community**. We aim to strike a balance of what would work best for 80% of customers. Emerging practices are considered and discussed via Requests for Comment (RFCs)
131-
* **Idiomatic**. Utilities follow programming language idioms and language-specific best practices.
131+
* **Idiomatic**. Utilities follow programming language idioms and language-specific best practices.

docs/utilities/idempotency.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
title: Idempotency
33
description: Utility
44
---
5-
???+ warning
6-
**This utility is currently in developer preview** and is intended strictly for feedback and testing purposes **and not for production workloads**. The version and all future versions tagged with the `-preview` suffix should be treated as not stable. Until this utility is [General Availability](https://github.com/aws-powertools/powertools-lambda-dotnet/milestone/3) we may introduce significant breaking changes and improvements in response to customers feedback.
75

86
<!-- markdownlint-disable MD013 -->
97

examples/BatchProcessing/src/HelloWorld/HelloWorld.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<PackageReference Include="Amazon.Lambda.Core" Version="2.1.0" />
99
<PackageReference Include="Amazon.Lambda.Serialization.SystemTextJson" Version="2.3.1" />
1010
<PackageReference Include="AWS.Lambda.Powertools.BatchProcessing" Version="0.0.1-preview" />
11-
<PackageReference Include="AWS.Lambda.Powertools.Logging" Version="1.3.3" />
11+
<PackageReference Include="AWS.Lambda.Powertools.Logging" Version="1.4.4" />
1212
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
1313
</ItemGroup>
1414
</Project>

examples/Idempotency/src/HelloWorld/HelloWorld.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99
<PackageReference Include="Amazon.Lambda.APIGatewayEvents" Version="2.6.0" />
1010
<PackageReference Include="Amazon.Lambda.Serialization.SystemTextJson" Version="2.3.1" />
1111
<PackageReference Include="AWS.Lambda.Powertools.Idempotency" Version="0.2.2-preview" />
12-
<PackageReference Include="AWS.Lambda.Powertools.Logging" Version="1.3.3" />
12+
<PackageReference Include="AWS.Lambda.Powertools.Logging" Version="1.4.4" />
1313
</ItemGroup>
1414
</Project>

examples/Logging/src/HelloWorld/HelloWorld.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
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.Logging" Version="1.3.3" />
11+
<PackageReference Include="AWS.Lambda.Powertools.Logging" Version="1.4.4" />
1212
<PackageReference Include="AWSSDK.DynamoDBv2" Version="3.7.201.13" />
1313
</ItemGroup>
1414
</Project>

examples/Metrics/src/HelloWorld/HelloWorld.csproj

Lines changed: 2 additions & 2 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.Logging" Version="1.3.3" />
12-
<PackageReference Include="AWS.Lambda.Powertools.Metrics" Version="1.4.3" />
11+
<PackageReference Include="AWS.Lambda.Powertools.Logging" Version="1.4.4" />
12+
<PackageReference Include="AWS.Lambda.Powertools.Metrics" Version="1.5.3" />
1313
<PackageReference Include="AWSSDK.DynamoDBv2" Version="3.7.201.13" />
1414
</ItemGroup>
1515
</Project>

examples/ServerlessApi/src/LambdaPowertoolsAPI/LambdaPowertoolsAPI.csproj

Lines changed: 2 additions & 2 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.3.3" />
17-
<PackageReference Include="AWS.Lambda.Powertools.Metrics" Version="1.4.3" />
16+
<PackageReference Include="AWS.Lambda.Powertools.Logging" Version="1.4.4" />
17+
<PackageReference Include="AWS.Lambda.Powertools.Metrics" Version="1.5.3" />
1818
<PackageReference Include="AWS.Lambda.Powertools.Tracing" Version="1.3.2" />
1919
</ItemGroup>
2020
</Project>

examples/Tracing/src/HelloWorld/HelloWorld.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
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.Logging" Version="1.3.3" />
11+
<PackageReference Include="AWS.Lambda.Powertools.Logging" Version="1.4.4" />
1212
<PackageReference Include="AWS.Lambda.Powertools.Tracing" Version="1.3.2" />
1313
<PackageReference Include="AWSSDK.DynamoDBv2" Version="3.7.201.13" />
1414
</ItemGroup>
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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+
using System;
17+
using System.Collections.Generic;
18+
using System.IO;
19+
using System.Net.Http;
20+
using System.Text.Json;
21+
using System.Threading.Tasks;
22+
using Amazon.DynamoDBv2;
23+
using Amazon.Lambda.APIGatewayEvents;
24+
using Amazon.Lambda.Core;
25+
26+
namespace AWS.Lambda.Powertools.Idempotency.Tests.Handlers;
27+
28+
public class IdempotencyFunctionMethodDecorated
29+
{
30+
public bool MethodCalled;
31+
32+
public IdempotencyFunctionMethodDecorated(AmazonDynamoDBClient client)
33+
{
34+
Idempotency.Configure(builder =>
35+
builder
36+
.UseDynamoDb(storeBuilder =>
37+
storeBuilder
38+
.WithTableName("idempotency_table")
39+
.WithDynamoDBClient(client)
40+
));
41+
}
42+
43+
44+
public async Task<APIGatewayProxyResponse> Handle(APIGatewayProxyRequest apigProxyEvent, ILambdaContext context)
45+
{
46+
Idempotency.RegisterLambdaContext(context);
47+
var result= await InternalFunctionHandler(apigProxyEvent);
48+
49+
return result;
50+
}
51+
52+
private async Task<APIGatewayProxyResponse> InternalFunctionHandler(APIGatewayProxyRequest apigProxyEvent)
53+
{
54+
Dictionary<string, string> headers = new()
55+
{
56+
{"Content-Type", "application/json"},
57+
{"Access-Control-Allow-Origin", "*"},
58+
{"Access-Control-Allow-Methods", "GET, OPTIONS"},
59+
{"Access-Control-Allow-Headers", "*"}
60+
};
61+
62+
try
63+
{
64+
var address = JsonDocument.Parse(apigProxyEvent.Body).RootElement.GetProperty("address").GetString();
65+
var pageContents = await GetPageContents(address);
66+
var output = $"{{ \"message\": \"hello world\", \"location\": \"{pageContents}\" }}";
67+
68+
return new APIGatewayProxyResponse
69+
{
70+
Body = output,
71+
StatusCode = 200,
72+
Headers = headers
73+
};
74+
75+
}
76+
catch (IOException)
77+
{
78+
return new APIGatewayProxyResponse
79+
{
80+
Body = "{}",
81+
StatusCode = 500,
82+
Headers = headers
83+
};
84+
}
85+
}
86+
87+
[Idempotent]
88+
private async Task<string> GetPageContents(string address)
89+
{
90+
MethodCalled = true;
91+
92+
var client = new HttpClient();
93+
using var response = await client.GetAsync(address);
94+
using var content = response.Content;
95+
var pageContent = await content.ReadAsStringAsync();
96+
97+
return pageContent;
98+
}
99+
}

libraries/tests/AWS.Lambda.Powertools.Idempotency.Tests/IdempotencyTest.cs

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,27 @@
11
/*
22
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3-
*
3+
*
44
* Licensed under the Apache License, Version 2.0 (the "License").
55
* You may not use this file except in compliance with the License.
66
* A copy of the License is located at
7-
*
7+
*
88
* http://aws.amazon.com/apache2.0
9-
*
9+
*
1010
* or in the "license" file accompanying this file. This file is distributed
1111
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
1212
* express or implied. See the License for the specific language governing
1313
* permissions and limitations under the License.
1414
*/
1515

16+
using System;
17+
using System.Collections.Generic;
1618
using System.IO;
1719
using System.Text.Json;
1820
using System.Threading.Tasks;
1921
using Amazon.DynamoDBv2;
2022
using Amazon.DynamoDBv2.Model;
2123
using Amazon.Lambda.APIGatewayEvents;
24+
using Amazon.Lambda.TestUtilities;
2225
using AWS.Lambda.Powertools.Idempotency.Tests.Handlers;
2326
using AWS.Lambda.Powertools.Idempotency.Tests.Persistence;
2427
using FluentAssertions;
@@ -66,5 +69,57 @@ public async Task EndToEndTest()
6669
TableName = _tableName
6770
});
6871
scanResponse.Count.Should().Be(1);
72+
73+
// delete row dynamo
74+
var key = new Dictionary<string, AttributeValue>
75+
{
76+
["id"] = new AttributeValue { S = "testFunction.GetPageContents#ff323c6f0c5ceb97eed49121babcec0f" }
77+
};
78+
await _client.DeleteItemAsync(new DeleteItemRequest{TableName = _tableName, Key = key});
79+
}
80+
81+
[Fact]
82+
[Trait("Category", "Integration")]
83+
public async Task EndToEndTestMethod()
84+
{
85+
var function = new IdempotencyFunctionMethodDecorated(_client);
86+
87+
var options = new JsonSerializerOptions
88+
{
89+
PropertyNameCaseInsensitive = true
90+
};
91+
92+
var context = new TestLambdaContext
93+
{
94+
RemainingTime = TimeSpan.FromSeconds(30)
95+
};
96+
97+
var request = JsonSerializer.Deserialize<APIGatewayProxyRequest>(await File.ReadAllTextAsync("./resources/apigw_event2.json"),options);
98+
99+
var response = await function.Handle(request, context);
100+
function.MethodCalled.Should().BeTrue();
101+
102+
function.MethodCalled = false;
103+
104+
var response2 = await function.Handle(request, context);
105+
function.MethodCalled.Should().BeFalse();
106+
107+
// Assert
108+
JsonSerializer.Serialize(response).Should().Be(JsonSerializer.Serialize(response2));
109+
response.Body.Should().Contain("hello world");
110+
response2.Body.Should().Contain("hello world");
111+
112+
var scanResponse = await _client.ScanAsync(new ScanRequest
113+
{
114+
TableName = _tableName
115+
});
116+
scanResponse.Count.Should().Be(1);
117+
118+
// delete row dynamo
119+
var key = new Dictionary<string, AttributeValue>
120+
{
121+
["id"] = new AttributeValue { S = "testFunction.GetPageContents#ff323c6f0c5ceb97eed49121babcec0f" }
122+
};
123+
await _client.DeleteItemAsync(new DeleteItemRequest{TableName = _tableName, Key = key});
69124
}
70125
}

libraries/tests/AWS.Lambda.Powertools.Idempotency.Tests/Internal/IdempotentAspectTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ public void Handle_WhenIdempotencyOnSubMethodAnnotated_AndSecondCall_AndNotExpir
311311
// Arrange
312312
var store = Substitute.For<BasePersistenceStore>();
313313
store.SaveInProgress(Arg.Any<JsonDocument>(), Arg.Any<DateTimeOffset>(), Arg.Any<double>())
314-
.Throws(new IdempotencyItemAlreadyExistsException());
314+
.Returns(_ => throw new IdempotencyItemAlreadyExistsException());
315315

316316
Idempotency.Configure(builder => builder.WithPersistenceStore(store));
317317

@@ -327,7 +327,7 @@ public void Handle_WhenIdempotencyOnSubMethodAnnotated_AndSecondCall_AndNotExpir
327327
.Returns(record);
328328

329329
// Act
330-
var function = new IdempotencyInternalFunction(false);
330+
var function = new IdempotencyInternalFunction(true);
331331
Basket resultBasket = function.HandleRequest(product, new TestLambdaContext());
332332

333333
// assert

version.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
},
77
"Utilities": {
88
"Parameters": "1.1.2",
9-
"Idempotency": "0.2.2-preview",
9+
"Idempotency": "1.0.0",
1010
"BatchProcessing": "0.0.1-preview"
1111
}
1212
}

0 commit comments

Comments
 (0)