Skip to content

feat(bb-logger,core): backend Lambda log lines carry the invocation's requestId - #1325

Merged
osama-rizk merged 5 commits into
mainfrom
feat/logger-request-id
Nov 6, 2026
Merged

osama-rizk merged 5 commits into
mainfrom
feat/logger-request-id

Conversation

@osama-rizk

@osama-rizk osama-rizk commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

Problem

Nothing tied the log lines of one request together. A Lambda container handles many invocations, and a Logger line carried only level, 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 every child() call.

Changes

  • packages/core/src/request-context.ts: a per-invocation AsyncLocalStorage store for the request id, plus currentRequestId(), exported for Building Block authors from @aws-blocks/core/bb-utils. The store is registered on globalThis.__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: createLambdaHandler runs each invocation inside that store with the Lambda context's awsRequestId. 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. LambdaContext gains the optional awsRequestId. 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 gets awsRequestId right after logger when an id is in scope, read with currentRequestId() from @aws-blocks/core/bb-utils. The degraded line written on a serialization failure keeps awsRequestId, requestId and traceId.
    • awsRequestId is a reserved field, like traceId: a context value under that key is ignored. It has its own key so an app's own requestId context field (a common name for an app-chosen correlation id) is kept as is and can't split one request across two ids.
  • Types and docs: LogEntry.awsRequestId (JSDoc, regenerated API.md); the README log-format section, with a CloudWatch Logs Insights filter awsRequestId = … example and the local-dev note; DESIGN.md "Serialization Format" and "Mock vs AWS Behavior Differences".
  • Changeset: @aws-blocks/bb-logger minor (new field on every AWS log line); @aws-blocks/core and @aws-blocks/blocks patch.

Verification

Red is the new tests run with lambda-handler.ts, request-context.ts and serializer.ts reverted to main; green is the same tests with this PR.

  • core/src/lambda-handler.test.ts: the backend method sees the invocation's awsRequestId; 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.
    • Red against main: makes the invocation awsRequestId visible… and keeps overlapping invocations apart fail. Red against the store-guard and init-scope fixes reverted: imports the backend outside the first invocation request id and replaces a non-store value at the globalThis key… fail (150 pass, 2 fail).
    • Green: 6/6.
  • bb-logger/src/request-id.test.ts: the line carries awsRequestId inside an invocation; there's none outside one; a context requestId is kept beside it; a context awsRequestId is ignored; overlapping async invocations each log their own id; key order is level, 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.
    • Green: 8/8.
  • test-apps/comprehensive/test/logging.test.ts (E2E): locally, lines carry no awsRequestId. 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 own requestId beside 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).
  • Suites: core passes 2428/2428 (1 skipped), bb-logger 69/69, packages/blocks 433/433 (2 skipped) and 51/51. npm run lint:deps is clean, and npm run check:api passes after update:api (the only change is awsRequestId?: string on LogEntry). biome lint finding counts on the touched source files match main.

Not changed

  • Local dev. The dev server sets no request id, so local lines don't have the field. That's documented as a mock/AWS difference.
  • Other compute. Only createLambdaHandler sets the id. SSR hosting web functions, container compute and AgentCore runtimes don't yet, and the docs say so.

…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.
@osama-rizk
osama-rizk requested a review from a team as a code owner November 6, 2026 10:14
@osama-rizk osama-rizk self-assigned this Nov 6, 2026
@changeset-bot

changeset-bot Bot commented Nov 6, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 1adfc9b

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 23 packages
Name Type
@aws-blocks/bb-logger Minor
@aws-blocks/core Patch
@aws-blocks/blocks Patch
@aws-blocks/bb-kv-store Patch
@aws-blocks/bb-distributed-table Patch
@aws-blocks/data-common Patch
@aws-blocks/bb-app-setting Patch
@aws-blocks/bb-secret Patch
@aws-blocks/bb-data Patch
@aws-blocks/bb-distributed-data Patch
@aws-blocks/bb-database Patch
@aws-blocks/bb-auth-basic Patch
@aws-blocks/bb-auth-cognito Patch
@aws-blocks/bb-auth-oidc Patch
@aws-blocks/bb-realtime Patch
@aws-blocks/bb-async-job Patch
@aws-blocks/bb-cron-job Patch
@aws-blocks/bb-file-bucket Patch
@aws-blocks/bb-agent Patch
@aws-blocks/bb-knowledge-base Patch
@aws-blocks/bb-email-client Patch
@aws-blocks/bb-tracer Patch
@aws-blocks/bb-metrics Patch

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 osama-rizk changed the title feat(bb-logger,core): every log line on AWS carries the invocation's requestId feat(bb-logger,core): backend Lambda log lines carry the invocation's requestId Nov 6, 2026
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.
@osama-rizk

osama-rizk commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks for the review. All of it is addressed:

  • E2E blocker: fixed in d287cae by moving the Lambda id to its own reserved key, awsRequestId (details on the serializer.ts thread). logging.test.ts:172 ("separate child loggers have independent context") is unchanged and holds on a deployed run, because the app's requestId field is no longer touched.
  • E2E coverage: d287cae adds an invocation request id group to test-apps/comprehensive/test/logging.test.ts, using captureLogOutput. 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 own requestId beside one shared invocation id. Locally, lines carry no awsRequestId. npm run test:e2e:local: 398 pass, 0 fail, 4 skipped (the 2 deployed-only tests among them).
  • JSON.stringify fallback: the degraded line keeps requestId and traceId since 1a1b0f4, and now awsRequestId too. Non-string values are dropped so the fallback can't throw again (tests in request-id.test.ts).
  • Title and scope: the title now says "backend Lambda log lines", and the changeset, README and DESIGN scope the field to createLambdaHandler functions. SSR hosting web functions, container compute and AgentCore are listed as not setting it.

This branch had an error being deployed

1 failed and 1 inactive deployments
e2e — 1adfc9b6 Deployed Nov 6, 2026 by osama-rizk via E2E Teardown Verify #5525
agent-bench — 1adfc9b6 Deployed Nov 6, 2026 by osama-rizk via bench / summary #4786
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants