Skip to content

refactor(commons): extract InvokeStore fallback into InvocationScoped - #5615

Merged
svozza merged 2 commits into
mainfrom
ref/invocation-scoped-primitive
Sep 4, 2026
Merged

svozza merged 2 commits into
mainfrom
ref/invocation-scoped-primitive

Conversation

@dreamorosi

Copy link
Copy Markdown
Contributor

Summary

Extracts repeated invocation-scoping behavior from seven logger, metrics, and batch stores into a tested commons primitive. This removes inconsistent fallback and error behavior while leaving each store responsible only for its domain logic.

Changes

  • Replace 36 repeated accessor branches across seven stores. Previously, only LogInvocationStore.setLogLevel guarded on hasContext(), while BatchProcessingStore asserted InvokeStore in only 2 of 14 accessors, producing inconsistent TypeError and Error failures.
  • Add the internal InvocationScoped<T> commons subpath export at ./utils/invocation-scoped, with direct coverage for the flag-off, missing-global, inactive-context, and scoped modes. Scoped and shared slots use Box<T> so explicit undefined shadows the shared value while an unset slot falls back.
  • Migrate the logger, metrics, and batch stores to invocation-scoped cells. Writes outside an active invocation use shared storage, every accessor reports InvokeStore is not available when required, and MetricsStore.clearMetrics clears its timestamp consistently in both modes.
  • Move @aws/lambda-invoke-store to devDependencies in logger, metrics, and batch because their sources no longer import it; commons retains the runtime augmentation while tests continue using it directly.
  • Notes for reviewers:
    • Private pass-through getters remain in the stores.
    • Box<T> deliberately distinguishes an explicit undefined value from an unset slot.
    • Commons retains the bare import '@aws/lambda-invoke-store' for runtime augmentation.
    • Batch errors pushed before prepare() now persist under concurrency; this is deliberate unification and is not externally observable.
    • LogInvocationStore continues relying on the existing configureBuffer() precondition.
    • Copy semantics remain unchanged and out of scope.
    • Tracer is deliberately untouched because its fallback is the X-Ray SDK context rather than an instance field.
  • Verification: 100% unit coverage in commons (238), logger (238), metrics (164), and batch (128); tracer has 113 passing tests; lint:ci is clean.

Issue number: closes #5554


By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.

Seven *Store classes across logger, metrics, and batch each re-implemented the same
per-accessor branch 36 times: check AWS_LAMBDA_MAX_CONCURRENCY, assert InvokeStore,
get or create under a symbol key, or touch an instance fallback. They had drifted:
only LogInvocationStore.setLogLevel guarded on hasContext(), and BatchProcessingStore
asserted InvokeStore in 2 of 14 accessors, so failures varied between TypeError and Error.

Add InvocationScoped<T> to commons as the @internal ./utils/invocation-scoped
subpath export. Test the full mode matrix: flag off, flag on without the global,
flag on without an active context, and scoped. Box scoped and shared slots so an
explicit undefined shadows the shared value, while an unset slot falls back.

Migrate the seven stores to declare cells and retain only domain logic. Unify behavior
as fixes: writes outside an active invocation context use the shared value; every
accessor throws 'InvokeStore is not available' when the flag is on but the global is
missing; MetricsStore.clearMetrics clears the timestamp identically in both modes.

Move @aws/lambda-invoke-store to devDependencies in logger, metrics, and batch.
Their sources no longer import it, commons carries the runtime augmentation, and tests
still use it directly.

Keep copy semantics unchanged and out of scope. Leave tracer untouched because its
fallback is the X-Ray SDK context rather than an instance field.

Refs #5554
@powertools-for-aws-oss-automation powertools-for-aws-oss-automation Bot added the size/XXL PRs with 1K+ LOC, largely documentation related label Sep 3, 2026
@dreamorosi
dreamorosi requested a review from svozza September 3, 2026 15:47
@svozza
svozza merged commit e352cb0 into main Sep 4, 2026
85 checks passed
@svozza
svozza deleted the ref/invocation-scoped-primitive branch September 4, 2026 08:01

This branch was previously deployed

1 inactive deployment
e2e-tests — afa6ec62 Deployed Sep 4, 2026 by svozza via teardown-lmi-capacity-providers #883
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/XXL PRs with 1K+ LOC, largely documentation related

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Maintenance: extract the InvokeStore/instance-fallback branch into a shared primitive

2 participants