@unito/integration-sdk
Version:
Integration SDK
37 lines (36 loc) • 1.28 kB
JavaScript
/**
* 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 {
_apiCallCount = 0;
_totalApiDurationNs = 0;
_totalThrottleDelayNs = 0;
_requestStartTime;
constructor() {
this._requestStartTime = process.hrtime.bigint();
}
static startRequest() {
return new RequestMetrics();
}
endRequest() {
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, throttleDelay) {
this._apiCallCount++;
this._totalApiDurationNs += duration;
this._totalThrottleDelayNs += throttleDelay;
}
}