Skip to content

Commit d497a44

Browse files
committed
Make api wraplogger also be controlled by logapi flag
1 parent 7e8d2b3 commit d497a44

File tree

6 files changed

+24
-10
lines changed

6 files changed

+24
-10
lines changed

src/common/wrap/wrap-logger.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,11 @@ const contextMap = new Map()
2020
* @param {Object} sharedEE - The shared event emitter on which a new scoped event emitter will be based.
2121
* @param {Object} parent - The parent object housing the logger function
2222
* @param {string} loggerFn - The name of the function in the parent object to wrap
23+
* @param {boolean} [autoCaptured=true] - True if log was captured from auto wrapping. False if it was captured from the API manual usage.
2324
* @returns {Object} Scoped event emitter with a debug ID of `logger`.
2425
*/
2526
// eslint-disable-next-line
26-
export function wrapLogger(sharedEE, parent, loggerFn, context) {
27+
export function wrapLogger(sharedEE, parent, loggerFn, context, autoCaptured = true) {
2728
if (!(typeof parent === 'object' && !!parent && typeof loggerFn === 'string' && !!loggerFn && typeof parent[loggerFn] === 'function')) return warn(29)
2829
const ee = scopedEE(sharedEE)
2930
const wrapFn = wfn(ee)
@@ -35,6 +36,7 @@ export function wrapLogger(sharedEE, parent, loggerFn, context) {
3536
const ctx = new EventContext(contextId)
3637
ctx.level = context.level
3738
ctx.customAttributes = context.customAttributes
39+
ctx.autoCaptured = autoCaptured
3840

3941
const contextLookupKey = parent[loggerFn]?.[flag] || parent[loggerFn]
4042
contextMap.set(contextLookupKey, ctx)

src/features/logging/instrument/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ export class Instrument extends InstrumentBase {
3232

3333
/** emitted by wrap-logger function */
3434
this.ee.on('wrap-logger-end', function handleLog ([message]) {
35-
const { level, customAttributes } = this
36-
bufferLog(instanceEE, message, customAttributes, level)
35+
const { level, customAttributes, autoCaptured } = this
36+
bufferLog(instanceEE, message, customAttributes, level, autoCaptured)
3737
})
3838
this.importAggregator(agentRef, () => import(/* webpackChunkName: "logging-aggregate" */ '../aggregate'))
3939
}

src/loaders/api/wrapLogger.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ import { setupAPI } from './sharedHandlers'
99

1010
export function setupWrapLoggerAPI (agent) {
1111
setupAPI(WRAP_LOGGER, (parent, functionName, { customAttributes = {}, level = LOG_LEVELS.INFO } = {}) => {
12-
wrapLogger(agent.ee, parent, functionName, { customAttributes, level })
12+
wrapLogger(agent.ee, parent, functionName, { customAttributes, level }, false)
1313
}, agent)
1414
}

tests/components/api.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -812,7 +812,7 @@ describe('API tests', () => {
812812
})])
813813

814814
expectHandled(SUPPORTABILITY_METRIC_CHANNEL, ['API/logging/info/called'])
815-
expectHandled('log', [expect.any(Number), 'test1', {}, 'INFO', true, undefined])
815+
expectHandled('log', [expect.any(Number), 'test1', {}, 'INFO', false, undefined])
816816

817817
const callCount = agent.ee.emit.mock.calls.length
818818
/** does NOT emit data for observed fn */
@@ -845,7 +845,7 @@ describe('API tests', () => {
845845
})])
846846

847847
expectHandled(SUPPORTABILITY_METRIC_CHANNEL, ['API/logging/warn/called'])
848-
expectHandled('log', [expect.any(Number), 'test1', {}, 'warn', true, undefined])
848+
expectHandled('log', [expect.any(Number), 'test1', {}, 'warn', false, undefined])
849849
})
850850

851851
test('should emit events with concat string for multiple args', () => {

tests/components/logging/instrument.test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ test('should subscribe to wrap-logger events and buffer them', async () => {
3232

3333
const message = faker.string.uuid()
3434
myLoggerSuite.myTestLogger(message)
35-
expect(loggingUtilsModule.bufferLog).toHaveBeenCalledWith(loggingInstrument.ee, message, customAttributes, 'error')
35+
expect(loggingUtilsModule.bufferLog).toHaveBeenCalledWith(loggingInstrument.ee, message, customAttributes, 'error', true)
3636
})
3737

3838
test('wrapLogger should not re-wrap or overwrite context if called more than once', async () => {
@@ -44,12 +44,12 @@ test('wrapLogger should not re-wrap or overwrite context if called more than onc
4444

4545
let message = faker.string.uuid()
4646
myLoggerSuite.myTestLogger(message)
47-
expect(loggingUtilsModule.bufferLog).toHaveBeenCalledWith(loggingInstrument.ee, message, customAttributes, 'error') // ignores args: 2
47+
expect(loggingUtilsModule.bufferLog).toHaveBeenCalledWith(loggingInstrument.ee, message, customAttributes, 'error', true) // ignores args: 2
4848

4949
const newCustomAttributes = { args: faker.string.uuid() }
5050
/** re-wrap the logger with a NEW context, new events should NOT get that context because the wrapper should early-exit */
5151
wrapLogger(loggingInstrument.ee, myLoggerSuite, 'myTestLogger', { customAttributes: newCustomAttributes, level: 'info' })
5252
message = faker.string.uuid()
5353
myLoggerSuite.myTestLogger(message)
54-
expect(loggingUtilsModule.bufferLog).toHaveBeenCalledWith(loggingInstrument.ee, message, newCustomAttributes, 'info')
54+
expect(loggingUtilsModule.bufferLog).toHaveBeenCalledWith(loggingInstrument.ee, message, newCustomAttributes, 'info', true)
5555
})

tests/specs/logging/modes.e2e.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ describe('logging mode from RUM flags', () => {
2727
console.log("It's a-me, Mario")
2828
console.debug('Mamma mia')
2929
newrelic.log('Here we go')
30+
const printer = {
31+
log: function (message) {} // other args are ignored
32+
}
33+
newrelic.wrapLogger(printer, 'log', { level: 'info' })
34+
printer.log('Wahoo!')
3035
})
3136
])
3237

@@ -35,6 +40,7 @@ describe('logging mode from RUM flags', () => {
3540
expect(logPayload.logs[0].message).toEqual("It's a-me, Mario")
3641
// The console.debug should not be captured because it's above verbosity level.
3742
// The newrelic.log should not be captured because logapi flag is set to OFF.
43+
// The printer.log should not be captured because logapi flag is set to OFF.
3844
})
3945

4046
it('only captures api logs when log flag is 0', async () => {
@@ -47,13 +53,19 @@ describe('logging mode from RUM flags', () => {
4753
console.log("It's a-me, Mario")
4854
newrelic.log('Mamma mia')
4955
newrelic.log('Here we go', { level: 'trace' })
56+
const printer = {
57+
log: function (message) {} // other args are ignored
58+
}
59+
newrelic.wrapLogger(printer, 'log', { level: 'info' })
60+
printer.log('Wahoo!')
5061
})
5162
])
5263

5364
const logPayload = JSON.parse(logsHarvests[0].request.body)[0]
54-
expect(logPayload.logs.length).toEqual(1)
65+
expect(logPayload.logs.length).toEqual(2)
5566
// The console.log should not be captured because log flag is set to OFF.
5667
expect(logPayload.logs[0].message).toEqual('Mamma mia')
5768
// The second newrelic.log should not be captured because the level is above INFO flag.
69+
expect(logPayload.logs[1].message).toEqual('Wahoo!')
5870
})
5971
})

0 commit comments

Comments
 (0)