forked from newrelic/node-newrelic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
109 lines (87 loc) · 3.48 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
'use strict';
var path = require('path')
, logger = require(path.join(__dirname, 'lib', 'logger.js'))
, message
, agent
;
var APP_NAME_REGEX = /^[A-Za-z0-9 -_\[\](){}?!.'"]*$/;
var agentVersion = require(path.join(__dirname, 'package.json')).version;
logger.trace("Using New Relic for Node.js version %s.", agentVersion);
try {
logger.debug("Process was running %s seconds before agent was loaded.",
process.uptime());
if (process.version && process.version.split('.')[1] < 6) {
message = "New Relic for Node.js requires a version of Node equal to or\n" +
"greater than 0.6.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(' '));
/* 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(path.join(__dirname, '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(path.join(__dirname, 'lib', 'agent.js'));
agent = new Agent(config);
var appNames = agent.config.applications();
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 testVal;
appNames.forEach(function (name) {
testVal = name.match(APP_NAME_REGEX);
if (!testVal){
message = "New Relic requires that you name this application using alphanumeric" +
" and certain punctuation characters ([](){}.?!') only.\n" +
"Reset app_name to follow these naming conventions " +
"in your newrelic.js file or set environment variable\n" +
"NEW_RELIC_APP_NAME. Not starting!";
config.agent_enabled = false;
config.emit('agent_enabled', false);
logger.error(message);
console.log(message);
}
});
var shimmer = require(path.join(__dirname, '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 message = "New Relic for Node.js halted startup due to an error:";
logger.error(error, message);
console.error(message);
console.error(error.stack);
});
}
}
catch (error) {
var 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(path.join(__dirname, 'api.js'));
}
else {
API = require(path.join(__dirname, 'stub_api.js'));
}
module.exports = new API(agent);