Tracing
The Tracing utility is an opinionated thin wrapper for AWS X-Ray Java SDK a provides functionality to reduce the overhead of performing common tracing tasks.

Key Features
- Capture cold start as annotation, and responses as well as full exceptions as metadata
- Helper methods to improve the developer experience of creating new X-Ray subsegments.
- Better developer experience when developing with multiple threads.
- Auto patch supported modules by AWS X-Ray
- GraalVM support
Install¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | |
Initialization¶
Before your use this utility, your AWS Lambda function must have permissions to send traces to AWS X-Ray.
Example using AWS Serverless Application Model (SAM)
1 2 3 4 5 6 7 8 9 10 11 | |
The Powertools for AWS Lambda (Java) service name is used as the X-Ray namespace. This can be set using the environment variable
POWERTOOLS_SERVICE_NAME
Lambda handler¶
You can enable tracing using either the @Tracing annotation or the functional API.
With the @Tracing annotation, add it to your handleRequest method or any method to capture it as a separate subsegment automatically. You can optionally customize the segment name that appears in traces.
With the functional API, use TracingUtils.withSubsegment() to manually create subsegments without AspectJ configuration.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | |
1 2 3 4 5 6 | |
When using the @Tracing annotation, the utility performs these additional tasks to ease operations:
- Creates a
ColdStartannotation to easily filter traces that have had an initialization overhead. - Creates a
Serviceannotation if service parameter orPOWERTOOLS_SERVICE_NAMEis set. - Captures any response, or full exceptions generated by the handler, and include as tracing metadata.
By default, the @Tracing annotation uses captureMode=ENVIRONMENT_VAR, which means it will only record method responses and exceptions if you set
the environment variables POWERTOOLS_TRACER_CAPTURE_RESPONSE and POWERTOOLS_TRACER_CAPTURE_ERROR to true. You can override this behavior by
specifying a different captureMode to always record response, exception, both, or neither.
Note
When using the functional API with TracingUtils.withSubsegment(), response and exception capture is not automatic. You can manually add metadata using TracingUtils.putMetadata() as needed.
Returning sensitive information from your Lambda handler or functions?
When using the @Tracing annotation, you can disable it from capturing responses and exceptions as tracing metadata with captureMode=DISABLED
or globally by setting the environment variables POWERTOOLS_TRACER_CAPTURE_RESPONSE and POWERTOOLS_TRACER_CAPTURE_ERROR to false.
When using the functional API, you have full control over what metadata is captured.
1 2 3 4 5 6 | |
1 2 3 4 5 6 7 8 9 10 11 12 | |
1 2 3 4 5 6 7 8 9 | |
Annotations & Metadata¶
Annotations are key-values associated with traces and indexed by AWS X-Ray. You can use them to filter traces and to create Trace Groups to slice and dice your transactions.
Metadata are key-values also associated with traces but not indexed by AWS X-Ray. You can use them to add additional context for an operation using any native object.
You can add annotations using putAnnotation() method from TracingUtils
1 2 3 4 5 6 7 8 9 10 | |
You can add metadata using putMetadata() method from TracingUtils
1 2 3 4 5 6 7 8 9 10 | |
Override default object mapper¶
You can optionally choose to override default object mapper which is used to serialize method response and exceptions when enabled. You might want to supply custom object mapper in order to control how serialisation is done, for example, when you want to log only specific fields from received event due to security.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | |
Advanced usage¶
Multi-threaded programming¶
When working with multiple threads, you need to pass the trace entity to ensure proper trace context propagation.
1 2 3 4 5 6 7 8 9 10 11 12 13 | |
Instrumenting SDK clients and HTTP calls¶
AWS SDK for Java 2.x¶
Powertools for AWS Lambda (Java) includes the aws-xray-recorder-sdk-aws-sdk-v2-instrumentor library, which automatically instruments all AWS SDK v2 clients when you add the powertools-tracing dependency to your project. This means downstream calls to AWS services are traced without any additional configuration.
If you need more control over which clients are instrumented, you can manually add the TracingInterceptor to specific clients:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | |
For more details, refer to the AWS X-Ray documentation on tracing AWS SDK calls and outgoing HTTP calls.
Testing your code¶
When using @Tracing annotation, your Junit test cases needs to be configured to create parent Segment required by AWS X-Ray SDK for Java.
Below are two ways in which you can configure your tests.
Configure environment variable on project level (Recommended)¶
You can choose to configure environment variable on project level for your test cases run. This is recommended approach as it will avoid the need of configuring each test case specifically.
Below are examples configuring your maven/gradle projects. You can choose to configure it differently as well as long as you are making sure that environment variable LAMBDA_TASK_ROOT is set. This variable is
used internally via AWS X-Ray SDK to configure itself properly for lambda runtime.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | |
1 2 3 4 | |
Configure test cases (Not Recommended)¶
You can choose to configure each of your test case instead as well if you choose not to configure environment variable on project level. Below is an example configuration needed for each test case.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | |