fix(event-handler): avoid runtime import of metrics package in HTTP metrics middleware#5310
Merged
svozza merged 6 commits intoJun 3, 2026
Conversation
…etrics middleware The HTTP metrics middleware did a value import of `MetricUnit` from `@aws-lambda-powertools/metrics`. Because `MetricUnit` is a runtime value, bundlers retained the import even for consumers that only use the `Router` and other middleware and never opt into metrics. Since the metrics package is an optional peer dependency, bundles without it installed crashed at init on Lambda with `ERR_MODULE_NOT_FOUND`. Inline the metric unit string literals instead, matching the type-only approach already used by the tracer middleware, so no runtime dependency on the metrics package leaks into Router-only bundles. Closes aws-powertools#5309
…uard Replace the comment-stripping and import-matching regexes in the metrics middleware import guard with a linear character scan and string checks, to clear the static-analysis security hotspots flagged on CI. The guard still fails if a runtime value import of the metrics package is reintroduced.
Drop the hand-rolled comment-stripping scanner in favour of a plain statement split. Comments never trim to start with `import`, so they cannot cause a false positive, and splitting on the statement terminator keeps multi-line imports covered without any regex.
dreamorosi
requested changes
Jun 2, 2026
Contributor
dreamorosi
left a comment
There was a problem hiding this comment.
Thank you for the quick PR - left a comment to remove the test case added in this PR.
Remove the import guard test per maintainer feedback.
dreamorosi
approved these changes
Jun 2, 2026
dreamorosi
approved these changes
Jun 3, 2026
svozza
approved these changes
Jun 3, 2026
|
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.



Summary
Changes
The HTTP metrics middleware (
packages/event-handler/src/http/middleware/metrics.ts) did a value import ofMetricUnitfrom@aws-lambda-powertools/metrics:Because
MetricUnitis a runtime value (not a type), bundlers such as esbuild retain this import even when the consuming code only uses theRouterand other middleware (e.g.cors) and never opts into the metrics middleware. The middleware barrel (http/middleware/index.ts) re-exportsmetricsalongsidecors, and the package does not declaresideEffects: false, so aRouter + cors-only bundle keepsmetrics.jsand itsMetricUnitimport.Since
@aws-lambda-powertools/metricsis an optional peer dependency, bundles that do not install it (and have no Powertools Lambda layer) crash at init on AWS Lambda withERR_MODULE_NOT_FOUND, returning HTTP 502 behind API Gateway.This change inlines the metric unit string literals (
'Milliseconds','Count') instead of importing theMetricUnitvalue. The literals are type-safe becauseaddMetric'sunitparameter is typed as the union of those exact string literals. This matches the type-only approach already used by the sibling tracer middleware (http/middleware/tracer.ts), so no runtime dependency on the metrics package leaks into Router-only bundles.A regression test asserts that the middleware source carries no value (non-
type) import from@aws-lambda-powertools/metrics. Verified that reintroducing the value import makes the test fail. Also verified manually by bundling aRouter + cors-only handler with esbuild (format: 'esm',bundle: true,platform: 'node'): after the change the output contains no reference to@aws-lambda-powertools/metrics.Issue number: closes #5309
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.