server-apm.js
1.49 KB
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
const lineparse = require('../lib/line-parse');
const Sender = require('influx-batch-sender');
const MysqlSender = require('../lib/mysql-sender');
const config = require('../common/config');
const msg2row = require('./msg2row');
const errorSender = new MysqlSender(config.table.error);
const slowRouterSender = new MysqlSender(config.table.slow);
const influxSender = new Sender(config.reportRoute);
const server = {
async handle(data) {
let msgs = lineparse.parse(data);
for (let m of msgs) {
if (m.measurement === 'web-server-duration') {
let duration = parseInt(m.fields.duration);
if (duration > config.slowRoute.min || duration < config.slowRoute.max) {
slowRouterSender.addMessage(msg2row.slowRouter(m));
}
influxSender.addMessage({
measurement: 'route-info',
tags: {
app: m.tags.app,
hostname: m.tags.hostname,
route: m.tags.route
},
field: {
duration: m.duration
}
});
} else if (m.measurement === 'error-report') {
errorSender.addMessage(msg2row.errorRouter(m));
}
}
}
};
module.exports = function(req, res, next) {
let data = req.body || '';
server.handle(data).then(() => {
res.json();
}).catch(next);
};