From 90777ec0f8fdb4aa034ef9ef0f9c1621afd03370 Mon Sep 17 00:00:00 2001 From: Abhimanyu Babbar Date: Sat, 16 Nov 2024 00:56:08 +0530 Subject: [PATCH 1/2] chore: added support for stats calculation middleware on the endpoint customTransform --- src/middlewares/stats.ts | 15 +++++++++++++++ src/routes/userTransform.ts | 7 +++++-- src/util/cluster.js | 6 +++++- 3 files changed, 25 insertions(+), 3 deletions(-) create mode 100644 src/middlewares/stats.ts diff --git a/src/middlewares/stats.ts b/src/middlewares/stats.ts new file mode 100644 index 0000000000..926e2cd35b --- /dev/null +++ b/src/middlewares/stats.ts @@ -0,0 +1,15 @@ +import { Context, Next } from 'koa'; + +export class StatsMiddleware { + private static instanceID: string = process.env.INSTANCE_ID || 'unknown'; + + private static workerID: string = process.env.WORKER_ID || 'unknown'; + + public static async executionStats(ctx: Context, next: Next) { + const start = Date.now(); + await next(); + const ms = Date.now() - start; + ctx.set('X-Response-Time', `${ms}ms`); + ctx.set('X-Instance-ID', `${StatsMiddleware.instanceID}/${StatsMiddleware.workerID}`); + } +} diff --git a/src/routes/userTransform.ts b/src/routes/userTransform.ts index fc61ab7b94..aec63ace17 100644 --- a/src/routes/userTransform.ts +++ b/src/routes/userTransform.ts @@ -1,7 +1,9 @@ import Router from '@koa/router'; -import { RouteActivationMiddleware } from '../middlewares/routeActivation'; -import { FeatureFlagMiddleware } from '../middlewares/featureFlag'; +import { Context } from 'koa'; import { UserTransformController } from '../controllers/userTransform'; +import { FeatureFlagMiddleware } from '../middlewares/featureFlag'; +import { RouteActivationMiddleware } from '../middlewares/routeActivation'; +import { StatsMiddleware } from '../middlewares/stats'; const router = new Router(); @@ -15,6 +17,7 @@ router.post( '/customTransform', RouteActivationMiddleware.isUserTransformRouteActive, FeatureFlagMiddleware.handle, + StatsMiddleware.executionStats, UserTransformController.transform, ); router.post( diff --git a/src/util/cluster.js b/src/util/cluster.js index b9b86cd3c6..cf5aa7aa27 100644 --- a/src/util/cluster.js +++ b/src/util/cluster.js @@ -28,6 +28,8 @@ async function shutdownWorkers() { } function start(port, app, metricsApp) { + console.log(numWorkers); + if (cluster.isMaster) { logger.info(`Master (pid: ${process.pid}) has started`); @@ -44,7 +46,9 @@ function start(port, app, metricsApp) { // Fork workers. for (let i = 0; i < numWorkers; i += 1) { - cluster.fork(); + cluster.fork({ + WORKER_ID: `worker-${i + 1}`, + }); } cluster.on('online', (worker) => { From d84365cecd9a8b4d0df87c060ebe0e7891dd2c82 Mon Sep 17 00:00:00 2001 From: Abhimanyu Babbar Date: Sat, 16 Nov 2024 00:57:39 +0530 Subject: [PATCH 2/2] chore: updated the default values --- src/middlewares/stats.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/middlewares/stats.ts b/src/middlewares/stats.ts index 926e2cd35b..6fe0cc1c2c 100644 --- a/src/middlewares/stats.ts +++ b/src/middlewares/stats.ts @@ -1,9 +1,9 @@ import { Context, Next } from 'koa'; export class StatsMiddleware { - private static instanceID: string = process.env.INSTANCE_ID || 'unknown'; + private static instanceID: string = process.env.INSTANCE_ID || 'default'; - private static workerID: string = process.env.WORKER_ID || 'unknown'; + private static workerID: string = process.env.WORKER_ID || 'master'; public static async executionStats(ctx: Context, next: Next) { const start = Date.now();