diff --git a/src/middlewares/stats.ts b/src/middlewares/stats.ts new file mode 100644 index 0000000000..6fe0cc1c2c --- /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 || 'default'; + + private static workerID: string = process.env.WORKER_ID || 'master'; + + 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) => {