generated from MapColonies/ts-npm-package-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(tracing): added hook to integrate with pino logger (#17)
* feat(tracing): added hook to integrate with pino logger * refactor(tracing): simplified building the args for the logfn * test(tracing): removed code duplication from tests
- Loading branch information
1 parent
b1226f9
commit 688d67f
Showing
8 changed files
with
152 additions
and
16 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export { Tracing } from './tracing'; | ||
export { logMethod } from './loggerHook'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { getSpan, context } from '@opentelemetry/api'; | ||
import { LogFn, Logger } from 'pino'; | ||
|
||
// ignored because its the same type needed for LogFn | ||
// eslint-disable-next-line @typescript-eslint/ban-types, @typescript-eslint/no-explicit-any | ||
export type LogFnArgs = [obj: object, msg?: string, ...args: any[]] | [msg: string, ...args: any[]]; | ||
|
||
// disabled because this function signature is required by pino | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
export function logMethod(this: Logger, args: LogFnArgs, method: LogFn, level: number): void { | ||
const span = getSpan(context.active()); | ||
|
||
if (!span) { | ||
method.apply<Logger, LogFnArgs, void>(this, args); | ||
return; | ||
} | ||
const traceObj = { spanId: span.context().spanId, traceId: span.context().traceId }; | ||
|
||
let logFnArgs: LogFnArgs; | ||
const [firstArg, ...rest] = args; | ||
if (typeof firstArg === 'object') { | ||
logFnArgs = [{ ...firstArg, ...traceObj }, ...rest]; | ||
} else { | ||
logFnArgs = [traceObj, firstArg, ...rest]; | ||
} | ||
|
||
method.apply<Logger, LogFnArgs, void>(this, logFnArgs); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { trace, context, setSpan, Tracer } from '@opentelemetry/api'; | ||
import { NodeTracerProvider } from '@opentelemetry/node'; | ||
import * as faker from 'faker'; | ||
import { AsyncHooksContextManager } from '@opentelemetry/context-async-hooks'; | ||
import { LogFn, Logger } from 'pino'; | ||
import { logMethod } from '../../src'; | ||
import { LogFnArgs } from '../../src/tracing/loggerHook'; | ||
|
||
describe('#logMethod', () => { | ||
let provider: NodeTracerProvider; | ||
let tracer: Tracer; | ||
|
||
beforeAll(() => { | ||
provider = new NodeTracerProvider(); | ||
provider.register({ contextManager: new AsyncHooksContextManager() }); | ||
tracer = trace.getTracer('test_tracer'); | ||
}); | ||
|
||
afterAll(async () => { | ||
await provider.shutdown(); | ||
}); | ||
|
||
it('should call the method with the same args if tracer is not enabled', () => { | ||
const args = [faker.random.word()] as [string]; | ||
const logFn = jest.fn(); | ||
|
||
logMethod.apply(({} as unknown) as Logger, [args, logFn, 5]); | ||
|
||
expect(logFn).toHaveBeenLastCalledWith(...args); | ||
}); | ||
|
||
it('should add traceId and spanId when logging only msg', () => { | ||
const args = [faker.random.word()] as [string]; | ||
const logFn = jest.fn(); | ||
|
||
const span = tracer.startSpan('test_span'); | ||
const ctx = setSpan(context.active(), span); | ||
|
||
context.with<[LogFnArgs, LogFn, number], typeof logMethod>(ctx, logMethod, ({} as unknown) as Logger, args, logFn, 5); | ||
|
||
span.end(); | ||
|
||
const { spanId, traceId } = span.context(); | ||
expect(logFn).toHaveBeenLastCalledWith({ spanId, traceId }, ...args); | ||
}); | ||
|
||
it('should add traceId and spanId to the logging only obj', () => { | ||
// pino logfn uses the type object | ||
// eslint-disable-next-line @typescript-eslint/ban-types | ||
const args = [{ test: faker.random.word() }, faker.random.word()] as [object, string]; | ||
const logFn = jest.fn(); | ||
|
||
const span = tracer.startSpan('test_span'); | ||
const ctx = setSpan(context.active(), span); | ||
|
||
context.with<[LogFnArgs, LogFn, number], typeof logMethod>(ctx, logMethod, ({} as unknown) as Logger, args, logFn, 5); | ||
|
||
span.end(); | ||
|
||
const { spanId, traceId } = span.context(); | ||
const [obj, ...rest] = args; | ||
expect(logFn).toHaveBeenLastCalledWith({ spanId, traceId, ...obj }, ...rest); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters