Skip to content

docs(event_handler): demonstrate how to combine logger correlation ID and middleware #3064

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
2 changes: 1 addition & 1 deletion docs/core/event_handler/api_gateway.md
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ Here's a sample middleware that extracts and injects correlation ID, using `APIG
```

1. You can access current request like you normally would.
2. [Shared context is available](#sharing-contextual-data) to any middleware, Router and App instances.
2. [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.
3. Get response from the next middleware (if any) or from `/todos` route.
4. You can manipulate headers, body, or status code before returning it.
5. Register one or more middlewares in order of execution.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def inject_correlation_id(app: APIGatewayRestResolver, next_middleware: NextMidd
request_id = app.current_event.request_context.request_id # (1)!

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

# Inject correlation ID in shared context and Logger
app.append_context(correlation_id=correlation_id) # (2)!
Expand All @@ -35,6 +35,6 @@ def get_todos():
return {"todos": todos.json()[:10]}


@logger.inject_lambda_context
@logger.inject_lambda_context(correlation_id_path='headers."x-correlation-id"')
def lambda_handler(event, context):
return app.resolve(event, context)
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def inject_correlation_id(app: APIGatewayRestResolver, next_middleware: NextMidd
request_id = app.current_event.request_context.request_id

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

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