Skip to content

Commit ad47e65

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

File tree

1 file changed

+101
-0
lines changed

1 file changed

+101
-0
lines changed

docs/core/logging.md

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,107 @@ 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="12"
161+
{
162+
"level": "INFO",
163+
"location": "collect.handler:7",
164+
"message": "Collecting payment",
165+
"timestamp": "2021-05-03 11:47:12,494+0200",
166+
"service": "payment",
167+
"cold_start": true,
168+
"lambda_function_name": "test",
169+
"lambda_function_memory_size": 128,
170+
"lambda_function_arn": "arn:aws:lambda:eu-west-1:12345678910:function:test",
171+
"lambda_request_id": "52fdfc07-2182-154f-163f-5f0f9a621d72",
172+
"correlation_id": "correlation_id_value"
173+
}
174+
```
175+
We provide [built-in JSON Pointer expression](https://datatracker.ietf.org/doc/html/draft-ietf-appsawg-json-pointer-03){target="_blank"}
176+
for known event sources, where either a request ID or X-Ray Trace ID are present.
177+
178+
=== "App.java"
179+
180+
```java hl_lines="10"
181+
import software.amazon.lambda.powertools.logging.CorrelationIdPathConstants;
182+
183+
/**
184+
* Handler for requests to Lambda function.
185+
*/
186+
public class App implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> {
187+
188+
Logger log = LogManager.getLogger();
189+
190+
@Logging(correlationIdPath = CorrelationIdPathConstants.API_GATEWAY_REST)
191+
public APIGatewayProxyResponseEvent handleRequest(final APIGatewayProxyRequestEvent input, final Context context) {
192+
...
193+
log.info("Collecting payment")
194+
...
195+
}
196+
}
197+
```
198+
199+
=== "Example Event"
200+
201+
```json hl_lines="3"
202+
{
203+
"requestContext": {
204+
"requestId": "correlation_id_value"
205+
}
206+
}
207+
```
208+
209+
=== "Example CloudWatch Logs excerpt"
210+
211+
```json hl_lines="12"
212+
{
213+
"level": "INFO",
214+
"location": "collect.handler:8",
215+
"message": "Collecting payment",
216+
"timestamp": "2021-05-03 11:47:12,494+0200",
217+
"service": "payment",
218+
"cold_start": true,
219+
"lambda_function_name": "test",
220+
"lambda_function_memory_size": 128,
221+
"lambda_function_arn": "arn:aws:lambda:eu-west-1:12345678910:function:test",
222+
"lambda_request_id": "52fdfc07-2182-154f-163f-5f0f9a621d72",
223+
"correlation_id": "correlation_id_value"
224+
}
225+
```
226+
126227
## Appending additional keys
127228

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

0 commit comments

Comments
 (0)