Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

1.20 #761

Unanswered
hjgraca asked this question in General
1.20 #761
Feb 11, 2025 · 0 comments
Discussion options

Summary

As part of our ongoing effort to make Powertools fully AOT compatible, we're thrilled to announce Idempotency support for native ahead-of-time (AOT) compilation.
Additionally, we've introduced custom key prefix support for Idempotency.
We've also addressed a bug in our Tracing utility that affected implementations using Top Level Statements.

Special thanks to @nCubed for bringing the Tracing issue to our attention.

Idempotency AOT

Docs

To enable Idempotency utility with AOT support, you need to configure the serialization context by adding WithJsonSerializationContext() to your Idempotency configuration.

In the example below, we use the default LambdaFunctionJsonSerializerContext. If you're using the Logging utility, you can alternatively use the PowertoolsSourceGeneratorSerializer.For more information visit our documentation

Idempotency.Configure(builder =>
builder.WithJsonSerializationContext(LambdaFunctionJsonSerializerContext.Default)));

Full example

public static class Function
{
 private static async Task Main()
 {
 var tableName = Environment.GetEnvironmentVariable("IDEMPOTENCY_TABLE_NAME");
 Idempotency.Configure(builder =>
 builder
 .WithJsonSerializationContext(LambdaFunctionJsonSerializerContext.Default)
 .WithOptions(optionsBuilder => optionsBuilder
 .WithExpiration(TimeSpan.FromHours(1)))
 .UseDynamoDb(storeBuilder => storeBuilder
 .WithTableName(tableName)
 ));
 Func<APIGatewayProxyRequest, ILambdaContext, APIGatewayProxyResponse> handler = FunctionHandler;
 await LambdaBootstrapBuilder.Create(handler,
 new SourceGeneratorLambdaJsonSerializer<LambdaFunctionJsonSerializerContext>())
 .Build()
 .RunAsync();
 }
 [Idempotent]
 public static APIGatewayProxyResponse FunctionHandler(APIGatewayProxyRequest apigwProxyEvent,
 ILambdaContext context)
 {
 return new APIGatewayProxyResponse
 {
 Body = JsonSerializer.Serialize(response, typeof(Response), LambdaFunctionJsonSerializerContext.Default),
 StatusCode = 200,
 Headers = new Dictionary<string, string> { { "Content-Type", "application/json" } }
 };
 }
}
[JsonSerializable(typeof(APIGatewayProxyRequest))]
[JsonSerializable(typeof(APIGatewayProxyResponse))]
[JsonSerializable(typeof(Response))]
public partial class LambdaFunctionJsonSerializerContext : JsonSerializerContext
{
}

Idempotency custom key prefix

Docs

By default, the idempotency key follows the format [ClassName].[DecoratedMethodName]#[PayloadHash].
With this release, You can now customize this prefix by setting the KeyPrefix property in the Idempotency decorator, making code refactoring more manageable without impacting existing idempotency items.

public class Function
{
 public Function()
 {
 var tableName = Environment.GetEnvironmentVariable("IDEMPOTENCY_TABLE_NAME");
 Idempotency.Configure(builder => builder.UseDynamoDb(tableName));
 }
 [Idempotent(KeyPrefix = "MyCustomKeyPrefix")]
 public APIGatewayProxyResponse FunctionHandler(APIGatewayProxyRequest apigwProxyEvent, ILambdaContext context)
 {
 return TestHelper.TestMethod(apigwProxyEvent);
 }
}

Tracing segment name sanitization

Tracing segment name docs and X-Ray docs

Previously, string names automatically generated from Tracing-decorated methods weren't sanitized, which could silently fail and prevent traces from appearing in X-Ray due to invalid characters. While this could be addressed by specifying a custom segment name in the Tracing decorator, the custom string itself might still contain invalid characters.
To resolve this, we've implemented two improvements:

  1. Added documentation describing valid characters for segment names
  2. Implemented automatic string sanitization for both auto-generated and custom segment names

These changes ensure that no traces are dropped due to invalid characters in segment names, maintaining reliable trace data collection in X-Ray.

 /// <summary>
 /// Set custom segment name for the operation.
 /// The default is '## {MethodName}'.
 ///
 /// The logical name of the service that handled the request, up to 200 characters. 
 /// Names can contain Unicode letters, numbers, and whitespace, and the following symbols: \_, ., :, /, %, &amp;, #, =, +, \,円 -, @
 /// </summary>
 public string SegmentName { get; set; } = "";
using System.Text.RegularExpressions;
public static string SanitizeString(string input)
{
 // Define a regular expression pattern to match allowed characters
 string pattern = @"[^a-zA-Z0-9\s_\.\:/%&#=+\-@]";
 
 // Replace any character that does not match the pattern with an empty string
 return Regex.Replace(input, pattern, string.Empty);
}

Changes

📜 Documentation updates

🔧 Maintenance

This release was made possible by the following contributors:

@dependabot[bot], @hjgraca and dependabot[bot]


This discussion was created from the release 1.20.
You must be logged in to vote

Replies: 0 comments

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
1 participant

AltStyle によって変換されたページ (->オリジナル) /