serverapm-service.js
3.24 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
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
const Sender = require('influx-batch-sender');
const MysqlSender = require('../lib/mysql-sender');
const config = require('../common/config');
const msg2row = require('./msg2row');
const cache = global.yoho.cache.master;
const logger = global.yoho.logger;
const errorSqlSender = new MysqlSender(config.table.error);
const slowRouterSqlSender = new MysqlSender(config.table.slow);
const routeInfluxSender = new Sender(config.reportRoute);
const apiInfluxSender = new Sender(config.reportApi);
const _ = require('lodash');
const API_BLACK_LIST = [
'app.shop.banner'
];
async function handleWebServerDuration(m) {
let duration = _.parseInt(m.fields.duration);
if (duration > config.slowRoute.min / 10 && duration < config.slowRoute.max) {
const newData = _.merge({}, m, {
fields: {
duration
}
});
slowRouterSqlSender.addMessage(msg2row.slowRouter(newData));
}
if (m.tags.type.toLowerCase() === 'api') {
if (_.includes(API_BLACK_LIST, m.tags.api)) {
return;
}
logger.debug('[api] route info [%s]', JSON.stringify(m));
apiInfluxSender.addMessage({
tags: {
app: m.tags.app,
host: m.tags.hostname,
api: m.tags.api
},
fields: {
duration,
times: 1
}
});
}
if (m.tags.type.toLowerCase() === 'route') {
logger.debug('[server] route info [%s]', JSON.stringify(m));
routeInfluxSender.addMessage({
tags: {
app: m.tags.app,
host: m.tags.hostname,
route: m.tags.route,
ajax: m.fields.ajax
},
fields: {
duration
}
});
try {
const degradeKey = `${m.tags.app}:degradessr`;
const data = await cache.getAsync(degradeKey);
if (data) {
const list = JSON.parse(data || '[]');
_.forEach(list, async item => {
const key = `${degradeKey}:${item.time}`;
const result = await cache.getAsync(key);
if (!result) {
cache.setAsync(key, 1, item.time);
} else {
cache.incrAsync(key, 1);
}
});
}
} catch (error) {
logger.error('[server] route info degradeKey [%s]', error.message);
}
}
}
function handleErrorReport(m) {
routeInfluxSender.addMessage({
measurement: 'error-info',
tags: {
app: m.tags.app,
host: m.tags.hostname,
route: m.tags.route,
code: m.tags.code
},
fields: {
times: 1
}
});
// 排除情况
const msg = _.get(m, 'fields.message', '');
if (!msg) {
return;
}
if (msg === '""') {
return;
}
const type = _.get(m, 'tags.type', '');
if (type === 'api') {
logger.debug('[api] error info [%s]', JSON.stringify(m));
} else {
logger.debug('[server] error info [%s]', JSON.stringify(m));
}
errorSqlSender.addMessage(msg2row.errorRouter(m));
}
function handleProcessInfo(m) {
logger.debug('[process] info [%s]', JSON.stringify(m));
m.measurement = 'process-info';
_.set(m, 'fields.memory', _.parseInt(_.get(m, 'fields.memory', '0')));
_.set(m, 'fields.cpu', _.parseInt(_.get(m, 'fields.cpu', '0')));
routeInfluxSender.addMessage(m);
}
module.exports = {
handleErrorReport,
handleProcessInfo,
handleWebServerDuration
};