Skip to content

fix(event-handler): avoid runtime import of metrics package in HTTP metrics middleware#5310

Merged
svozza merged 6 commits into
aws-powertools:mainfrom
vishwakt:fix/5309-event-handler-metrics-runtime-import
Jun 3, 2026
Merged

fix(event-handler): avoid runtime import of metrics package in HTTP metrics middleware#5310
svozza merged 6 commits into
aws-powertools:mainfrom
vishwakt:fix/5309-event-handler-metrics-runtime-import

Conversation

@vishwakt
Copy link
Copy Markdown
Contributor

@vishwakt vishwakt commented Jun 2, 2026

Summary

Changes

The HTTP metrics middleware (packages/event-handler/src/http/middleware/metrics.ts) did a value import of MetricUnit from @aws-lambda-powertools/metrics:

import { MetricUnit } from '@aws-lambda-powertools/metrics';

Because MetricUnit is a runtime value (not a type), bundlers such as esbuild retain this import even when the consuming code only uses the Router and other middleware (e.g. cors) and never opts into the metrics middleware. The middleware barrel (http/middleware/index.ts) re-exports metrics alongside cors, and the package does not declare sideEffects: false, so a Router + cors-only bundle keeps metrics.js and its MetricUnit import.

Since @aws-lambda-powertools/metrics is an optional peer dependency, bundles that do not install it (and have no Powertools Lambda layer) crash at init on AWS Lambda with ERR_MODULE_NOT_FOUND, returning HTTP 502 behind API Gateway.

This change inlines the metric unit string literals ('Milliseconds', 'Count') instead of importing the MetricUnit value. The literals are type-safe because addMetric's unit parameter 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 a Router + 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.

…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.
@powertools-for-aws-oss-automation powertools-for-aws-oss-automation Bot added size/M PR between 30-99 LOC and removed size/M PR between 30-99 LOC labels Jun 2, 2026
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.
@powertools-for-aws-oss-automation powertools-for-aws-oss-automation Bot added size/M PR between 30-99 LOC and removed size/M PR between 30-99 LOC labels Jun 2, 2026
Copy link
Copy Markdown
Contributor

@dreamorosi dreamorosi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the quick PR - left a comment to remove the test case added in this PR.

Comment thread packages/event-handler/tests/unit/http/middleware/metrics.test.ts Outdated
Remove the import guard test per maintainer feedback.
@powertools-for-aws-oss-automation powertools-for-aws-oss-automation Bot added size/S PR between 10-29 LOC and removed size/M PR between 30-99 LOC labels Jun 2, 2026
@powertools-for-aws-oss-automation powertools-for-aws-oss-automation Bot added size/S PR between 10-29 LOC and removed size/S PR between 10-29 LOC labels Jun 3, 2026
@powertools-for-aws-oss-automation powertools-for-aws-oss-automation Bot added size/S PR between 10-29 LOC and removed size/S PR between 10-29 LOC labels Jun 3, 2026
@sonarqubecloud
Copy link
Copy Markdown

sonarqubecloud Bot commented Jun 3, 2026

@svozza svozza merged commit f4a6752 into aws-powertools:main Jun 3, 2026
36 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/S PR between 10-29 LOC

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: event-handler HTTP metrics middleware forces a runtime import of Metrics

3 participants