clientapm-service.js
5.03 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
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
const msg2row = require('./msg2row');
const Sender = require('influx-batch-sender');
const MysqlSender = require('../lib/mysql-sender');
const ipService = require('./ip-service');
const useragent = require('useragent');
const _ = require('lodash');
const config = global.yoho.config;
const logger = global.yoho.logger;
const durationType = {
dcl: 'domcontentload',
ld: 'domreadyload',
fs: 'firstscreen'
};
const routeInfluxSender = new Sender(config.reportRoute);
const slowRouterSqlSender = new MysqlSender(config.table.slow);
const errorRouterSqlSender = new MysqlSender(config.table.error);
const perfReportSqlSender = new MysqlSender(config.table.perf);
function handleClientError(scope, item) {
// 无效数据
if (!/cdn.yoho.cn/.test(item.sc)) {
return;
}
if (!item.msg) {
return;
}
if (item.msg.toLowerCase() === 'script error') {
return;
}
if (item.msg.toLowerCase().charCodeAt(0) < 'a'.charCodeAt(0) ||
item.msg.toLowerCase().charCodeAt(0) > 'z'.charCodeAt(0)) {
return;
}
let data = {
tags: {
app: scope.app,
route: item.r
},
fields: {
reqID: item.rid,
uid: item.u,
udid: item.ud
},
time: new Date().getTime() * 1000000
};
_.merge(data, {
measurement: 'error-report',
tags: {
type: 'client'
},
fields: Object.assign({
message: item.msg,
useragent: scope.useragent,
stack: item.st ? item.st.replace(/"/g, '') : '',
script: item.sc,
line: _.parseInt(item.ln || 0),
column: _.parseInt(item.cn || 0),
})
});
const ipInfo = ipService.getIsp(scope.ip);
const row = Object.assign(msg2row.errorRouter(data), ipInfo, {ip: scope.ip});
logger.debug('[client] error info [%s]', JSON.stringify(row));
errorRouterSqlSender.addMessage(row);
}
function handleClientFirst(scope, item) {
let data = {
tags: {
app: scope.app,
route: item.r
},
fields: {
reqID: item.rid,
uid: item.u,
udid: item.ud
},
time: new Date().getTime() * 1000000
};
if (!item.t) {
return;
}
const duration = _.parseInt(item.t);
if (!duration || duration > 1000 * 60 * 10) {
return;
}
_.merge(data, {
measurement: 'route-info',
tags: {
type: durationType[item.tp],
},
fields: {
duration: duration,
useragent: scope.useragent
}
});
routeInfluxSender.addMessage({
measurement: 'client-info',
tags: {
app: scope.app,
host: item.hostname,
route: item.r,
type: durationType[item.tp]
},
fields: {
duration: duration
}
});
logger.debug('[client] slow route info [%s]', JSON.stringify(data));
if (duration > config.slowRoute.min && duration < config.slowRoute.max) {
slowRouterSqlSender.addMessage(msg2row.slowRouter(data));
}
}
async function handleClientTiming(scope, item) {
const agent = useragent.lookup(scope.useragent);
const data = {
app: scope.app,
reqid: item.rid,
uid: item.u,
udid: item.ud,
route: item.r,
create_time: new Date().getTime(),
domType: 'html',
mineType: 'html',
url: item.pt,
dnsTime: _.parseInt(item.dt || '0'),
tcpTime: _.parseInt(item.tt || '0'),
responseTime: _.parseInt(item.rt || '0'),
domParseTime: _.parseInt(item.domt || '0'),
pageRenderTime: _.parseInt(item.et || '0'),
pageInteractTime: _.parseInt(item.ot || '0'),
pageLoadTime: _.parseInt(item.rrt || '0'),
screenWidth: _.parseInt(item.sw || '0'),
screenHeight: _.parseInt(item.sh || '0'),
useragent: scope.useragent,
os: item.pf,
ip: scope.ip,
browserName: agent.family,
browserVersion: agent.toVersion(),
osName: agent.os.family,
osVersion: agent.os.toVersion()
};
const ipInfo = ipService.getIsp(scope.ip);
_.merge(data, ipInfo);
logger.debug('[client] perf info [%s]', JSON.stringify(data));
perfReportSqlSender.addMessage(data);
}
function handleClientResource(scope, item) {
const data = {
app: scope.app,
reqid: item.rid,
uid: item.u,
udid: item.ud,
route: item.r,
create_time: new Date().getTime(),
domType: 'resource',
mineType: item.mtp,
dnsTime: _.parseInt(item.dt || '0'),
tcpTime: _.parseInt(item.tt || '0'),
responseTime: _.parseInt(item.rt || '0'),
pageLoadTime: _.parseInt(item.rrt || '0'),
};
logger.debug('[client] resource perf info [%s]', JSON.stringify(data));
perfReportSqlSender.addMessage(data);
}
module.exports = {
handleClientError,
handleClientFirst,
handleClientTiming,
handleClientResource
};