Skip to content

Commit cbc2644

Browse files
authored
feat: add spanId to all logs, add tests for traceId and spanId check (#69)
1 parent 4f28ea3 commit cbc2644

File tree

4 files changed

+101
-14
lines changed

4 files changed

+101
-14
lines changed

package-lock.json

Lines changed: 12 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"@commitlint/cli": "^17.7.1",
2727
"@commitlint/config-conventional": "^17.7.0",
2828
"@gravity-ui/eslint-config": "^3.2.0",
29-
"@gravity-ui/nodekit": "^2.0.0",
29+
"@gravity-ui/nodekit": "^2.4.1",
3030
"@gravity-ui/prettier-config": "^1.1.0",
3131
"@gravity-ui/tsconfig": "^1.0.0",
3232
"@types/accept-language-parser": "^1.5.6",

src/base-middleware.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ export function setupBaseMiddleware(ctx: AppContext, expressApp: Express) {
4343
const traceId = req.ctx.getTraceId();
4444
if (traceId) {
4545
res.setHeader('x-trace-id', traceId);
46-
req.ctx.addLoggerExtra('traceId', traceId);
4746
}
4847

4948
req.ctx.addLoggerExtra('req', {
@@ -55,7 +54,6 @@ export function setupBaseMiddleware(ctx: AppContext, expressApp: Express) {
5554
const requestStartedExtra = ctx.config.appDevMode
5655
? {req: {url: ctx.utils.redactSensitiveQueryParams(req.url)}}
5756
: {
58-
traceId,
5957
req: {
6058
id: requestId,
6159
method: req.method,
@@ -74,7 +72,6 @@ export function setupBaseMiddleware(ctx: AppContext, expressApp: Express) {
7472
const responseExtra = ctx.config.appDevMode
7573
? {res: {responseTime, statusCode}}
7674
: {
77-
traceId,
7875
res: {
7976
responseTime,
8077
statusCode,

src/tests/logging.test.ts

Lines changed: 88 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,27 @@ import {DEFAULT_REQUEST_ID_HEADER} from '../constants';
33
import {NodeKit} from '@gravity-ui/nodekit';
44
import request from 'supertest';
55

6+
const genRandomId = (length = 16) => {
7+
return [...Array(length)].map(() => Math.floor(Math.random() * 16).toString(16)).join('');
8+
};
9+
610
const APP_NAME = 'app';
11+
const USER_AGENT_HEADER = 'user-agent';
12+
const UBER_TRACE_ID_KEY = 'uber-trace-id';
13+
const X_TRACE_ID_KEY = 'x-trace-id';
714

815
const setupApp = () => {
916
const logger = {
1017
write: jest.fn(),
1118
};
1219

13-
const nodekit = new NodeKit({config: {appLoggingDestination: logger, appName: 'app'}});
20+
const nodekit = new NodeKit({
21+
config: {
22+
appLoggingDestination: logger,
23+
appName: APP_NAME,
24+
appTracingEnabled: true,
25+
},
26+
});
1427
const routes = {
1528
'GET /get': {
1629
handler: (_: Request, res: Response) => {
@@ -69,7 +82,7 @@ describe('log system', () => {
6982
await agent
7083
.get('/get')
7184
.set(DEFAULT_REQUEST_ID_HEADER, requestId)
72-
.set('user-agent', userAgent);
85+
.set(USER_AGENT_HEADER, userAgent);
7386

7487
// last log with response
7588
let log = JSON.parse(logger.write.mock.calls?.pop() || '{}');
@@ -127,7 +140,7 @@ describe('log system', () => {
127140
await agent
128141
.post('/post')
129142
.set(DEFAULT_REQUEST_ID_HEADER, requestId)
130-
.set('user-agent', userAgent);
143+
.set(USER_AGENT_HEADER, userAgent);
131144

132145
// last log with response
133146
let log = JSON.parse(logger.write.mock.calls?.pop() || '{}');
@@ -271,4 +284,76 @@ describe('log system', () => {
271284
},
272285
});
273286
});
287+
288+
it('log should contain traceId and spanId', async () => {
289+
const {app, logger} = setupApp();
290+
291+
const agent = request.agent(app.express);
292+
293+
const requestId = Math.random().toString();
294+
const userAgent = 'user-agent-' + Math.random().toString();
295+
296+
const traceId = genRandomId(32);
297+
const spanId = genRandomId(16);
298+
const traceFlags = '01';
299+
const uberTraceId = `${traceId}:${spanId}:0:${traceFlags}`;
300+
301+
await agent
302+
.get('/get')
303+
.set(DEFAULT_REQUEST_ID_HEADER, requestId)
304+
.set(USER_AGENT_HEADER, userAgent)
305+
.set(UBER_TRACE_ID_KEY, uberTraceId);
306+
307+
// last log with response
308+
let log = JSON.parse(logger.write.mock.calls?.pop() || '{}');
309+
310+
// check response log
311+
expect(log).toMatchObject({
312+
msg: `[Express GET] Request completed [${requestId}]`,
313+
level: 30,
314+
name: APP_NAME,
315+
time: expect.any(Number),
316+
traceId,
317+
req: {
318+
id: requestId,
319+
method: 'GET',
320+
url: '/get',
321+
},
322+
res: {
323+
statusCode: '200',
324+
responseTime: expect.any(Number),
325+
headers: {
326+
[DEFAULT_REQUEST_ID_HEADER]: requestId,
327+
[X_TRACE_ID_KEY]: traceId,
328+
},
329+
},
330+
});
331+
332+
expect(log?.spanId).toBeTruthy();
333+
334+
// first log with request
335+
log = JSON.parse(logger.write.mock.calls?.pop() || '{}');
336+
337+
// check request log
338+
expect(log).toMatchObject({
339+
msg: `[Express GET] Request started [${requestId}]`,
340+
level: 30,
341+
name: APP_NAME,
342+
time: expect.any(Number),
343+
traceId,
344+
req: {
345+
id: requestId,
346+
method: 'GET',
347+
url: '/get',
348+
headers: {
349+
[DEFAULT_REQUEST_ID_HEADER]: requestId,
350+
[UBER_TRACE_ID_KEY]: uberTraceId,
351+
},
352+
remoteAddress: expect.any(String),
353+
remotePort: expect.any(Number),
354+
},
355+
});
356+
357+
expect(log?.spanId).toBeTruthy();
358+
});
274359
});

0 commit comments

Comments
 (0)