Skip to content

Commit bd11d0c

Browse files
committed
chore(http): add dns and connect time
1 parent 21ae449 commit bd11d0c

File tree

2 files changed

+66
-36
lines changed

2 files changed

+66
-36
lines changed

bin/tsw/ajax/ajax.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -402,23 +402,21 @@ Ajax.prototype.doRequest = function(opt) {
402402
times.start = new Date().getTime();
403403

404404
function report(opt, isFail, code) {
405-
405+
const toIp = request.remoteIp || opt.ip;
406406

407407
if (isTST.isTST(opt)) {
408-
// 忽略安全中心请求
409-
return;
408+
return; // 忽略安全中心请求
410409
}
411410

412411
if (isFail === 1 && opt.ignoreErrorReport) {
413412
isFail = 2;
414413
}
415414

416415
if (opt.dcapi) {
417-
418416
logger.debug(logPre + '返回码:' + code + ', isFail:' + isFail);
419417

420418
dcapi.report(Deferred.extend({}, opt.dcapi, {
421-
toIp: opt.ip,
419+
toIp: toIp,
422420
code: code,
423421
isFail: isFail,
424422
delay: new Date() - times.start
@@ -554,6 +552,17 @@ Ajax.prototype.doRequest = function(opt) {
554552
request.setNoDelay(true);
555553
request.setSocketKeepAlive(true);
556554

555+
request.once('socket', function(socket) {
556+
socket.once('lookup', (err, address, family, host) => {
557+
if (err) {
558+
logger.error(logPre + err.stack);
559+
this.emit('fail');
560+
return;
561+
}
562+
this.remoteIp = address;
563+
});
564+
});
565+
557566
defer.always(function() {
558567
clearTimeout(tid);
559568
request.removeAllListeners();

bin/tsw/runtime/capturer.js

Lines changed: 52 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,10 @@ process.nextTick(function() {
3939
let bodySize = 0;
4040
const maxBodySize = 1024 * 1024;
4141
const timeStart = Date.now();
42-
let timeEnd = 0;
42+
let timeLookup = timeStart;
43+
let timeConnect = timeStart;
4344
let timeResponse = 0;
44-
// var timeCurr = timeStart;
45+
let timeEnd = 0;
4546
let remoteAddress = '';
4647
let remotePort = '';
4748
let localAddress = '';
@@ -122,10 +123,10 @@ process.nextTick(function() {
122123
GotRequestHeaders: new Date(timeStart),
123124
ClientDoneRequest: new Date(timeStart),
124125
GatewayTime: 0,
125-
DNSTime: 0,
126-
TCPConnectTime: 0,
126+
DNSTime: timeLookup - timeStart,
127+
TCPConnectTime: timeConnect - timeStart,
127128
HTTPSHandshakeTime: 0,
128-
ServerConnected: new Date(timeStart),
129+
ServerConnected: new Date(timeConnect),
129130
FiddlerBeginRequest: new Date(timeStart),
130131
ServerGotRequest: new Date(timeStart),
131132
ServerBeginResponse: new Date(timeResponse),
@@ -139,6 +140,51 @@ process.nextTick(function() {
139140
logJson.ajax.push(curr);
140141
};
141142

143+
144+
const finish = function(maybeResponse) {
145+
if (timeEnd) {
146+
return;
147+
}
148+
149+
timeEnd = new Date().getTime();
150+
151+
if (captureBody) {
152+
buffer = Buffer.concat(result);
153+
result = [];
154+
}
155+
156+
// 上报
157+
if (captureBody) {
158+
report(maybeResponse);
159+
}
160+
};
161+
162+
request.once('socket', function(socket) {
163+
socket.once('lookup', (err, address, family, host) => {
164+
timeLookup = Date.now();
165+
if (err) {
166+
logger.error(logPre + err.stack);
167+
finish();
168+
return;
169+
}
170+
const cost = timeLookup - timeStart;
171+
logger.debug(`${logPre}dns lookup ${host} -> ${address}, cost ${cost}ms`);
172+
});
173+
174+
socket.once('connect', function() {
175+
timeConnect = Date.now();
176+
const cost = timeConnect - timeStart;
177+
remoteAddress = this.remoteAddress;
178+
remotePort = this.remotePort;
179+
logger.debug(`${logPre}connect ${remoteAddress}:${remotePort}, cost ${cost}ms`);
180+
});
181+
});
182+
183+
request.once('error', function(err) {
184+
logger.error(err.stack);
185+
finish();
186+
});
187+
142188
request.once('response', (response) => {
143189
timeResponse = Date.now();
144190

@@ -163,35 +209,10 @@ process.nextTick(function() {
163209

164210
const done = function() {
165211
this.removeListener('data', data);
166-
167-
if (timeEnd) {
168-
return;
169-
}
170-
171-
timeEnd = new Date().getTime();
172-
173-
if (captureBody) {
174-
buffer = Buffer.concat(result);
175-
result = [];
176-
}
177-
178-
// 上报
179-
if (captureBody) {
180-
report(response);
181-
}
212+
finish(response);
182213
};
183214

184215
const data = function(chunk) {
185-
// var cost = Date.now() - timeCurr;
186-
187-
// timeCurr = Date.now();
188-
189-
// logger.debug('${logPre}receive data: ${size},\tcost: ${cost}ms',{
190-
// logPre: logPre,
191-
// cost: cost,
192-
// size: chunk.length
193-
// });
194-
195216
bodySize += chunk.length;
196217

197218
if (captureBody && bodySize <= maxBodySize) {

0 commit comments

Comments
 (0)