forked from X-Profiler/xprofiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxprofiler.js
89 lines (71 loc) · 2.24 KB
/
xprofiler.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
'use strict';
const xprofiler = require('bindings')('xprofiler');
const utils = require('./lib/utils');
const clean = require('./lib/clean');
const { patch } = require('./patch');
const configure = require('./lib/configure');
const configList = require('./xprofiler.json');
const runOnceStatus = {
bypassLogThreadStarted: false,
commandsListenerThreadStarted: false,
hooksSetted: false
};
let configured = false;
function checkNecessary() {
if (!configured) {
throw new Error('must run "require(\'xprofiler\')()" to set xprofiler config first!');
}
}
function runOnce(onceKey, onceFunc) {
checkNecessary();
if (runOnceStatus[onceKey]) {
return;
}
runOnceStatus[onceKey] = true;
onceFunc();
}
function start(config = {}) {
// set config by user and env
const finalConfig = exports.setConfig(config);
// clean & set logdir info to file
const logdir = finalConfig.log_dir;
clean(logdir);
utils.setLogDirToFile(logdir);
if (process.env.XPROFILER_UNIT_TEST_SINGLE_MODULE !== 'YES') {
// start performance log thread
exports.runLogBypass();
// start commands listener thread
exports.runCommandsListener();
// set hooks
exports.setHooks();
}
// patch modules
patch(finalConfig, {
// http status
addLiveRequest: xprofiler.addLiveRequest,
addCloseRequest: xprofiler.addCloseRequest,
addSentRequest: xprofiler.addSentRequest,
addRequestTimeout: xprofiler.addRequestTimeout,
addHttpStatusCode: xprofiler.addHttpStatusCode
});
}
exports = module.exports = start;
exports.start = start;
exports.setConfig = function (config) {
// set config
const finalConfig = configure(configList, config);
configured = true;
xprofiler.configure(finalConfig);
return finalConfig;
};
exports.getXprofilerConfig = function () {
checkNecessary();
return xprofiler.getConfig();
};
['info', 'error', 'debug'].forEach(level => exports[level] = function (...args) {
checkNecessary();
xprofiler[level](...args);
});
exports.runLogBypass = runOnce.bind(null, 'bypassLogThreadStarted', xprofiler.runLogBypass);
exports.runCommandsListener = runOnce.bind(null, 'commandsListenerThreadStarted', xprofiler.runCommandsListener);
exports.setHooks = runOnce.bind(null, 'hooksSetted', xprofiler.setHooks);