generated from amazon-archives/__template_MIT-0
-
Notifications
You must be signed in to change notification settings - Fork 96
feat: Support CRaC and priming of powertools metrics and idempotency-dynamodb #1861
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
phipag
merged 22 commits into
aws-powertools:main
from
subhash686:support_crac_on_powertools_metrics
Jul 31, 2025
Merged
Changes from 7 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
597d123
Automatic priming of powertools-metrics
subhash686 ec43605
Merge remote-tracking branch 'upstream/v2' into support_crac_on_power…
subhash686 91ee9df
Fixed resource name and added unit tests and javadoc comments
subhash686 e10a624
Invoke prime Dynamo persistent store
subhash686 747dad8
Merge branch 'v2' into support_crac_on_powertools_metrics
subhash686 c8d2404
Merge branch 'main' into support_crac_on_powertools_metrics
subhash686 7ba4cfa
Merge branch 'main' into support_crac_on_powertools_metrics
subhash686 8b2dba4
Merge branch 'main' into support_crac_on_powertools_metrics
subhash686 85513ea
Fixed Sonar issues
subhash686 e1906e9
Merge branch 'main' into support_crac_on_powertools_metrics
subhash686 0c80204
Update classesloaded.txt
subhash686 babd855
Merge branch 'main' into support_crac_on_powertools_metrics
phipag 789564b
Moved priming to MetricsFactory and DynamoDBPersistenceStore, added P…
subhash686 4cebb6b
Update classesloaded.txt
subhash686 6336252
Update LambdaMetricsAspect.java
subhash686 27dad71
Update DynamoDBPersistenceStore.java
subhash686 685877e
Merge branch 'main' into support_crac_on_powertools_metrics
subhash686 ec37b49
Merge branch 'main' into support_crac_on_powertools_metrics
subhash686 a1e1545
Improved priming.md documentation and static initialization of classe…
subhash686 6261d06
Merge branch 'support_crac_on_powertools_metrics' of https://github.c…
subhash686 844dad7
Fixed a Sonarqube finding and added more unit tests to ensure all ava…
subhash686 9b6e5f9
Merge branch 'main' into support_crac_on_powertools_metrics
subhash686 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# Automatic Priming for AWS Lambda Powertools Java | ||
|
||
## Table of Contents | ||
- [Overview](#overview) | ||
- [Implementation Steps](#general-implementation-steps) | ||
- [Known Issues](#known-issues-and-solutions) | ||
- [Reference Implementation](#reference-implementation) | ||
|
||
## Overview | ||
Priming is the process of preloading dependencies and initializing resources during the INIT phase, rather than during the INVOKE phase to further optimize startup performance with SnapStart. | ||
This is required because Java frameworks that use dependency injection load classes into memory when these classes are explicitly invoked, which typically happens during Lambda’s INVOKE phase. | ||
|
||
This documentation provides guidance for automatic class priming in AWS Lambda Powertools Java modules. | ||
|
||
|
||
## Implementation Steps | ||
Classes are proactively loaded using Java runtime hooks which are part of the open source CRaC (Coordinated Restore at Checkpoint) project. | ||
phipag marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
This implementation uses this hook, called `beforeCheckpoint()`, to prime SnapStart-enabled Java functions via Class Priming. | ||
phipag marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
In order to generate the `classloaded` files for Powertools java module, follow these general steps. | ||
phipag marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
1. **Add Maven Profile** | ||
- Add maven test profile with the following VM argument for generating classes loaded files. | ||
```shell | ||
-Xlog:class+load=info:classesloaded.txt | ||
``` | ||
- You can find an example of this in profile `generate-classesloaded-file` in this [pom.xml](powertools-metrics/pom.xml). | ||
phipag marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
2. **Generate classes loaded file** | ||
- Run tests with `-Pgenerate-classesloaded-file` profile. | ||
```shell | ||
mvn -Pgenerate-classesloaded-file clean test | ||
``` | ||
- This will generate a file named `classesloaded.txt` in the target directory of the module. | ||
|
||
3. **Cleanup the file** | ||
- The classes loaded file generated in Step 2 has the format | ||
`[0.054s][info][class,load] java.lang.Object source: shared objects file` | ||
but we are only interested in `java.lang.Object` the fully qualified class name. | ||
phipag marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
- To strip the lines to include only the fully qualified class name, | ||
Use the following regex to replace with empty string. | ||
- `^\[[\[\]0-9.a-z,]+ ` (to replace the left part) | ||
- `( source: )[0-9a-z :/._$-]+` (to replace the right part) | ||
|
||
4. **Place the file** | ||
phipag marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
- Move the cleanedup file containing to the corresponding resources directory of the module. See [example](powertools-metrics/src/resources/classesloaded.txt). | ||
phipag marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
5. **Register and checkpoint** | ||
phipag marked this conversation as resolved.
Show resolved
Hide resolved
|
||
- Register the CRaC Resource in the constructor of the main class. | ||
- Add the `beforeCheckpoint()` hook in the same class to invoke `ClassPreLoader.preloadClasses()`. | ||
- This will ensure that the classes are loaded during the INIT phase of the Lambda function. See [example](powertools-metrics/src/main/java/software/amazon/lambda/powertools/metrics/MetricsFactory.java) | ||
phipag marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
## Known Issues | ||
- This is a manual process at the moment, but it can be automated in the future. | ||
- Because the file is generated while running tests, it includes test classes as well. | ||
phipag marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
## Reference Implementation | ||
Working example is available in the [powertools-metrics](powertools-metrics/src/main/java/software/amazon/lambda/powertools/metrics/MetricsFactory.java). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.