/** * Accumulates per-request metrics for Provider API calls. * * Created once per incoming request (in the start middleware), threaded through Context and RequestOptions, * incremented by Provider on each API call, and logged by the finish middleware. */ export class RequestMetrics { private _apiCallCount = 0; private _totalApiDurationNs = 0; private _totalThrottleDelayNs = 0; private _requestStartTime: bigint; private constructor() { this._requestStartTime = process.hrtime.bigint(); } static startRequest(): RequestMetrics { return new RequestMetrics(); } endRequest(): { durationNs: bigint; apiCallCount: number; totalApiDurationNs: number; totalThrottleDelayNs: number; } { return { durationNs: process.hrtime.bigint() - this._requestStartTime, apiCallCount: this._apiCallCount, totalApiDurationNs: this._totalApiDurationNs, totalThrottleDelayNs: this._totalThrottleDelayNs, }; } /** * Record a completed API call. * Increments the call counter and accumulates the call's network duration and pre-flight * throttle delay (time spent waiting in the rate limiter before the request was dispatched). */ recordExternalApiCall(duration: number, throttleDelay: number): void { this._apiCallCount++; this._totalApiDurationNs += duration; this._totalThrottleDelayNs += throttleDelay; } }