Skip to content

Commit 2ebe301

Browse files
author
Pankaj Agrawal
committed
docs: correlation id extraction
1 parent 020414d commit 2ebe301

File tree

1 file changed

+99
-0
lines changed

1 file changed

+99
-0
lines changed

docs/core/logging.md

+99
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,105 @@ to customise what is logged.
123123
}
124124
```
125125

126+
## Setting a Correlation ID
127+
128+
You can set a Correlation ID using `correlationIdPath` attribute by passing a [JSON Pointer expression](https://datatracker.ietf.org/doc/html/draft-ietf-appsawg-json-pointer-03){target="_blank"}.
129+
130+
=== "App.java"
131+
132+
```java hl_lines="8"
133+
/**
134+
* Handler for requests to Lambda function.
135+
*/
136+
public class App implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> {
137+
138+
Logger log = LogManager.getLogger();
139+
140+
@Logging(correlationIdPath = "/headers/my_request_id_header")
141+
public APIGatewayProxyResponseEvent handleRequest(final APIGatewayProxyRequestEvent input, final Context context) {
142+
...
143+
log.info("Collecting payment")
144+
...
145+
}
146+
}
147+
```
148+
=== "Example Event"
149+
150+
```json hl_lines="3"
151+
{
152+
"headers": {
153+
"my_request_id_header": "correlation_id_value"
154+
}
155+
}
156+
```
157+
158+
=== "Example CloudWatch Logs excerpt"
159+
160+
```json hl_lines="11"
161+
{
162+
"level": "INFO",
163+
"message": "Collecting payment",
164+
"timestamp": "2021-05-03 11:47:12,494+0200",
165+
"service": "payment",
166+
"coldStart": true,
167+
"functionName": "test",
168+
"functionMemorySize": 128,
169+
"functionArn": "arn:aws:lambda:eu-west-1:12345678910:function:test",
170+
"lambda_request_id": "52fdfc07-2182-154f-163f-5f0f9a621d72",
171+
"correlation_id": "correlation_id_value"
172+
}
173+
```
174+
We provide [built-in JSON Pointer expression](https://datatracker.ietf.org/doc/html/draft-ietf-appsawg-json-pointer-03){target="_blank"}
175+
for known event sources, where either a request ID or X-Ray Trace ID are present.
176+
177+
=== "App.java"
178+
179+
```java hl_lines="10"
180+
import software.amazon.lambda.powertools.logging.CorrelationIdPathConstants;
181+
182+
/**
183+
* Handler for requests to Lambda function.
184+
*/
185+
public class App implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> {
186+
187+
Logger log = LogManager.getLogger();
188+
189+
@Logging(correlationIdPath = CorrelationIdPathConstants.API_GATEWAY_REST)
190+
public APIGatewayProxyResponseEvent handleRequest(final APIGatewayProxyRequestEvent input, final Context context) {
191+
...
192+
log.info("Collecting payment")
193+
...
194+
}
195+
}
196+
```
197+
198+
=== "Example Event"
199+
200+
```json hl_lines="3"
201+
{
202+
"requestContext": {
203+
"requestId": "correlation_id_value"
204+
}
205+
}
206+
```
207+
208+
=== "Example CloudWatch Logs excerpt"
209+
210+
```json hl_lines="11"
211+
{
212+
"level": "INFO",
213+
"message": "Collecting payment",
214+
"timestamp": "2021-05-03 11:47:12,494+0200",
215+
"service": "payment",
216+
"coldStart": true,
217+
"functionName": "test",
218+
"functionMemorySize": 128,
219+
"functionArn": "arn:aws:lambda:eu-west-1:12345678910:function:test",
220+
"lambda_request_id": "52fdfc07-2182-154f-163f-5f0f9a621d72",
221+
"correlation_id": "correlation_id_value"
222+
}
223+
```
224+
126225
## Appending additional keys
127226

128227
You can append your own keys to your existing logs via `appendKey`.

0 commit comments

Comments
 (0)