-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathagent.js
56 lines (47 loc) · 1.43 KB
/
agent.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
'use strict';
const xtransit = require('xtransit');
const xprofiler = require('xprofiler');
const assert = require('assert');
const path = require('path');
const fs = require('fs');
class AgentBootHook {
constructor(agent) {
this.agent = agent;
this.config = agent.config;
}
configWillLoad() {
// nodejs will handler absolute/relative
let logDir = this.config.xtransit.logDir;
logDir = path.resolve(this.config.logger.dir, logDir);
this.config.xtransit.logDir = logDir;
/* istanbul ignore next */
if (!fs.existsSync(logDir)) {
fs.mkdirSync(logDir, { recursive: true });
}
}
configDidLoad() {
xprofiler.start({
log_dir: this.config.xtransit.logDir,
log_interval: this.config.xtransit.logInterval,
check_throw: this.config.xtransit.checkThrow,
});
}
async serverDidReady() {
const { server, appId, appSecret } = this.config.xtransit;
assert(server && appId && appSecret, 'xtransit config error, server, appId, appSecret must be passed in.');
// logger
this.logger = {};
for (const method of [ 'info', 'warn', 'error', 'debug' ]) {
this.logger[method] = (message, ...args) => {
this.agent.logger[method](`[xtransit] ${message}`, ...args);
};
}
// start xtransit
xtransit.start({
logger: this.logger,
...this.config.xtransit,
});
this.logger.info('xtransit start.');
}
}
module.exports = AgentBootHook;