Skip to content

Commit da3e96b

Browse files
committed
Update tracing docs highlighting functional API.
1 parent 6a4e97c commit da3e96b

File tree

1 file changed

+84
-58
lines changed

1 file changed

+84
-58
lines changed

docs/core/tracing.md

Lines changed: 84 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ a provides functionality to reduce the overhead of performing common tracing tas
2020

2121
=== "Maven"
2222

23-
```xml hl_lines="3-7 16 18 24-27"
23+
```xml hl_lines="3-7 25-28"
2424
<dependencies>
2525
...
2626
<dependency>
@@ -32,6 +32,7 @@ a provides functionality to reduce the overhead of performing common tracing tas
3232
</dependencies>
3333
...
3434
<!-- configure the aspectj-maven-plugin to compile-time weave (CTW) the aws-lambda-powertools-java aspects into your project -->
35+
<!-- Note: This AspectJ configuration is not needed when using the functional approach -->
3536
<build>
3637
<plugins>
3738
...
@@ -73,22 +74,23 @@ a provides functionality to reduce the overhead of performing common tracing tas
7374

7475
=== "Gradle"
7576

76-
```groovy hl_lines="3 11"
77+
```groovy hl_lines="3 11 12"
7778
plugins {
7879
id 'java'
79-
id 'io.freefair.aspectj.post-compile-weaving' version '8.1.0'
80+
id 'io.freefair.aspectj.post-compile-weaving' version '8.1.0' // Not needed when using the functional approach
8081
}
8182
8283
repositories {
8384
mavenCentral()
8485
}
8586
8687
dependencies {
87-
aspect 'software.amazon.lambda:powertools-tracing:{{ powertools.version }}'
88+
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
8890
}
8991
90-
sourceCompatibility = 11
91-
targetCompatibility = 11
92+
sourceCompatibility = 11 // or higher
93+
targetCompatibility = 11 // or higher
9294
```
9395

9496
## Initialization
@@ -118,11 +120,13 @@ The Powertools for AWS Lambda (Java) service name is used as the X-Ray namespace
118120

119121
### Lambda handler
120122

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.
124124

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"
126130

127131
```java hl_lines="3 10 15"
128132
public class App implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> {
@@ -146,6 +150,25 @@ segment name that appears in traces.
146150
}
147151
```
148152

153+
=== "Functional API"
154+
155+
```java hl_lines="1 6 7 8 10 11 12"
156+
import software.amazon.lambda.powertools.tracing.TracingUtils;
157+
158+
public class App implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> {
159+
160+
public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent input, Context context) {
161+
TracingUtils.withSubsegment("businessLogic1", subsegment -> {
162+
// Business logic 1
163+
});
164+
165+
TracingUtils.withSubsegment("businessLogic2", subsegment -> {
166+
// Business logic 2
167+
});
168+
}
169+
}
170+
```
171+
149172
=== "Custom Segment names"
150173

151174
```java hl_lines="3"
@@ -157,22 +180,25 @@ segment name that appears in traces.
157180
}
158181
```
159182

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:
161184

162185
* Creates a `ColdStart` annotation to easily filter traces that have had an initialization overhead.
163186
* Creates a `Service` annotation if service parameter or `POWERTOOLS_SERVICE_NAME` is set.
164187
* Captures any response, or full exceptions generated by the handler, and include as tracing metadata.
165188

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.
166192

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.
170195

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.
174200

175-
=== "Disable on annotation"
201+
=== "@Tracing annotation - Disable on method"
176202

177203
```java hl_lines="3"
178204
public class App implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> {
@@ -183,7 +209,7 @@ different supported `captureMode` to record response, exception or both.
183209
}
184210
```
185211

186-
=== "Disable Globally"
212+
=== "@Tracing annotation - Disable Globally"
187213

188214
```yaml hl_lines="11 12"
189215
Resources:
@@ -200,6 +226,20 @@ different supported `captureMode` to record response, exception or both.
200226
POWERTOOLS_TRACER_CAPTURE_ERROR: false
201227
```
202228

229+
=== "Functional API"
230+
231+
```java hl_lines="6 7 8"
232+
import software.amazon.lambda.powertools.tracing.TracingUtils;
233+
234+
public class App implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> {
235+
236+
public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent input, Context context) {
237+
TracingUtils.withSubsegment("businessLogic", subsegment -> {
238+
// With functional API, you control what metadata is captured
239+
});
240+
}
241+
```
242+
203243
### Annotations & Metadata
204244

205245
**Annotations** are key-values associated with traces and indexed by AWS X-Ray. You can use them to filter traces and to
@@ -272,32 +312,13 @@ specific fields from received event due to security.
272312
}
273313
```
274314

275-
## Utilities
276-
277-
Tracing modules comes with certain utility method when you don't want to use annotation for capturing a code block
278-
under a subsegment, or you are doing multithreaded programming. Refer examples below.
315+
## Advanced usage
279316

280-
=== "Functional Api"
317+
### Multi-threaded programming
281318

282-
```java hl_lines="7 8 9 11 12 13"
283-
import software.amazon.lambda.powertools.tracing.Tracing;
284-
import software.amazon.lambda.powertools.tracing.TracingUtils;
285-
286-
public class App implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> {
287-
288-
public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent input, Context context) {
289-
TracingUtils.withSubsegment("loggingResponse", subsegment -> {
290-
// Some business logic
291-
});
292-
293-
TracingUtils.withSubsegment("localNamespace", "loggingResponse", subsegment -> {
294-
// Some business logic
295-
});
296-
}
297-
}
298-
```
319+
When working with multiple threads, you need to pass the trace entity to ensure proper trace context propagation.
299320

300-
=== "Multi Threaded Programming"
321+
=== "Multi-threaded example"
301322

302323
```java hl_lines="7 9 10 11"
303324
import static software.amazon.lambda.powertools.tracing.TracingUtils.withEntitySubsegment;
@@ -317,25 +338,33 @@ under a subsegment, or you are doing multithreaded programming. Refer examples b
317338

318339
## Instrumenting SDK clients and HTTP calls
319340

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
323342

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.
325344

326-
```java hl_lines="1 2 7"
327-
import com.amazonaws.xray.AWSXRay;
328-
import com.amazonaws.xray.handlers.TracingHandler;
345+
If you need more control over which clients are instrumented, you can manually add the `TracingInterceptor` to specific clients:
346+
347+
=== "Manual instrumentation (optional)"
348+
349+
```java hl_lines="1 2 3 8 9 10 11"
350+
import com.amazonaws.xray.interceptors.TracingInterceptor;
351+
import software.amazon.awssdk.core.client.config.ClientOverrideConfiguration;
352+
import software.amazon.awssdk.services.dynamodb.DynamoDbClient;
329353

330354
public class LambdaHandler {
331-
private AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard()
332-
.withRegion(Regions.fromName(System.getenv("AWS_REGION")))
333-
.withRequestHandlers(new TracingHandler(AWSXRay.getGlobalRecorder()))
355+
private DynamoDbClient client = DynamoDbClient.builder()
356+
.region(Region.US_WEST_2)
357+
.overrideConfiguration(ClientOverrideConfiguration.builder()
358+
.addExecutionInterceptor(new TracingInterceptor())
359+
.build()
360+
)
334361
.build();
335362
// ...
336363
}
337364
```
338365

366+
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+
339368
## Testing your code
340369

341370
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
351380

352381
=== "Maven (pom.xml)"
353382

354-
```xml hl_lines="4-13"
383+
```xml
355384
<build>
356385
...
357386
<plugins>
@@ -370,9 +399,9 @@ used internally via AWS X-Ray SDK to configure itself properly for lambda runtim
370399

371400
```
372401

373-
=== "Gradle (build.gradle) "
402+
=== "Gradle (build.gradle)"
374403

375-
```json hl_lines="2-4"
404+
```json
376405
// Configures environment variable to avoid initialization of AWS X-Ray segments for each tests
377406
test {
378407
environment "LAMBDA_TASK_ROOT", "handler"
@@ -418,6 +447,3 @@ Below is an example configuration needed for each test case.
418447
// test logic
419448
}
420449
```
421-
422-
423-

0 commit comments

Comments
 (0)