|
| 1 | +<p align="center"> |
| 2 | + <img src="docs/Arch_AWS-Lambda_64.svg" alt="AWS Lambda service icon"> |
| 3 | +</p> |
| 4 | + |
| 5 | +<h2 align="center">AWS Lambda Profiler Extension for Java</h2> |
| 6 | + |
| 7 | +The Lambda profiler extension allows you to profile your Java functions invoke by invoke, with high fidelity, and no |
| 8 | +code changes. It uses the [async-profiler](https://github.com/async-profiler/async-profiler) project to produce |
| 9 | +profiling data and automatically uploads the data as HTML flame graphs to S3. |
| 10 | + |
| 11 | +<p align="center"> |
| 12 | + <img src="docs/example-cold-start-flame-graph.png" alt="A flame graph of a Java Lambda function"> |
| 13 | +</p> |
| 14 | + |
| 15 | +## Current status |
| 16 | +**This is an alpha release and not yet ready for production use.** We're especially interested in early feedback on usability, features, performance, and compatibility. Please send feedback by opening a [GitHub issue](https://github.com/aws/aws-lambda-java-libs/issues/new). |
| 17 | + |
| 18 | +The profiler has been tested with Lambda managed runtimes for Java 17 and Java 21. |
| 19 | + |
| 20 | +## How to use the Lambda Profiler |
| 21 | + |
| 22 | +To use the profiler you need to |
| 23 | + |
| 24 | +1. Build the extension in this repo |
| 25 | +2. Deploy it as a Lambda Layer and attach the layer to your function |
| 26 | +3. Create an S3 bucket for the results, or reuse an existing one |
| 27 | +4. Give your function permission to write to the bucket |
| 28 | +5. Configure the required environment variables. |
| 29 | + |
| 30 | +The above assumes you're using the ZIP deployment method with managed runtimes. If you deploy your functions as container images instead, you will need to include the profiler in your Dockerfile at `/opt/extensions/` rather than using a Lambda layer. |
| 31 | + |
| 32 | +### Quick Start |
| 33 | + |
| 34 | +The following [Quick Start](#quick-start) gives AWS CLI commands you can run to get started (MacOS/Linux). There are also [examples](examples) using infrastructure as code for you to refer to. |
| 35 | + |
| 36 | +1. Clone the repo |
| 37 | + |
| 38 | + ```bash |
| 39 | + git clone https://github.com/aws/aws-lambda-java-libs |
| 40 | + ``` |
| 41 | + |
| 42 | +2. Build the extension |
| 43 | + |
| 44 | + ```bash |
| 45 | + cd aws-lambda-java-libs/experimental/aws-lambda-java-profiler/extension |
| 46 | + ./build_layer.sh |
| 47 | + ``` |
| 48 | + |
| 49 | +3. Run the `update-function.sh` script which will create a new S3 bucket, Lambda layer and all the configuration required. |
| 50 | + |
| 51 | + ```bash |
| 52 | + cd .. |
| 53 | + ./update-function.sh YOUR_FUNCTION_NAME |
| 54 | + ``` |
| 55 | + |
| 56 | +4. Invoke your function and review the flame graph in S3 using your browser. |
| 57 | + |
| 58 | +### Configuration |
| 59 | + |
| 60 | +#### Required Environment Variables |
| 61 | + |
| 62 | +| Name | Value | |
| 63 | +|-----------------------------------------|-----------------------------------------------------------------------------------------------| |
| 64 | +| AWS_LAMBDA_PROFILER_RESULTS_BUCKET_NAME | Your unique bucket name | |
| 65 | +| JAVA_TOOL_OPTIONS | -XX:+UnlockDiagnosticVMOptions -XX:+DebugNonSafepoints -javaagent:/opt/profiler-extension.jar | |
| 66 | + |
| 67 | +#### Optional Environment Variables |
| 68 | + |
| 69 | +| Name | Default Value | Options | |
| 70 | +|------------------------------------------|-----------------------------------------------------------|--------------------------------| |
| 71 | +| AWS_LAMBDA_PROFILER_START_COMMAND | start,event=wall,interval=1us | | |
| 72 | +| AWS_LAMBDA_PROFILER_STOP_COMMAND | stop,file=%s,include=*AWSLambda.main,include=start_thread | file=%s is required | |
| 73 | +| AWS_LAMBDA_PROFILER_DEBUG | false | true - to enable debug logging | |
| 74 | +| AWS_LAMBDA_PROFILER_COMMUNICATION_PORT | 1234 | a valid port number | |
| 75 | + |
| 76 | +### How does it work? |
| 77 | + |
| 78 | +In `/src` is the code for a Java agent. It's entry point `AgentEntry.premain()` is executed as the runtime starts up. |
| 79 | +The environment variable `JAVA_TOOL_OPTIONS` is used to specify which `.jar` file the agent is in. The `MANIFEST.MF` file is used to specify the pre-main class. |
| 80 | + |
| 81 | +When the agent is constructed, it starts the profiler and registers itself as a Lambda extension for `INVOKE` request. |
| 82 | + |
| 83 | +A new thread is created to handle calling `/next` and uploading the results of the profiler to S3. The bucket to upload |
| 84 | +the result to is configurable using an environment variable. |
| 85 | + |
| 86 | +### Troubleshooting |
| 87 | + |
| 88 | +- Ensure the Lambda function execution role has the necessary permissions to write to the S3 bucket. |
| 89 | +- Verify that the environment variables are set correctly in your Lambda function configuration. |
| 90 | +- Check CloudWatch logs for any error messages from the extension. |
| 91 | + |
| 92 | +## Contributing |
| 93 | + |
| 94 | +Contributions to improve the Java profiler extension are welcome. Please see [CONTRIBUTING.md](../../CONTRIBUTING.md) for more information on how to report bugs or submit pull requests. |
| 95 | + |
| 96 | +Issues or contributions to the [async-profiler](https://github.com/async-profiler/async-profiler) itself should be submitted to that project. |
| 97 | + |
| 98 | +### Security |
| 99 | + |
| 100 | +If you discover a potential security issue in this project we ask that you notify AWS Security via our [vulnerability reporting page](http://aws.amazon.com/security/vulnerability-reporting/). Please do **not** create a public GitHub issue. |
| 101 | + |
| 102 | +### Code of conduct |
| 103 | + |
| 104 | +This project has adopted the [Amazon Open Source Code of Conduct](https://aws.github.io/code-of-conduct). See [CODE_OF_CONDUCT.md](doc/CODE_OF_CONDUCT.md) for more details. |
| 105 | + |
| 106 | +## License |
| 107 | + |
| 108 | +This project is licensed under the [Apache 2.0](../../LICENSE) License. It uses the following projects: |
| 109 | + |
| 110 | +- [async-profiler](https://github.com/async-profiler/async-profiler) (Apache 2.0 license) |
| 111 | +- [AWS SDK for Java 2.0](https://github.com/aws/aws-sdk-java-v2) (Apache 2.0 license) |
| 112 | +- Other libraries in this repository (Apache 2.0 license) |
| 113 | + |
0 commit comments