Repository navigation
feat(bb-logger,core): backend Lambda log lines carry the invocation's requestId - #1325
Merged
Merged
Conversation
…requestId Logs from one request couldn't be tied together. Core's createLambdaHandler now runs each invocation inside an AsyncLocalStorage scope holding its awsRequestId (registered on globalThis so every copy of core shares it), and bb-logger adds it to each line as requestId. A requestId passed in context still wins; local dev sets none.
🦋 Changeset detectedLatest commit: 1adfc9b The changes in this PR will be included in the next version bump. This PR includes changesets to release 23 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
osama-rizk
temporarily deployed
to
agent-bench
November 6, 2026 10:16 — with
GitHub Actions
Inactive
osama-rizk
temporarily deployed
to
agent-bench
November 6, 2026 10:16 — with
GitHub Actions
Inactive
osama-rizk
temporarily deployed
to
agent-bench
November 6, 2026 10:16 — with
GitHub Actions
Inactive
osama-rizk
temporarily deployed
to
agent-bench
November 6, 2026 10:16 — with
GitHub Actions
Inactive
osama-rizk
temporarily deployed
to
agent-bench
November 6, 2026 10:16 — with
GitHub Actions
Inactive
osama-rizk
temporarily deployed
to
agent-bench
November 6, 2026 10:16 — with
GitHub Actions
Inactive
osama-rizk
temporarily deployed
to
agent-bench
November 6, 2026 10:16 — with
GitHub Actions
Inactive
osama-rizk
temporarily deployed
to
agent-bench
November 6, 2026 12:36 — with
GitHub Actions
Inactive
osama-rizk
temporarily deployed
to
agent-bench
November 6, 2026 12:36 — with
GitHub Actions
Inactive
osama-rizk
temporarily deployed
to
agent-bench
November 6, 2026 12:36 — with
GitHub Actions
Inactive
osama-rizk
temporarily deployed
to
agent-bench
November 6, 2026 12:36 — with
GitHub Actions
Inactive
osama-rizk
temporarily deployed
to
agent-bench
November 6, 2026 12:36 — with
GitHub Actions
Inactive
osama-rizk
temporarily deployed
to
agent-bench
November 6, 2026 12:36 — with
GitHub Actions
Inactive
osama-rizk
temporarily deployed
to
agent-bench
November 6, 2026 12:36 — with
GitHub Actions
Inactive
osama-rizk
temporarily deployed
to
agent-bench
November 6, 2026 12:36 — with
GitHub Actions
Inactive
osama-rizk
temporarily deployed
to
agent-bench
November 6, 2026 12:36 — with
GitHub Actions
Inactive
osama-rizk
temporarily deployed
to
agent-bench
November 6, 2026 12:36 — with
GitHub Actions
Inactive
osama-rizk
had a problem deploying
to
agent-bench
November 6, 2026 13:32 — with
GitHub Actions
Failure
An app's own requestId context field is now left alone and can't replace the invocation id, so every line one request logs shares one id. Adds deployed E2E checks that each call logs one UUID and calls differ.
Contributor
Author
|
Thanks for the review. All of it is addressed:
|
belaboe
approved these changes
Nov 6, 2026
This branch had an error being deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Nothing tied the log lines of one request together. A Lambda container handles many invocations, and a
Loggerline carried onlylevel,message,timestamp,logger,traceId(when X-Ray is on) and caller context. So to find "everything this failing request logged", an app had to thread its own correlation id through everychild()call.Changes
packages/core/src/request-context.ts: a per-invocationAsyncLocalStoragestore for the request id, pluscurrentRequestId(), exported for Building Block authors from@aws-blocks/core/bb-utils. The store is registered onglobalThis.__BLOCKS_REQUEST_ID_STORE__, and a second copy of core reuses it, so a Building Block that resolves its own copy still sees the id. A value at that key that isn't a store is replaced.packages/core/src/lambda-handler.ts:createLambdaHandlerruns each invocation inside that store with the Lambda context'sawsRequestId. Init (config load, the backend import, Building Block constructors) runs outside it, so long-lived state created there doesn't keep the first request's id.LambdaContextgains the optionalawsRequestId. Every event type (API Gateway, SQS, EventBridge, WebSocket) is covered, because the wrap is around the whole invocation.packages/bb-logger/src/serializer.ts: each line getsawsRequestIdright afterloggerwhen an id is in scope, read withcurrentRequestId()from@aws-blocks/core/bb-utils. The degraded line written on a serialization failure keepsawsRequestId,requestIdandtraceId.awsRequestIdis a reserved field, liketraceId: a context value under that key is ignored. It has its own key so an app's ownrequestIdcontext field (a common name for an app-chosen correlation id) is kept as is and can't split one request across two ids.LogEntry.awsRequestId(JSDoc, regeneratedAPI.md); the README log-format section, with a CloudWatch Logs Insightsfilter awsRequestId = …example and the local-dev note; DESIGN.md "Serialization Format" and "Mock vs AWS Behavior Differences".@aws-blocks/bb-loggerminor (new field on every AWS log line);@aws-blocks/coreand@aws-blocks/blockspatch.Verification
Red is the new tests run with
lambda-handler.ts,request-context.tsandserializer.tsreverted to main; green is the same tests with this PR.core/src/lambda-handler.test.ts: the backend method sees the invocation'sawsRequestId; overlapping invocations, with one held on a gate while the other completes, each see their own id; no id outside an invocation; the backend import runs outside the first invocation's id; a non-store value at the global key is replaced, and a second copy of the module reuses the registered store.makes the invocation awsRequestId visible…andkeeps overlapping invocations apartfail. Red against the store-guard and init-scope fixes reverted:imports the backend outside the first invocation request idandreplaces a non-store value at the globalThis key…fail (150 pass, 2 fail).bb-logger/src/request-id.test.ts: the line carriesawsRequestIdinside an invocation; there's none outside one; a contextrequestIdis kept beside it; a contextawsRequestIdis ignored; overlapping async invocations each log their own id; key order islevel, message, timestamp, logger, awsRequestId; the degraded line on a serialization failure keeps the correlation ids and drops non-string ones. The tests use the store core registers, not a stand-in.test-apps/comprehensive/test/logging.test.ts(E2E): locally, lines carry noawsRequestId. On a deployed backend, every line of one call carries the same UUID, two calls carry different ones, and the child-logger call keeps the app's ownrequestIdbeside one shared invocation id. The existing "separate child loggers have independent context" test passes unchanged on both.npm run test:e2e:local: 398 pass, 0 fail, 4 skipped (including the 2 deployed-only tests).packages/blocks433/433 (2 skipped) and 51/51.npm run lint:depsis clean, andnpm run check:apipasses afterupdate:api(the only change isawsRequestId?: stringonLogEntry).biome lintfinding counts on the touched source files match main.Not changed
createLambdaHandlersets the id. SSR hosting web functions, container compute and AgentCore runtimes don't yet, and the docs say so.