Skip to content

Commit b523583

Browse files
authored
docs(event_handler): demonstrate how to combine logger correlation ID and middleware (aws-powertools#3064)
* fix(parameters): make cache aware of single vs multiple calls Signed-off-by: heitorlessa <[email protected]> * chore: cleanup, add test for single and nested Signed-off-by: heitorlessa <[email protected]> * docs(event_handler): use correct correlation_id in logger.set_correlation_id example * docs(event_handler): combine correlation id with logger for a more powerful example * fix(parameters): make cache aware of single vs multiple calls Signed-off-by: heitorlessa <[email protected]> * chore: cleanup, add test for single and nested Signed-off-by: heitorlessa <[email protected]> * docs: remove Logger dependency on retrieval to address Simon's feedback * docs: fix racing condition, more notes based on Simon's feedback Signed-off-by: heitorlessa <[email protected]> --------- Signed-off-by: heitorlessa <[email protected]>
1 parent da61b63 commit b523583

File tree

3 files changed

+13
-11
lines changed

3 files changed

+13
-11
lines changed

docs/core/event_handler/api_gateway.md

+6-4
Original file line numberDiff line numberDiff line change
@@ -406,10 +406,12 @@ Here's a sample middleware that extracts and injects correlation ID, using `APIG
406406
```
407407

408408
1. You can access current request like you normally would.
409-
2. [Shared context is available](#sharing-contextual-data) to any middleware, Router and App instances.
410-
3. Get response from the next middleware (if any) or from `/todos` route.
411-
4. You can manipulate headers, body, or status code before returning it.
412-
5. Register one or more middlewares in order of execution.
409+
2. Logger extracts it first in the request path, so we can use it. <br><br> If this was available before, we'd use `app.context.get("correlation_id")`.
410+
3. [Shared context is available](#sharing-contextual-data) to any middleware, Router and App instances. <br><br> For example, another middleware can now use `app.context.get("correlation_id")` to retrieve it.
411+
4. Get response from the next middleware (if any) or from `/todos` route.
412+
5. You can manipulate headers, body, or status code before returning it.
413+
6. Register one or more middlewares in order of execution.
414+
7. Logger extracts correlation ID from header and makes it available under `correlation_id` key, and `get_correlation_id()` method.
413415

414416
=== "middleware_getting_started_output.json"
415417

examples/event_handler_rest/src/middleware_getting_started.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,21 @@ def inject_correlation_id(app: APIGatewayRestResolver, next_middleware: NextMidd
1212
request_id = app.current_event.request_context.request_id # (1)!
1313

1414
# Use API Gateway REST API request ID if caller didn't include a correlation ID
15-
correlation_id = app.current_event.headers.get("x-correlation-id", request_id)
15+
correlation_id = logger.get_correlation_id() or request_id # (2)!
1616

1717
# Inject correlation ID in shared context and Logger
18-
app.append_context(correlation_id=correlation_id) # (2)!
18+
app.append_context(correlation_id=correlation_id) # (3)!
1919
logger.set_correlation_id(correlation_id)
2020

2121
# Get response from next middleware OR /todos route
22-
result = next_middleware(app) # (3)!
22+
result = next_middleware(app) # (4)!
2323

2424
# Include Correlation ID in the response back to caller
25-
result.headers["x-correlation-id"] = correlation_id # (4)!
25+
result.headers["x-correlation-id"] = correlation_id # (5)!
2626
return result
2727

2828

29-
@app.get("/todos", middlewares=[inject_correlation_id]) # (5)!
29+
@app.get("/todos", middlewares=[inject_correlation_id]) # (6)!
3030
def get_todos():
3131
todos: Response = requests.get("https://jsonplaceholder.typicode.com/todos")
3232
todos.raise_for_status()
@@ -35,6 +35,6 @@ def get_todos():
3535
return {"todos": todos.json()[:10]}
3636

3737

38-
@logger.inject_lambda_context
38+
@logger.inject_lambda_context(correlation_id_path='headers."x-correlation-id"') # (7)!
3939
def lambda_handler(event, context):
4040
return app.resolve(event, context)

examples/event_handler_rest/src/middleware_global_middlewares_module.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def inject_correlation_id(app: APIGatewayRestResolver, next_middleware: NextMidd
1818
request_id = app.current_event.request_context.request_id
1919

2020
# Use API Gateway REST API request ID if caller didn't include a correlation ID
21-
correlation_id = app.current_event.headers.get("x-correlation-id", request_id)
21+
correlation_id = logger.get_correlation_id() or request_id # elsewhere becomes app.context.get("correlation_id")
2222

2323
# Inject correlation ID in shared context and Logger
2424
app.append_context(correlation_id=correlation_id)

0 commit comments

Comments
 (0)