Skip to content

Commit a68bc6f

Browse files
authored
feat: add option to exclude actions from self telemetry (#80)
1 parent c5fdd54 commit a68bc6f

File tree

3 files changed

+107
-10
lines changed

3 files changed

+107
-10
lines changed

src/router.ts

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -102,16 +102,20 @@ export function setupRoutes(ctx: AppContext, expressApp: Express, routes: AppRou
102102

103103
res.on('finish', () => {
104104
if (req.ctx.config.appTelemetryChEnableSelfStats) {
105-
req.ctx.stats({
106-
service: 'self',
107-
action: req.routeInfo.handlerName || UNNAMED_CONTROLLER,
108-
responseStatus: res.statusCode,
109-
requestId: req.ctx.get(REQUEST_ID_PARAM_NAME) || '',
110-
requestTime: req.originalContext.getTime(), //We have to use req.originalContext here to get full time
111-
requestMethod: req.method,
112-
requestUrl: req.originalUrl,
113-
traceId: req.ctx.getTraceId() || '',
114-
});
105+
const disableSelfStats = Boolean(req.routeInfo.disableSelfStats);
106+
107+
if (!disableSelfStats) {
108+
req.ctx.stats({
109+
service: 'self',
110+
action: req.routeInfo.handlerName || UNNAMED_CONTROLLER,
111+
responseStatus: res.statusCode,
112+
requestId: req.ctx.get(REQUEST_ID_PARAM_NAME) || '',
113+
requestTime: req.originalContext.getTime(), //We have to use req.originalContext here to get full time
114+
requestMethod: req.method,
115+
requestUrl: req.originalUrl,
116+
traceId: req.ctx.getTraceId() || '',
117+
});
118+
}
115119
}
116120
});
117121

src/tests/self-stats.test.ts

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import {AppRoutes, ExpressKit, Request, Response} from '..';
2+
import {DEFAULT_REQUEST_ID_HEADER} from '../constants';
3+
import {AppConfig, NodeKit} from '@gravity-ui/nodekit';
4+
import request from 'supertest';
5+
6+
const APP_NAME = 'app';
7+
8+
const setupApp = ({config}: {config?: AppConfig} = {}) => {
9+
const nodekit = new NodeKit({
10+
config: {
11+
appName: APP_NAME,
12+
appTelemetryChEnableSelfStats: true,
13+
...config,
14+
},
15+
});
16+
const stats = jest.fn();
17+
nodekit.ctx.stats = stats;
18+
19+
const routes: AppRoutes = {
20+
'GET /ping-self-stats': {
21+
handler: (_: Request, res: Response) => {
22+
res.status(200);
23+
res.send({status: 'ok'});
24+
},
25+
},
26+
'GET /ping-no-self-stats': {
27+
handler: (_: Request, res: Response) => {
28+
res.status(200);
29+
res.send({status: 'ok'});
30+
},
31+
disableSelfStats: true,
32+
},
33+
};
34+
35+
const app = new ExpressKit(nodekit, routes);
36+
37+
return {app, stats};
38+
};
39+
40+
describe('self stats telemetry', () => {
41+
const env = process.env;
42+
43+
beforeEach(() => {
44+
jest.resetModules();
45+
process.env = {...env, APP_NAME};
46+
});
47+
48+
afterEach(() => {
49+
process.env = env;
50+
});
51+
52+
it('self stats telemetry sended', async () => {
53+
const {app, stats} = setupApp();
54+
55+
const agent = request.agent(app.express);
56+
const requestId = Math.random().toString();
57+
58+
await agent.get('/ping-self-stats').set(DEFAULT_REQUEST_ID_HEADER, requestId).expect(200);
59+
60+
// last self stats data
61+
const stat = stats.mock.calls?.pop() || {};
62+
63+
expect(stat).toMatchObject([
64+
{
65+
service: 'self',
66+
action: 'handler',
67+
responseStatus: 200,
68+
requestId,
69+
requestMethod: 'GET',
70+
requestUrl: '/ping-self-stats',
71+
traceId: '',
72+
},
73+
]);
74+
});
75+
76+
it('self stats telemetry skipped', async () => {
77+
const {app, stats} = setupApp();
78+
79+
const agent = request.agent(app.express);
80+
const requestId = Math.random().toString();
81+
82+
await agent
83+
.get('/ping-no-self-stats')
84+
.set(DEFAULT_REQUEST_ID_HEADER, requestId)
85+
.expect(200);
86+
87+
// last self stats data
88+
const stat = stats.mock.calls?.pop() || null;
89+
90+
expect(stat).toBeNull();
91+
});
92+
});

src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ export enum AuthPolicy {
9595
export interface AppRouteParams {
9696
authPolicy?: `${AuthPolicy}`;
9797
handlerName?: string;
98+
disableSelfStats?: boolean;
9899
}
99100

100101
export interface AppRouteDescription extends AppRouteParams {

0 commit comments

Comments
 (0)