You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
aspect 'software.amazon.lambda:powertools-tracing:{{ powertools.version }}' // Not needed when using the functional approach
89
+
implementation 'software.amazon.lambda:powertools-tracing:{{ powertools.version }}' // Use this instead of 'aspect' when using the functional approach
88
90
}
89
91
90
-
sourceCompatibility = 11
91
-
targetCompatibility = 11
92
+
sourceCompatibility = 11 // or higher
93
+
targetCompatibility = 11 // or higher
92
94
```
93
95
94
96
## Initialization
@@ -118,11 +120,13 @@ The Powertools for AWS Lambda (Java) service name is used as the X-Ray namespace
118
120
119
121
### Lambda handler
120
122
121
-
To enable Powertools for AWS Lambda (Java) tracing to your function add the `@Tracing` annotation to your `handleRequest` method or on
122
-
any method will capture the method as a separate subsegment automatically. You can optionally choose to customize
123
-
segment name that appears in traces.
123
+
You can enable tracing using either the `@Tracing` annotation or the functional API.
124
124
125
-
=== "Tracing annotation"
125
+
**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.
126
+
127
+
**With the functional API**, use `TracingUtils.withSubsegment()` to manually create subsegments without AspectJ configuration.
128
+
129
+
=== "@Tracing annotation"
126
130
127
131
```java hl_lines="3 10 15"
128
132
public class App implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> {
@@ -146,6 +150,25 @@ segment name that appears in traces.
@@ -157,22 +180,25 @@ segment name that appears in traces.
157
180
}
158
181
```
159
182
160
-
When using this`@Tracing` annotation, Utility performs these additional tasks to ease operations:
183
+
When using the`@Tracing` annotation, the utility performs these additional tasks to ease operations:
161
184
162
185
* Creates a `ColdStart` annotation to easily filter traces that have had an initialization overhead.
163
186
* Creates a `Service` annotation if service parameter or `POWERTOOLS_SERVICE_NAME` is set.
164
187
* Captures any response, or full exceptions generated by the handler, and include as tracing metadata.
165
188
189
+
By default, the `@Tracing` annotation uses `captureMode=ENVIRONMENT_VAR`, which means it will only record method responses and exceptions if you set
190
+
the environment variables `POWERTOOLS_TRACER_CAPTURE_RESPONSE` and `POWERTOOLS_TRACER_CAPTURE_ERROR` to `true`. You can override this behavior by
191
+
specifying a different `captureMode` to always record response, exception, both, or neither.
166
192
167
-
By default, this annotation will automatically record method responses and exceptions. You can change the default behavior by setting
168
-
the environment variables `POWERTOOLS_TRACER_CAPTURE_RESPONSE` and `POWERTOOLS_TRACER_CAPTURE_ERROR` as needed. Optionally, you can override behavior by
169
-
different supported `captureMode` to record response, exception or both.
193
+
!!! note
194
+
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.
170
195
171
-
!!! warning "Returning sensitive information from your Lambda handler or functions, where `Tracing` is used?"
172
-
You can disable annotation from capturing their responses and exception as tracing metadata with **`captureMode=DISABLED`**
173
-
or globally by setting environment variables **`POWERTOOLS_TRACER_CAPTURE_RESPONSE`** and **`POWERTOOLS_TRACER_CAPTURE_ERROR`** to **`false`**
196
+
!!! warning "Returning sensitive information from your Lambda handler or functions?"
197
+
When using the `@Tracing` annotation, you can disable it from capturing responses and exceptions as tracing metadata with **`captureMode=DISABLED`**
198
+
or globally by setting environment variables **`POWERTOOLS_TRACER_CAPTURE_RESPONSE`** and **`POWERTOOLS_TRACER_CAPTURE_ERROR`** to **`false`**.
199
+
When using the functional API, you have full control over what metadata is captured.
174
200
175
-
=== "Disable on annotation"
201
+
=== "@Tracing annotation - Disable on method"
176
202
177
203
```java hl_lines="3"
178
204
public class App implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> {
@@ -183,7 +209,7 @@ different supported `captureMode` to record response, exception or both.
183
209
}
184
210
```
185
211
186
-
=== "Disable Globally"
212
+
=== "@Tracing annotation - Disable Globally"
187
213
188
214
```yaml hl_lines="11 12"
189
215
Resources:
@@ -200,6 +226,20 @@ different supported `captureMode` to record response, exception or both.
@@ -317,25 +338,33 @@ under a subsegment, or you are doing multithreaded programming. Refer examples b
317
338
318
339
## Instrumenting SDK clients and HTTP calls
319
340
320
-
Powertools for Lambda (Java) cannot intercept SDK clients instantiation to add X-Ray instrumentation. You should make sure to instrument the SDK clients explicitly. Refer details on
321
-
[how to instrument SDK client with Xray](https://docs.aws.amazon.com/xray/latest/devguide/xray-sdk-java.html#xray-sdk-java-awssdkclients)
322
-
and [outgoing http calls](https://docs.aws.amazon.com/xray/latest/devguide/xray-sdk-java.html#xray-sdk-java-httpclients). For example:
341
+
### AWS SDK for Java 2.x
323
342
324
-
=== "LambdaHandler.java"
343
+
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.
For more details, refer to the [AWS X-Ray documentation on tracing AWS SDK calls](https://docs.aws.amazon.com/xray/latest/devguide/xray-sdk-java-awssdkclients.html) and [outgoing HTTP calls](https://docs.aws.amazon.com/xray/latest/devguide/xray-sdk-java-httpclients.html).
367
+
339
368
## Testing your code
340
369
341
370
When using `@Tracing` annotation, your Junit test cases needs to be configured to create parent Segment required by [AWS X-Ray SDK for Java](https://docs.aws.amazon.com/xray/latest/devguide/xray-sdk-java.html).
@@ -351,7 +380,7 @@ used internally via AWS X-Ray SDK to configure itself properly for lambda runtim
351
380
352
381
=== "Maven (pom.xml)"
353
382
354
-
```xml hl_lines="4-13"
383
+
```xml
355
384
<build>
356
385
...
357
386
<plugins>
@@ -370,9 +399,9 @@ used internally via AWS X-Ray SDK to configure itself properly for lambda runtim
370
399
371
400
```
372
401
373
-
=== "Gradle (build.gradle)"
402
+
=== "Gradle (build.gradle)"
374
403
375
-
```json hl_lines="2-4"
404
+
```json
376
405
// Configures environment variable to avoid initialization of AWS X-Ray segments for each tests
377
406
test {
378
407
environment "LAMBDA_TASK_ROOT", "handler"
@@ -418,6 +447,3 @@ Below is an example configuration needed for each test case.
0 commit comments