diff --git a/test/versioned/pino/pino.tap.js b/test/versioned/pino/pino.tap.js index 5475381273..5a45717edd 100644 --- a/test/versioned/pino/pino.tap.js +++ b/test/versioned/pino/pino.tap.js @@ -41,31 +41,38 @@ tap.Test.prototype.addAssert( tap.test('Pino instrumentation', (t) => { t.autoend() - let logger - let stream - let pino - let agent - function setupAgent(config) { - agent = helper.instrumentMockedAgent(config) - agent.config.entity_guid = 'pino-guid' - pino = require('pino') - stream = sink() - logger = pino({ level: 'debug' }, stream) - return agent.config + function setupAgent(context, config) { + context.agent = helper.instrumentMockedAgent(config) + context.agent.config.entity_guid = 'pino-guid' + context.pino = require('pino') + context.stream = sink() + context.logger = context.pino({ level: 'debug' }, context.stream) + return context.agent.config } - t.afterEach(() => { - helper.unloadAgent(agent) + t.beforeEach(async (t) => { Object.keys(require.cache).forEach((key) => { if (/pino/.test(key)) { delete require.cache[key] } }) + + t.context.pino = null + t.context.agent = null + t.context.stream = null + t.context.logger = null + }) + + t.afterEach((t) => { + if (t.context.agent) { + helper.unloadAgent(t.context.agent) + } }) t.test('logging disabled', async (t) => { - setupAgent({ application_logging: { enabled: false } }) + setupAgent(t.context, { application_logging: { enabled: false } }) + const { agent, pino, stream } = t.context const disabledLogger = pino({ level: 'info' }, stream) const message = 'logs are not enriched' disabledLogger.info(message) @@ -82,14 +89,15 @@ tap.test('Pino instrumentation', (t) => { }) t.test('logging enabled', (t) => { - setupAgent({ application_logging: { enabled: true } }) + setupAgent(t.context, { application_logging: { enabled: true } }) + const { agent } = t.context const metric = agent.metrics.getMetric(LOGGING.LIBS.PINO) t.equal(metric.callCount, 1, `should create ${LOGGING.LIBS.PINO} metric`) t.end() }) t.test('local_decorating', (t) => { - setupAgent({ + setupAgent(t.context, { application_logging: { enabled: true, local_decorating: { enabled: true }, @@ -97,6 +105,7 @@ tap.test('Pino instrumentation', (t) => { metrics: { enabled: false } } }) + const { agent, logger, stream } = t.context const message = 'pino decorating test' helper.runInTransaction(agent, 'pino-test', async () => { logger.info(message) @@ -113,10 +122,9 @@ tap.test('Pino instrumentation', (t) => { }) t.test('forwarding', (t) => { - let config t.autoend() - t.beforeEach(() => { - config = setupAgent({ + t.beforeEach((t) => { + t.context.config = setupAgent(t.context, { application_logging: { enabled: true, local_decorating: { enabled: false }, @@ -127,6 +135,7 @@ tap.test('Pino instrumentation', (t) => { }) t.test('should have proper metadata outside of a transaction', async (t) => { + const { agent, config, logger, stream } = t.context const message = 'pino unit test' const level = 'info' logger[level](message) @@ -143,6 +152,7 @@ tap.test('Pino instrumentation', (t) => { }) t.test('should not crash nor enqueue log line when invalid json', async (t) => { + const { agent, config, pino } = t.context // When you log an object that will be the first arg to the logger level // the 2nd arg is the message const message = { 'pino "unit test': 'prop' } @@ -166,6 +176,7 @@ tap.test('Pino instrumentation', (t) => { }) t.test('should have proper error keys when error is present', async (t) => { + const { agent, config, logger, stream } = t.context const err = new Error('This is a test') const level = 'error' logger[level](err) @@ -192,6 +203,7 @@ tap.test('Pino instrumentation', (t) => { }) t.test('should add proper trace info in transaction', (t) => { + const { agent, config, logger, stream } = t.context helper.runInTransaction(agent, 'pino-test', async (tx) => { const level = 'info' const message = 'My debug test' @@ -226,6 +238,7 @@ tap.test('Pino instrumentation', (t) => { t.test( 'should assign hostname from NR linking metadata when not defined as a core chinding', async (t) => { + const { agent, config, pino } = t.context const localStream = sink() const localLogger = pino({ base: undefined }, localStream) const message = 'pino unit test' @@ -242,6 +255,7 @@ tap.test('Pino instrumentation', (t) => { ) t.test('should properly handle child loggers', (t) => { + const { agent, config, logger, stream } = t.context const childLogger = logger.child({ module: 'child' }) helper.runInTransaction(agent, 'pino-test', async (tx) => { // these are defined in opposite order because the log aggregator is LIFO @@ -294,7 +308,7 @@ tap.test('Pino instrumentation', (t) => { t.autoend() t.test('should count logger metrics', (t) => { - setupAgent({ + setupAgent(t.context, { application_logging: { enabled: true, local_decorating: { enabled: false }, @@ -302,6 +316,7 @@ tap.test('Pino instrumentation', (t) => { metrics: { enabled: true } } }) + const { agent, pino, stream } = t.context const pinoLogger = pino( { @@ -371,7 +386,8 @@ tap.test('Pino instrumentation', (t) => { ] configValues.forEach(({ name, config }) => { t.test(`should not count logger metrics when ${name}`, (t) => { - setupAgent(config) + setupAgent(t.context, config) + const { agent, logger, stream } = t.context helper.runInTransaction(agent, 'pino-test', async () => { logger.info('This is a log message test') await once(stream, 'data')