Skip to content

Commit

Permalink
test: Fixed shadowed variable lint error in pino versioned tests (#1859)
Browse files Browse the repository at this point in the history
  • Loading branch information
jsumners-nr committed Nov 10, 2023
1 parent 08b5a78 commit e1b21eb
Showing 1 changed file with 37 additions and 21 deletions.
58 changes: 37 additions & 21 deletions test/versioned/pino/pino.tap.js
Expand Up @@ -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)
Expand All @@ -82,21 +89,23 @@ 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 },
forwarding: { enabled: false },
metrics: { enabled: false }
}
})
const { agent, logger, stream } = t.context
const message = 'pino decorating test'
helper.runInTransaction(agent, 'pino-test', async () => {
logger.info(message)
Expand All @@ -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 },
Expand All @@ -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)
Expand All @@ -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' }
Expand All @@ -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)
Expand All @@ -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'
Expand Down Expand Up @@ -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'
Expand All @@ -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
Expand Down Expand Up @@ -294,14 +308,15 @@ 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 },
forwarding: { enabled: false },
metrics: { enabled: true }
}
})
const { agent, pino, stream } = t.context

const pinoLogger = pino(
{
Expand Down Expand Up @@ -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')
Expand Down

0 comments on commit e1b21eb

Please sign in to comment.