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

Have default export as a function #238

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
184 changes: 93 additions & 91 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,111 +3,113 @@
var logger = require('./lib/logger.js')
var semver = require('semver')

var message
var agent

var agentVersion = require('./package.json').version
logger.info(
"Using New Relic for Node.js. Agent version: %s; Node version: %s.",
agentVersion, process.version
)

if (require.cache.__NR_cache) {
logger.warn(
'Attempting to load a second copy of newrelic from %s, using cache instead',
__dirname
)
module.exports = require.cache.__NR_cache
} else {
initialize()
}

function initialize() {
logger.debug(
'Loading agent from %s',
__dirname
module.exports = function init(_config) {
var message
var agent

var agentVersion = require('./package.json').version
logger.info(
"Using New Relic for Node.js. Agent version: %s; Node version: %s.",
agentVersion, process.version
)

try {
logger.debug("Process was running %s seconds before agent was loaded.",
process.uptime())
// Technically we run on 0.6, until we verify there are 0 users on 0.6, we
// should leave this code doing a check against 0.6, but then advise that
// people upgrade to one of our officially supported version (0.8 and higher)
if (semver.satisfies(process.version, '<0.6.0')) {
message = "New Relic for Node.js requires a version of Node equal to or\n" +
"greater than 0.8.0. Not starting!"

logger.error(message)
throw new Error(message)
}

logger.debug("Current working directory at module load is %s.", process.cwd())
logger.debug("Process title is %s.", process.title)
logger.debug("Application was invoked as %s.", process.argv.join(' '))
if (require.cache.__NR_cache) {
logger.warn(
'Attempting to load a second copy of newrelic from %s, using cache instead',
__dirname
)
module.exports = require.cache.__NR_cache
} else {
initialize(_config)
}

/* Loading the configuration can throw if a configuration file isn't found and
* the environment variable NEW_RELIC_NO_CONFIG_FILE isn't set.
*/
var config = require('./lib/config.js').initialize()
if (!config.agent_enabled) {
logger.info("Module not enabled in configuration; not starting.")
} else {
/* Only load the rest of the module if configuration is available and the
* configurator didn't throw.
*
* The agent must be a singleton, or else module loading will be patched
* multiple times, with undefined results. New Relic's instrumentation
* can't be enabled or disabled without an application restart.
*/
var Agent = require('./lib/agent.js')
agent = new Agent(config)
var appNames = agent.config.applications()

if (config.logging.diagnostics) {
logger.warn(
'Diagnostics logging is enabled, this may cause significant overhead.'
)
}
function initialize(appConfig) {
logger.debug(
'Loading agent from %s',
__dirname
)

try {
logger.debug("Process was running %s seconds before agent was loaded.",
process.uptime())
// Technically we run on 0.6, until we verify there are 0 users on 0.6, we
// should leave this code doing a check against 0.6, but then advise that
// people upgrade to one of our officially supported version (0.8 and higher)
if (semver.satisfies(process.version, '<0.6.0')) {
message = "New Relic for Node.js requires a version of Node equal to or\n" +
"greater than 0.8.0. Not starting!"

if (appNames.length < 1) {
message = "New Relic requires that you name this application!\n" +
"Set app_name in your newrelic.js file or set environment variable\n" +
"NEW_RELIC_APP_NAME. Not starting!"
logger.error(message)
throw new Error(message)
}

var shimmer = require('./lib/shimmer.js')
shimmer.patchModule(agent)
shimmer.bootstrapInstrumentation(agent)
logger.debug("Current working directory at module load is %s.", process.cwd())
logger.debug("Process title is %s.", process.title)
logger.debug("Application was invoked as %s.", process.argv.join(' '))

/* Loading the configuration can throw if a configuration file isn't found and
* the environment variable NEW_RELIC_NO_CONFIG_FILE isn't set.
*/
var config = require('./lib/config.js').initialize(appConfig)
if (!config.agent_enabled) {
logger.info("Module not enabled in configuration; not starting.")
} else {
/* Only load the rest of the module if configuration is available and the
* configurator didn't throw.
*
* The agent must be a singleton, or else module loading will be patched
* multiple times, with undefined results. New Relic's instrumentation
* can't be enabled or disabled without an application restart.
*/
var Agent = require('./lib/agent.js')
agent = new Agent(config)
var appNames = agent.config.applications()

if (config.logging.diagnostics) {
logger.warn(
'Diagnostics logging is enabled, this may cause significant overhead.'
)
}

agent.start(function cb_start(error) {
if (!error) {
return logger.debug("New Relic for Node.js is connected to New Relic.")
if (appNames.length < 1) {
message = "New Relic requires that you name this application!\n" +
"Set app_name in your newrelic.js file or set environment variable\n" +
"NEW_RELIC_APP_NAME. Not starting!"
logger.error(message)
throw new Error(message)
}

var errorMessage = "New Relic for Node.js halted startup due to an error:"
logger.error(error, errorMessage)
var shimmer = require('./lib/shimmer.js')
shimmer.patchModule(agent)
shimmer.bootstrapInstrumentation(agent)

agent.start(function cb_start(error) {
if (!error) {
return logger.debug("New Relic for Node.js is connected to New Relic.")
}

var errorMessage = "New Relic for Node.js halted startup due to an error:"
logger.error(error, errorMessage)

console.error(errorMessage)
console.error(error.stack)
})
}
} catch (error) {
message = "New Relic for Node.js was unable to bootstrap itself due to an error:"
logger.error(error, message)

console.error(errorMessage)
console.error(error.stack)
})
console.error(message)
console.error(error.stack)
}
} catch (error) {
message = "New Relic for Node.js was unable to bootstrap itself due to an error:"
logger.error(error, message)

console.error(message)
console.error(error.stack)
}
var API
if (agent) {
API = require('./api.js')
} else {
API = require('./stub_api.js')
}

var API
if (agent) {
API = require('./api.js')
} else {
API = require('./stub_api.js')
require.cache.__NR_cache = module.exports = new API(agent)
}

require.cache.__NR_cache = module.exports = new API(agent)
}
2 changes: 1 addition & 1 deletion test/integration/index-disabled.tap.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ test("loading the application via index.js with agent disabled", function (t) {
t.plan(2)

process.env.NEW_RELIC_ENABLED = 'false'
var api = require('../../index.js')
var api = require('../../index.js')()

t.ok(api, "have an API")
t.notOk(api.agent, "no associated agent")
Expand Down
4 changes: 2 additions & 2 deletions test/integration/index.tap.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ test('loading the application via index.js', {timeout: 5000}, function(t) {
process.env.NEW_RELIC_LICENSE_KEY = 'd67afc830dab717fd163bfcb0b8b88423e9a1a3b'

t.doesNotThrow(function cb_doesNotThrow() {
var api = require('../../index.js')
var api = require('../../index.js')()
agent = api.agent
t.equal(agent._state, 'starting', "agent is booting")
}, "just loading the agent doesn't throw")
Expand All @@ -33,7 +33,7 @@ test('loading the application via index.js', {timeout: 5000}, function(t) {
}
}

var api = require('../../index.js')
var api = require('../../index.js')()
agent = api.agent
t.ok(agent)

Expand Down
2 changes: 1 addition & 1 deletion test/smoke/express-server.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require('../../index.js') // same as require('newrelic')
require('../../index.js')() // same as require('newrelic')
var express = require('express')

var app = express()
Expand Down