Skip to content

Commit

Permalink
fix: updated agent to create a stub api when running in a worker thre…
Browse files Browse the repository at this point in the history
…ad (#1800)
  • Loading branch information
bizob2828 authored Oct 9, 2023
1 parent 30f0b16 commit 636e8f0
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 31 deletions.
53 changes: 26 additions & 27 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,35 @@ const NAMES = require('./lib/metrics/names')
const isESMSupported = psemver.satisfies('>=16.2.0')

const pkgJSON = require('./package.json')
if (!isMainThread) {
if (isMainThread) {
logger.info(
'Using New Relic for Node.js. Agent version: %s; Node version: %s.',
pkgJSON.version,
process.version
)

if (require.cache.__NR_cache) {
logger.warn(
'Attempting to load a second copy of newrelic from %s, using cache instead',
__dirname
)
if (require.cache.__NR_cache.agent) {
require.cache.__NR_cache.agent.recordSupportability('Agent/DoubleLoad')
}
module.exports = require.cache.__NR_cache
} else {
initialize()
}
} else {
logger.warn('Using New Relic for Node.js in worker_threads is not supported. Not starting!')
return
initApi({ apiPath: './stub_api' })
}

logger.info(
'Using New Relic for Node.js. Agent version: %s; Node version: %s.',
pkgJSON.version,
process.version
)
function initApi({ agent, apiPath }) {
const API = require(apiPath)

if (require.cache.__NR_cache) {
logger.warn(
'Attempting to load a second copy of newrelic from %s, using cache instead',
__dirname
)
if (require.cache.__NR_cache.agent) {
require.cache.__NR_cache.agent.recordSupportability('Agent/DoubleLoad')
}
module.exports = require.cache.__NR_cache
} else {
initialize()
const api = new API(agent)
require.cache.__NR_cache = module.exports = api
}

function initialize() {
Expand Down Expand Up @@ -103,15 +110,7 @@ function initialize() {
/* eslint-enable no-console */
}

let API = null
if (agent) {
API = require('./api')
} else {
API = require('./stub_api')
}

const api = new API(agent)
require.cache.__NR_cache = module.exports = api
const api = agent ? initApi({ agent, apiPath: './api' }) : initApi({ apiPath: './stub_api' })

// If we loaded an agent, record a startup time for the agent.
// NOTE: Metrics are recorded in seconds, so divide the value by 1000.
Expand Down
11 changes: 7 additions & 4 deletions test/unit/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -308,9 +308,10 @@ test('index tests', (t) => {
processVersionStub.satisfies.onCall(0).returns(true)
processVersionStub.satisfies.onCall(1).returns(true)
processVersionStub.satisfies.onCall(2).returns(false)
loadIndex()
const api = loadIndex()
t.equal(loggerMock.info.callCount, 2, 'should log info logs')
t.equal(loggerMock.info.args[1][0], 'No configuration detected. Not starting.')
t.equal(api.constructor.name, 'Stub')
t.end()
})

Expand All @@ -319,9 +320,10 @@ test('index tests', (t) => {
processVersionStub.satisfies.onCall(0).returns(true)
processVersionStub.satisfies.onCall(1).returns(true)
processVersionStub.satisfies.onCall(2).returns(false)
loadIndex()
const api = loadIndex()
t.equal(loggerMock.info.callCount, 2, 'should log info logs')
t.equal(loggerMock.info.args[1][0], 'Module disabled in configuration. Not starting.')
t.equal(api.constructor.name, 'Stub')
t.end()
})

Expand Down Expand Up @@ -362,15 +364,16 @@ test('index tests', (t) => {
t.end()
})

t.test('should log warning if not in main thread', (t) => {
t.test('should log warning if not in main thread and make a stub api', (t) => {
workerThreadsStub.isMainThread = false
loadIndex()
const api = loadIndex()
t.equal(loggerMock.warn.callCount, 1)
t.equal(
loggerMock.warn.args[0][0],
'Using New Relic for Node.js in worker_threads is not supported. Not starting!'
)
t.equal(loggerMock.info.callCount, 0, 'should not attempt to initialize agent')
t.equal(api.constructor.name, 'Stub')
t.end()
})
})

0 comments on commit 636e8f0

Please sign in to comment.