fix: guard global.metrics in singleFlightMiddleware - #670
Open
mateussaggin wants to merge 2 commits into
Open
Conversation
mateussaggin
requested review from
caroolcanelas2,
daniyelnnr and
silvadenisaraujo
July 28, 2026 02:38
juliobguedes
approved these changes
Jul 28, 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.

What
Guards the bare
metricsglobal insingleFlightMiddleware, and fixes the size reported by its flush metric.Why
Until v7,
global.metrics = new MetricsAccumulator()was a module-load side effect ofservice/Runtime.ts, so requiring the package initialized it. In v7 that moved insidestartApp()(service/index.ts), which only runs on the IO service runtime.singleFlightMiddlewarestill referencesmetricsas a bare global. Any standalone consumer ofHttpClient— one that never boots the service runtime — therefore crashes on the first request that sets aninflightKey:This is what broke the toolbelt CLI in v4.4.2 when it bumped to
@vtex/api@7.4.0(see vtex/toolbelt#1276 and vtex/toolbelt#1277). The toolbelt PR fixes the CLI by initializing the global itself; this PR makes the library not depend on a global only its own service runtime creates, so the whole class of bug can't recur for other consumers.Note
metricsMiddlewarealready takesmetricsas an explicit (optional) parameter and guards it —inflight.tsis the one place still reaching for the bare global.Also
entriesis aMapmethod, soinflight.entries.lengthevaluated to the function's arity (0) rather than the number of inflight requests. Thenode-vtex-api-inflight-map-sizemetric has been reporting a constant zero.Behavior
Inside the service runtime nothing changes:
global.metricsis defined, the flush metric is registered as before (now with a correct size). Outside it, the middleware works and simply skips the legacy metric registration.