Skip to content

Commit b1f4553

Browse files
feat: add performance payload
1 parent 29aa4eb commit b1f4553

File tree

4 files changed

+35
-0
lines changed

4 files changed

+35
-0
lines changed

__tests__/services/abstract-microservice-test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import dns from 'dns';
33
import axios from 'axios';
44
import { expect } from 'chai';
5+
import _ from 'lodash';
56
import sinon from 'sinon';
67
import BaseException from '@core/base-exception';
78
import MicroserviceRequest from '@core/microservice-request';
@@ -275,6 +276,8 @@ describe('services/abstract-microservice', () => {
275276
const stubbed = await createAxiosMock(req.toJSON());
276277
const secondCall = stubbed.getCall(1).firstArg;
277278

279+
_.unset(secondCall.data.getResult(), 'payload');
280+
278281
expect(secondCall.data.getResult()).to.deep.equal({ ...result, middleware: 'after' });
279282
expect(handler.firstCall.firstArg).to.deep.equal({ ...req.getParams(), middleware: 'before' });
280283
});
@@ -361,6 +364,8 @@ describe('services/abstract-microservice', () => {
361364

362365
stubbed.restore();
363366

367+
_.unset(data, 'params.payload.performance');
368+
364369
expect(result).to.instanceof(MicroserviceResponse);
365370
expect(result.getResult()).to.deep.equal(response);
366371
// Set correctly microservice url

__tests__/services/gateway-test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,8 @@ describe('services/gateway', () => {
303303
const response = res.json.firstCall.firstArg;
304304
const { data, headers } = stubbed.firstCall.firstArg;
305305

306+
_.unset(response.result, 'payload');
307+
306308
expect(response.getResult()).to.deep.equal({ endpointTriggerMiddleware, middleware: 'after' });
307309
expect(data.method).to.equal(endpointTriggerMiddleware);
308310
expect(data.params.middleware).to.equal('before');

src/services/abstract-microservice.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { Agent } from 'http';
22
import http from 'http';
3+
import { performance } from 'perf_hooks';
34
import axios from 'axios';
45
import _ from 'lodash';
56
import { v4 as uuidv4 } from 'uuid';
@@ -207,6 +208,22 @@ abstract class AbstractMicroservice {
207208
return this;
208209
}
209210

211+
/**
212+
* Inject performance record to request or response
213+
* @protected
214+
*/
215+
protected injectPerformance(data: Record<string, any>, key: string, type: 'req' | 'res'): void {
216+
const stack = data?.payload?.performance ?? [];
217+
218+
stack.push({
219+
key,
220+
type,
221+
point: performance.now(),
222+
});
223+
224+
_.set(data, 'payload.performance', stack);
225+
}
226+
210227
/**
211228
* Add process exit handler
212229
* E.g. for close DB connection etc.
@@ -345,6 +362,8 @@ abstract class AbstractMicroservice {
345362

346363
const task = new MicroserviceRequest(req.data);
347364

365+
this.injectPerformance(task.getParams()!, `${name}-start`, 'req');
366+
348367
return { task, req };
349368
} catch (e) {
350369
// Could not connect to ijson or channel
@@ -369,6 +388,7 @@ abstract class AbstractMicroservice {
369388
task: ITask['task'],
370389
req: ITask['req'],
371390
): Promise<MicroserviceResponse> {
391+
const { name } = this.options;
372392
const response = new MicroserviceResponse({ id: task.getId() });
373393
const taskSender =
374394
task instanceof MicroserviceRequest ? task.getParams()?.payload?.sender : null;
@@ -437,6 +457,8 @@ abstract class AbstractMicroservice {
437457
response.getId(),
438458
);
439459

460+
this.injectPerformance(response.getResult()!, `${name}-end`, 'res');
461+
440462
return response;
441463
}
442464

@@ -631,6 +653,7 @@ abstract class AbstractMicroservice {
631653
_.set(requestParams, 'payload.sender', sender);
632654
_.set(requestParams, 'payload.senderStack', [...(data.payload?.senderStack ?? []), sender]);
633655
_.set(requestParams, 'payload.isInternal', isInternal);
656+
this.injectPerformance(requestParams, `${microservice}-start`, 'req');
634657

635658
const request = new MicroserviceRequest({
636659
...(shouldGenerateId || reqId ? { id: reqId ?? uuidv4() } : {}),
@@ -670,6 +693,8 @@ abstract class AbstractMicroservice {
670693
throw new BaseException(result.error);
671694
}
672695

696+
this.injectPerformance(result.result, `${microservice}-end`, 'res');
697+
673698
response.setResult(result.result);
674699

675700
return response;

src/services/gateway.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,7 @@ class Gateway extends AbstractMicroservice {
295295
_.set(body, 'params.payload.senderStack', ['client']);
296296
_.set(body, 'params.payload.isInternal', false);
297297
_.set(body, 'params.payload.headers', headers);
298+
this.injectPerformance(body.params!, 'gateway-start', 'req');
298299

299300
const request = new MicroserviceRequest(body);
300301
const [microservice] = request.getMethod().split('.');
@@ -350,6 +351,8 @@ class Gateway extends AbstractMicroservice {
350351
`${request.getId()!}-gateway`,
351352
);
352353

354+
this.injectPerformance(result!, 'gateway-end', 'req');
355+
353356
response.setResult(result);
354357

355358
return response;

0 commit comments

Comments
 (0)