diff --git a/src/HttpClient/middlewares/inflight.ts b/src/HttpClient/middlewares/inflight.ts index 4197e010b..5b17df1d1 100644 --- a/src/HttpClient/middlewares/inflight.ts +++ b/src/HttpClient/middlewares/inflight.ts @@ -17,10 +17,13 @@ export const singleFlightMiddleware = async (ctx: MiddlewareContext, next: () => // cancel any request ctx.config.cancelToken = undefined - if (!metricsAdded) { + // global.metrics is only initialized by startApp() in the IO service + // runtime. Standalone consumers of HttpClient (e.g. the toolbelt CLI) + // run without it, so guard the bare global reference. + if (!metricsAdded && typeof metrics !== 'undefined') { metrics.addOnFlushMetric(() => ({ name: 'node-vtex-api-inflight-map-size', - size: inflight.entries.length, + size: inflight.size, })) metricsAdded = true }