@@ -3,14 +3,27 @@ import {DEFAULT_REQUEST_ID_HEADER} from '../constants';
33import { NodeKit } from '@gravity-ui/nodekit' ;
44import request from 'supertest' ;
55
6+ const genRandomId = ( length = 16 ) => {
7+ return [ ...Array ( length ) ] . map ( ( ) => Math . floor ( Math . random ( ) * 16 ) . toString ( 16 ) ) . join ( '' ) ;
8+ } ;
9+
610const 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
815const 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