Skip to content

Commit

Permalink
feat: add spanId getter for app context (#57)
Browse files Browse the repository at this point in the history
* feat: add spanId getter for app context
  • Loading branch information
goshander authored Oct 25, 2024
1 parent e080977 commit f0b4e02
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 18 deletions.
7 changes: 5 additions & 2 deletions src/lib/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
38 changes: 22 additions & 16 deletions src/tests/logging.test.ts
Original file line number Diff line number Diff line change
@@ -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({
Expand All @@ -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({
Expand All @@ -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({
Expand All @@ -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({
Expand All @@ -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');
Expand Down
40 changes: 40 additions & 0 deletions src/tests/tracing.test.ts
Original file line number Diff line number Diff line change
@@ -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: {
getTraceId: () => traceId,
getSpanId: () => 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.getTraceId());
expect(ctx.getSpanId()).toBe(tracing.getSpanId());
});

0 comments on commit f0b4e02

Please sign in to comment.