Skip to content

Commit 3be62ac

Browse files
pankajagrawal16Pankaj Agrawal
and
Pankaj Agrawal
authored
Doc update for tracer (#53)
Co-authored-by: Pankaj Agrawal <[email protected]>
1 parent 2a23f72 commit 3be62ac

File tree

2 files changed

+99
-7
lines changed

2 files changed

+99
-7
lines changed

docs/content/core/tracing.mdx

Lines changed: 99 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import Note from "../../src/components/Note"
88
Powertools tracing is an opinionated thin wrapper for [AWS X-Ray Java SDK](https://github.com/aws/aws-xray-sdk-java/)
99
a provides functionality to reduce the overhead of performing common tracing tasks.
1010

11+
![Tracer showcase](../media/tracer_utility_showcase.png)
12+
1113
** Key Features
1214

1315
* Capture cold start as annotation, and responses as well as full exceptions as metadata
@@ -34,25 +36,115 @@ Resources:
3436
The Powertools service name is used as the X-Ray namespace. This can be set using the environment variable
3537
`POWERTOOLS_SERVICE_NAME`
3638

37-
To enable Powertools tracing to your function add the @PowertoolsTracing annotation to your handleRequest method.
39+
To enable Powertools tracing to your function add the @PowertoolsTracing annotation to your handleRequest method or on
40+
any method will capture the method as a separate subsegment automatically.
3841

39-
```java
42+
```java:title=LambdaHandler.java
4043
public class App implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> {
4144

4245
@PowertoolsTracing
4346
public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent input, Context context) {
44-
...
47+
businessLogic1();
48+
49+
businessLogic2();
50+
}
51+
52+
@PowertoolsTracing
53+
public void businessLogic1(){
54+
4555
}
56+
57+
@PowertoolsTracing
58+
public void businessLogic2(){
59+
60+
}
61+
}
4662
```
4763

48-
By default this annotation will automatically record method responses and exceptions. To disable these features set the
49-
parameter to false in the annotation.
64+
By default this annotation will automatically record method responses and exceptions.
5065

51-
```java
66+
<Note type="warning">
67+
<strong>Returning sensitive information from your Lambda handler or functions, where Tracer is used?</strong>
68+
<br/><br/>
69+
You can disable Tracer from capturing their responses and exception as tracing metadata with <strong><code>captureResponse=false</code></strong> and <strong><code>captureError=false</code></strong>
70+
</Note><br/>
71+
72+
```java:title=HandlerWithoutCapturingResponseOrError.java
5273
public class App implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> {
5374

5475
@PowertoolsTracing(captureError = false, captureResponse = false)
5576
public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent input, Context context) {
5677
...
5778
}
58-
```
79+
```
80+
81+
### Annotations
82+
83+
Annotations are key-values indexed by AWS X-Ray on a per trace basis. You can use them to filter traces as well as to create [Trace Groups](https://aws.amazon.com/about-aws/whats-new/2018/11/aws-xray-adds-the-ability-to-group-traces/).
84+
85+
You can add annotations using `putAnnotation()` method from PowerTracer and it will be correctly inject for the subsegment in concern.
86+
87+
```java
88+
public class App implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> {
89+
90+
@PowertoolsTracing
91+
public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent input, Context context) {
92+
PowerTracer.putAnnotation("annotation", "value");
93+
}
94+
}
95+
```
96+
97+
### Metadata
98+
99+
Metadata are non-indexed values that can add additional context for an operation.
100+
101+
You can add metadata using `putMetadata()` method from PowerTracer and it will be correctly inject for the subsegment in concern.
102+
103+
```java
104+
public class App implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> {
105+
106+
@PowertoolsTracing
107+
public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent input, Context context) {
108+
PowerTracer.putMetadata("content", "value");
109+
}
110+
}
111+
```
112+
113+
## Utilities
114+
115+
Tracer modules comes with certain utility method when you don't want to use annotation for capturing a code block
116+
under a subsegment, or you are doing multithreaded programming. Refer examples below.
117+
118+
```java:title=InlineSubsegmentCapture.java
119+
public class App implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> {
120+
121+
public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent input, Context context) {
122+
PowerTracer.withSubsegment("loggingResponse", subsegment -> {
123+
// Some business logic
124+
});
125+
126+
PowerTracer.withSubsegment("localNamespace", "loggingResponse", subsegment -> {
127+
// Some business logic
128+
});
129+
}
130+
}
131+
```
132+
133+
```java:title=ThreadedProgramming.java
134+
public class App implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> {
135+
136+
public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent input, Context context) {
137+
// Extract existing trace data
138+
Entity traceEntity = AWSXRay.getTraceEntity();
139+
140+
Thread anotherThread = new Thread(() -> withEntitySubsegment("inlineLog", traceEntity, subsegment -> {
141+
// Business logic in sperate thread
142+
}));
143+
}
144+
}
145+
```
146+
147+
## Instrumenting SDK clients and HTTP calls
148+
149+
User should make sure to instrument the SDK clients explicitly based on the function dependency. Refer details on
150+
[how to instrument SDK client with Xray](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).
128 KB
Loading

0 commit comments

Comments
 (0)