-
Notifications
You must be signed in to change notification settings - Fork 127
/
index.js
388 lines (355 loc) · 12.7 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/*******************************************************************************
* Copyright 2014, 2015 IBM Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*******************************************************************************/
'use strict';
var VERSION = require('./package.json').version;
var assert = require('assert');
if (global.Appmetrics) {
assert(
global.Appmetrics.VERSION === VERSION,
'Multiple versions of Node Application Metrics are being initialized.\n' +
'This version ' +
VERSION +
' is incompatible with already initialized\n' +
'version ' +
global.Appmetrics.VERSION +
'.\n'
);
exports = module.exports = global.Appmetrics;
} else {
// This instance is the global, do all the setup here
global.Appmetrics = module.exports;
module.exports.VERSION = VERSION;
var path = require('path');
var main_filename = require.main != null ? require.main.filename : '';
var module_dir = path.dirname(module.filename);
var aspect = require('./lib/aspect.js');
var request = require('./lib/request.js');
var fs = require('fs');
var agent = require('./appmetrics');
const os = require('os');
var notOnZOS = !(process.platform === 'os390');
if (notOnZOS) {
var headlessZip = require('./headless_zip.js');
var heapdump = require('./heapdump.js');
}
// Set the plugin search path
agent.spath(path.join(module_dir, 'plugins'));
// Edit LIBPATH on AIX to enable libagentcore to be loaded
if (process.platform == 'aix') process.env.LIBPATH = module_dir + ':' + process.env.LIBPATH;
var hcAPI = require('./appmetrics-api.js');
var jsonProfilingMode = false;
var propertyMappings = {
mqttPort: 'com.ibm.diagnostics.healthcenter.mqtt.broker.port',
mqttHost: 'com.ibm.diagnostics.healthcenter.mqtt.broker.host',
applicationID: 'com.ibm.diagnostics.healthcenter.mqtt.application.id',
mqtt: 'com.ibm.diagnostics.healthcenter.mqtt',
profiling: 'com.ibm.diagnostics.healthcenter.data.profiling',
};
if (notOnZOS) {
var headlessPropertyMappings = {
'appmetrics.file.collection': 'com.ibm.diagnostics.healthcenter.headless',
'appmetrics.file.max.size': 'com.ibm.diagnostics.healthcenter.headless.files.max.size',
'appmetrics.file.run.duration': 'com.ibm.diagnostics.healthcenter.headless.run.duration',
'appmetrics.file.delay.start': 'com.ibm.diagnostics.healthcenter.headless.delay.start',
'appmetrics.file.run.pause.duration': 'com.ibm.diagnostics.healthcenter.headless.run.pause.duration',
'appmetrics.file.run.number.of.runs': 'com.ibm.diagnostics.healthcenter.headless.run.number.of.runs',
'appmetrics.file.files.to.keep': 'com.ibm.diagnostics.healthcenter.headless.files.to.keep',
'appmetrics.file.output.directory': 'com.ibm.diagnostics.healthcenter.headless.output.directory',
};
}
/*
* Load module probes into probes array by searching the probes directory.
* We handle the 'trace' probe as a special case because we don't want to put
* the probe hooks in by default due to the performance cost.
*/
var probes = [];
var traceProbe;
var dirPath = path.join(__dirname, 'probes');
var files = fs.readdirSync(dirPath);
files.forEach(function(fileName) {
var file = path.join(dirPath, fileName);
var probeModule = new (require(file))();
if (probeModule.name === 'trace') {
traceProbe = probeModule;
} else {
probes.push(probeModule);
}
});
var latencyData = {
count: 0,
min: 1 * 60 * 1000,
max: 0,
total: 0,
};
var latencyCheck = function() {
var start = process.hrtime();
setImmediate(function(start) {
var delta = process.hrtime(start);
var latency = delta[0] * 1000 + delta[1] / 1000000;
latencyData.count++;
latencyData.min = Math.min(latencyData.min, latency);
latencyData.max = Math.max(latencyData.max, latency);
latencyData.total = latencyData.total + latency;
}, start);
};
var latencyReport = function() {
if (latencyData.count == 0) return;
var latency = {
min: latencyData.min,
max: latencyData.max,
avg: latencyData.total / latencyData.count,
};
module.exports.emit('eventloop', { time: Date.now(), latency: latency });
latencyData.count = 0;
latencyData.min = 1 * 60 * 1000;
latencyData.max = 0;
latencyData.total = 0;
};
var latencyCheckInterval = 500;
var latencyReportInterval = 5000;
var latencyRunning = true;
var latencyCheckLoop = setInterval(latencyCheck, latencyCheckInterval);
var latencyReportLoop = setInterval(latencyReport, latencyReportInterval);
latencyCheckLoop.unref();
latencyReportLoop.unref();
/*
* Patch the module require function to run the probe attach function
* for any matching module. This loads the monitoring probes into the modules
*/
var data = {};
/* eslint no-proto:0 */
aspect.after(module.__proto__, 'require', data, function(obj, methodName, args, context, ret) {
if (ret == null || ret.__ddProbeAttached__) {
return ret;
} else {
for (var i = 0; i < probes.length; i++) {
if (probes[i].name === args[0]) {
ret = probes[i].attach(args[0], ret, module.exports);
}
if (probes[i].name === 'trace') {
ret = probes[i].attach(args[0], ret);
}
}
return ret;
}
});
if (notOnZOS) {
agent.setHeadlessZipFunction(headlessZip.headlessZip);
}
// Export any functions exported by the agent
for (var prop in agent) {
if (typeof agent[prop] == 'function') {
module.exports[prop] = agent[prop];
}
}
/*
* Provide API to enable data collection for a given data type.
* Profiling is done via a control message to the core monitoring agent.
* Requests require asking all probes to enable request events
* Other requests are passed to any probe matching the name
*/
module.exports.enable = function(data, config) {
switch (data) {
case 'profiling':
agent.sendControlCommand('profiling_node', 'on,profiling_node_subsystem');
break;
case 'requests':
probes.forEach(function(probe) {
probe.enableRequests();
});
break;
case 'trace':
if (probes.indexOf(traceProbe) === -1) {
probes.push(traceProbe);
}
traceProbe.enable();
break;
case 'eventloop':
if (latencyRunning === true) break;
latencyRunning = true;
latencyCheckLoop = setInterval(latencyCheck, latencyCheckInterval);
latencyReportLoop = setInterval(latencyReport, latencyReportInterval);
break;
default:
probes.forEach(function(probe) {
if (probe.name == data) {
probe.enable();
}
});
}
if (config) module.exports.setConfig(data, config);
};
/*
* Provide API to disable data collection for a given data type.
* Profiling is done via a control message to the core monitoring agent.
* Requests require asking all probes to disable request events
* Other requests are passed to any probe matching the name
*/
module.exports.disable = function(data) {
switch (data) {
case 'profiling':
agent.sendControlCommand('profiling_node', 'off,profiling_node_subsystem');
break;
case 'requests':
probes.forEach(function(probe) {
probe.disableRequests();
});
break;
case 'eventloop':
if (latencyRunning === false) break;
latencyRunning = false;
clearInterval(latencyCheckLoop);
clearInterval(latencyReportLoop);
break;
default:
probes.forEach(function(probe) {
if (probe.name == data) {
probe.disable();
}
});
}
};
/*
* Set the config for a type of data. These are passed through to the relevant
* probes except in the case of 'requests'. Here we check for any excludeModules config,
* and if present use that to control the relevant probes directly.
*/
module.exports.setConfig = function(data, config) {
switch (data) {
case 'requests':
request.setConfig(config);
/* check for exclude modules and disable those to be excluded */
if (typeof config.excludeModules !== 'undefined') {
config.excludeModules.forEach(function(module) {
probes.forEach(function(probe) {
if (probe.name === module) {
probe.disableRequests();
}
});
});
}
break;
case 'advancedProfiling':
if (typeof config.threshold !== 'undefined')
agent.sendControlCommand('profiling_node', config.threshold + ',profiling_node_threshold');
break;
default:
probes.forEach(function(probe) {
if (probe.name == data) {
probe.setConfig(config);
}
});
}
};
// Export emit() API for JS data providers
module.exports.emit = function(topic, data) {
if (typeof this.api !== 'undefined') {
// We have a listener, so fast path the notification to them
this.api.raiseLocalEvent(topic, data);
}
// Publish data that can be visualised in Health Center
if (topic == 'http' || topic == 'mqlight' || topic == 'mongo' || topic == 'mysql') {
data = JSON.stringify(data);
agent.nativeEmit(topic, String(data));
}
};
// Export monitor() API for consuming data in-process
module.exports.monitor = function() {
if (typeof this.api == 'undefined') {
this.start();
this.api = hcAPI.getAPI(agent, module.exports);
}
return this.api;
};
module.exports.lrtime = agent.lrtime;
module.exports.configure = function(options) {
options = options || {};
this.strongTracerInstrument = options.strongTracer ? options.strongTracer.tracer : null;
for (var key in options) {
if (propertyMappings[key]) {
agent.setOption(propertyMappings[key], options[key]);
} else {
agent.setOption(key, options[key]);
}
}
// If user has not specified application ID, use main filename
main_filename = options.applicationID ? options.applicationID : main_filename;
};
module.exports.transactionLink = function(linkName, callback) {
if (!this.strongTracerInstrument) return callback;
return this.strongTracerInstrument.transactionLink(linkName, callback);
};
module.exports.setJSONProfilingMode = function(val) {
jsonProfilingMode = val;
};
module.exports.getJSONProfilingMode = function() {
return jsonProfilingMode;
};
module.exports.getTotalPhysicalMemorySize = function() {
return os.totalmem();
};
if (notOnZOS) {
module.exports.writeSnapshot = function(args) {
return heapdump.writeSnapshot(args);
};
}
module.exports.start = function start() {
agent.setOption(propertyMappings['applicationID'], main_filename);
if (notOnZOS) {
for (var property in headlessPropertyMappings) {
var prop = agent.getOption(property);
if (prop) {
agent.setOption(headlessPropertyMappings[property], prop);
}
}
var headlessOutputDir = agent.getOption('com.ibm.diagnostics.healthcenter.headless.output.directory');
if (headlessOutputDir) {
headlessZip.setHeadlessOutputDir(headlessOutputDir);
}
var headlessFilesToKeep = agent.getOption('com.ibm.diagnostics.healthcenter.headless.files.to.keep');
if (headlessFilesToKeep && !isNaN(headlessFilesToKeep) && headlessFilesToKeep > 0) {
headlessZip.setFilesToKeep(headlessFilesToKeep);
}
}
var am = this;
agent.start();
process.on('exit', function() {
// take the event loop latency methods off the loop
if (latencyRunning === true) {
clearInterval(latencyCheckLoop);
clearInterval(latencyReportLoop);
}
if (notOnZOS) {
var headlessMode = agent.getOption('com.ibm.diagnostics.healthcenter.headless');
}
am.stop();
if (notOnZOS && headlessMode == 'on') {
headlessZip.tryZipOnExit();
}
});
// Start the probes
probes.forEach(function(probe) {
probe.start();
});
return this;
};
// Require the Node.js DC, when appmetrics is fully initialised.
try {
require('ibmapm-embed');
} catch (err) {
console.log('Error initializing APM');
}
}