Skip to content

Commit

Permalink
fix: improve metrics for num active requests (#342)
Browse files Browse the repository at this point in the history
* fix: improve metrics for num active requests

We now capture the number of active requests both at the start
and the end of a given request, because it may change a lot
while the request is being processed.
  • Loading branch information
cprice404 authored Mar 21, 2023
1 parent 04d6703 commit 822d48f
Showing 1 changed file with 42 additions and 5 deletions.
47 changes: 42 additions & 5 deletions src/config/middleware/experimental-metrics-csv-middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import {MomentoLogger, MomentoLoggerFactory} from '../logging/momento-logger';

function headerFields(): Array<string> {
return [
'numActiveRequests',
'numActiveRequestsAtStart',
'numActiveRequestsAtFinish',
'requestType',
'status',
'startTime',
Expand All @@ -19,14 +20,45 @@ function headerFields(): Array<string> {
}

interface RequestMetrics {
numActiveRequests: number;
/**
* number of requests active at the start of the request
*/
numActiveRequestsAtStart: number;
/**
* number of requests active at the finish of the request (including the request itself)
*/
numActiveRequestsAtFinish: number;
/**
* The generated grpc object type of the request
*/
requestType: string;
/**
* The grpc status code of the response
*/
status: number;
/**
* The time the request started (millis since epoch)
*/
startTime: number;
/**
* The time the body of the request was available to the grpc library (millis since epoch)
*/
requestBodyTime: number;
/**
* The time the request completed (millis since epoch)
*/
endTime: number;
/**
* The duration of the request (in millis)
*/
duration: number;
/**
* The size of the request body in bytes
*/
requestSize: number;
/**
* The size of the response body in bytes
*/
responseSize: number;
}

Expand All @@ -35,6 +67,7 @@ class ExperimentalMetricsCsvMiddlewareRequestHandler
{
private readonly logger: MomentoLogger;
private readonly csvPath: string;
private readonly numActiveRequestsAtStart: number;
private readonly startTime: number;
private requestBodyTime: number;
private requestType: string;
Expand All @@ -48,7 +81,8 @@ class ExperimentalMetricsCsvMiddlewareRequestHandler
constructor(logger: MomentoLogger, csvPath: string) {
this.logger = logger;
this.csvPath = csvPath;
ExperimentalMetricsCsvMiddleware.numActiveRequests++;
this.numActiveRequestsAtStart =
++ExperimentalMetricsCsvMiddleware.numActiveRequests;
this.startTime = new Date().getTime();

this.receivedResponseBody = false;
Expand Down Expand Up @@ -95,7 +129,9 @@ class ExperimentalMetricsCsvMiddlewareRequestHandler
private recordMetrics(): void {
const endTime = new Date().getTime();
const metrics: RequestMetrics = {
numActiveRequests: ExperimentalMetricsCsvMiddleware.numActiveRequests,
numActiveRequestsAtStart: this.numActiveRequestsAtStart,
numActiveRequestsAtFinish:
ExperimentalMetricsCsvMiddleware.numActiveRequests,
requestType: this.requestType,
status: this.responseStatusCode,
startTime: this.startTime,
Expand All @@ -107,7 +143,8 @@ class ExperimentalMetricsCsvMiddlewareRequestHandler
};

const csvRow = [
metrics.numActiveRequests,
metrics.numActiveRequestsAtStart,
metrics.numActiveRequestsAtFinish,
metrics.requestType,
metrics.status,
metrics.startTime,
Expand Down

0 comments on commit 822d48f

Please sign in to comment.