From 950973eb18085cc4b7f44d3b81ad20fb3a8ec6e2 Mon Sep 17 00:00:00 2001 From: Georgy Date: Tue, 15 Oct 2024 14:29:43 +0300 Subject: [PATCH 1/3] feat: add spanId getter for app context --- src/lib/context.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/lib/context.ts b/src/lib/context.ts index c75901a..b022701 100644 --- a/src/lib/context.ts +++ b/src/lib/context.ts @@ -233,8 +233,11 @@ export class AppContext { } getTraceId() { - // @ts-ignore - return this.span?._spanContext?.toTraceId(); + return this.span?.context()?.toTraceId(); + } + + getSpanId() { + return this.span?.context()?.toSpanId(); } // allow add extra logger data, after ctx already initialized (ex. to add traceId from ctx) From b3e025f0744469a9e3302134689f66f28347f930 Mon Sep 17 00:00:00 2001 From: Georgy Date: Wed, 16 Oct 2024 16:17:43 +0300 Subject: [PATCH 2/3] fix: add tracing tests --- src/tests/logging.test.ts | 38 +++++++++++++++++++++---------------- src/tests/tracing.test.ts | 40 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 16 deletions(-) create mode 100644 src/tests/tracing.test.ts diff --git a/src/tests/logging.test.ts b/src/tests/logging.test.ts index cd40687..8ed1038 100644 --- a/src/tests/logging.test.ts +++ b/src/tests/logging.test.ts @@ -1,14 +1,20 @@ import {NodeKit} from '..'; -const logger = { - write: jest.fn(), +const setupNodeKit = () => { + const logger = { + write: jest.fn(), + }; + + const nodekit = new NodeKit({config: {appLoggingDestination: logger}}); + + return {nodekit, logger}; }; test('check base logging system', () => { - const nodeKit = new NodeKit({config: {appLoggingDestination: logger}}); + const {nodekit, logger} = setupNodeKit(); // log function - nodeKit.ctx.log('log info'); + nodekit.ctx.log('log info'); let log = JSON.parse(logger.write.mock.lastCall?.pop() || '{}'); expect(log).toMatchObject({ @@ -17,7 +23,7 @@ test('check base logging system', () => { }); // logError function - nodeKit.ctx.logError('log error'); + nodekit.ctx.logError('log error'); log = JSON.parse(logger.write.mock.lastCall?.pop() || '{}'); expect(log).toMatchObject({ @@ -28,7 +34,7 @@ test('check base logging system', () => { // logError function with error object const err = new Error('error object'); - nodeKit.ctx.logError('log error with error object', err); + nodekit.ctx.logError('log error with error object', err); log = JSON.parse(logger.write.mock.lastCall?.pop() || '{}'); expect(log).toMatchObject({ @@ -43,34 +49,34 @@ test('check base logging system', () => { }); test('check logging with extra data', () => { - const nodeKit = new NodeKit({config: {appLoggingDestination: logger}}); + const {nodekit, logger} = setupNodeKit(); const extra = Math.random().toString(); // log function with extra param - nodeKit.ctx.log('log info', {extra}); + nodekit.ctx.log('log info', {extra}); let log = JSON.parse(logger.write.mock.lastCall?.pop() || '{}'); expect(log).toMatchObject({extra}); // add extra data to ctx const traceId = Math.random().toString(); - nodeKit.ctx.addLoggerExtra('traceId', traceId); + nodekit.ctx.addLoggerExtra('traceId', traceId); // log function with extra ctx data - nodeKit.ctx.log('log info'); + nodekit.ctx.log('log info'); log = JSON.parse(logger.write.mock.lastCall?.pop() || '{}'); expect(log).toMatchObject({traceId}); // log function with extra param and extra ctx data - nodeKit.ctx.log('log info', {extra}); + nodekit.ctx.log('log info', {extra}); log = JSON.parse(logger.write.mock.lastCall?.pop() || '{}'); expect(log).toMatchObject({traceId, extra}); // logError function with extra param and extra ctx data - nodeKit.ctx.logError('log error', new Error('err'), {extra}); + nodekit.ctx.logError('log error', new Error('err'), {extra}); log = JSON.parse(logger.write.mock.lastCall?.pop() || '{}'); expect(log).toMatchObject({ @@ -81,20 +87,20 @@ test('check logging with extra data', () => { }); test('check logging from nested ctx', () => { - const nodeKit = new NodeKit({config: {appLoggingDestination: logger}}); + const {nodekit, logger} = setupNodeKit(); const traceId = Math.random().toString(); - nodeKit.ctx.addLoggerExtra('traceId', traceId); + nodekit.ctx.addLoggerExtra('traceId', traceId); // log function from parent ctx - nodeKit.ctx.log('log info'); + nodekit.ctx.log('log info'); let log = JSON.parse(logger.write.mock.lastCall?.pop() || '{}'); expect(log).toMatchObject({traceId}); const ctxName = Math.random().toString(); const logPostfix = Math.random().toString(); - const newCtx = nodeKit.ctx.create(ctxName, {loggerPostfix: logPostfix}); + const newCtx = nodekit.ctx.create(ctxName, {loggerPostfix: logPostfix}); // log function from nested ctx newCtx.log('log info'); diff --git a/src/tests/tracing.test.ts b/src/tests/tracing.test.ts new file mode 100644 index 0000000..1efba34 --- /dev/null +++ b/src/tests/tracing.test.ts @@ -0,0 +1,40 @@ +import {SpanContext} from 'opentracing'; + +import {NodeKit} from '..'; + +const setupNodeKit = () => { + const nodekit = new NodeKit({ + config: { + appTracingEnabled: false, + appTracingServiceName: 'app', + }, + }); + + const traceId = Math.random().toString(); + const spanId = Math.random().toString(); + + jest.spyOn(SpanContext.prototype, 'toTraceId').mockImplementation(() => { + return traceId; + }); + + jest.spyOn(SpanContext.prototype, 'toSpanId').mockImplementation(() => { + return spanId; + }); + + return { + nodekit, + tracing: { + traceId, + spanId, + }, + }; +}; + +test('check traceId and spanId exist at child ctx', () => { + const {nodekit, tracing} = setupNodeKit(); + + const ctx = nodekit.ctx.create('app'); + + expect(ctx.getTraceId()).toBe(tracing.traceId); + expect(ctx.getSpanId()).toBe(tracing.spanId); +}); From 0cdc87c69b5ddb5a47d81c8dc9e66d2b741ae2df Mon Sep 17 00:00:00 2001 From: Georgy Date: Wed, 16 Oct 2024 16:27:44 +0300 Subject: [PATCH 3/3] fix: test with tracing mock --- src/tests/tracing.test.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/tests/tracing.test.ts b/src/tests/tracing.test.ts index 1efba34..c495d0b 100644 --- a/src/tests/tracing.test.ts +++ b/src/tests/tracing.test.ts @@ -24,8 +24,8 @@ const setupNodeKit = () => { return { nodekit, tracing: { - traceId, - spanId, + getTraceId: () => traceId, + getSpanId: () => spanId, }, }; }; @@ -35,6 +35,6 @@ test('check traceId and spanId exist at child ctx', () => { const ctx = nodekit.ctx.create('app'); - expect(ctx.getTraceId()).toBe(tracing.traceId); - expect(ctx.getSpanId()).toBe(tracing.spanId); + expect(ctx.getTraceId()).toBe(tracing.getTraceId()); + expect(ctx.getSpanId()).toBe(tracing.getSpanId()); });