Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: updated agent to create a stub api when running in a worker thread #1800

Merged
merged 1 commit into from
Oct 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
})
})
Loading