-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
82 lines (76 loc) · 3.02 KB
/
index.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
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
module.exports = function logRequestDetailsMiddleware(callback) {
return function logRequestDetails(agent) {
agent.on('request', ({req}) => {
const url = agent.url;
const eventTimes = {
startAt: getTimeNotASubjectOfClockDrift(),
socketAssigned: undefined,
dnsLookupAt: undefined,
tcpConnectionAt: undefined,
tlsHandshakeAt: undefined,
firstByteAt: undefined,
endAt: undefined
};
req.on('socket', (socket) => {
eventTimes.socketAssigned = getTimeNotASubjectOfClockDrift();
socket.on('lookup', () => {
eventTimes.dnsLookupAt = getTimeNotASubjectOfClockDrift();
});
socket.on('connect', () => {
eventTimes.tcpConnectionAt = getTimeNotASubjectOfClockDrift();
});
socket.on('secureConnect', () => {
eventTimes.tlsHandshakeAt = getTimeNotASubjectOfClockDrift();
});
socket.on('timeout', () => {
const err = new Error(`ETIMEDOUT for req.url: ${req.url}`);
err.code = `ETIMEDOUT`;
callback(err);
});
});
req.on('response', (res) => {
res.once('readable', () => {
eventTimes.firstByteAt = getTimeNotASubjectOfClockDrift();
});
(function consumeStreamSoEndEventAlwaysFire() {
res.on('data', () => {});
}());
res.on('end', () => {
eventTimes.endAt = getTimeNotASubjectOfClockDrift();
callback(null, {
url,
status: res.statusCode,
timings: getTimings(eventTimes)
});
});
});
});
}
};
function getTimeNotASubjectOfClockDrift() {
return process.hrtime();
}
function getTimings (eventTimes) {
return {
socketAssigned: getHrTimeDurationInMs(eventTimes.startAt, eventTimes.socketAssigned),
// There is no DNS lookup with IP address
dnsLookup: eventTimes.dnsLookupAt !== undefined ?
getHrTimeDurationInMs(eventTimes.socketAssigned, eventTimes.dnsLookupAt) : undefined,
tcpConnection: eventTimes.tcpConnectionAt !== undefined ?
getHrTimeDurationInMs(eventTimes.dnsLookupAt || eventTimes.socketAssigned, eventTimes.tcpConnectionAt) : undefined,
// There is no TLS handshake without https
tlsHandshake: eventTimes.tlsHandshakeAt !== undefined ?
(getHrTimeDurationInMs(eventTimes.tcpConnectionAt, eventTimes.tlsHandshakeAt)) : undefined,
firstByte: getHrTimeDurationInMs((eventTimes.tlsHandshakeAt || eventTimes.tcpConnectionAt || eventTimes.socketAssigned), eventTimes.firstByteAt),
contentTransfer: getHrTimeDurationInMs(eventTimes.firstByteAt, eventTimes.endAt),
total: getHrTimeDurationInMs(eventTimes.startAt, eventTimes.endAt)
}
}
const NS_PER_SEC = 1e9;
const MS_PER_NS = 1e6;
function getHrTimeDurationInMs (startTime, endTime) {
const secondDiff = endTime[0] - startTime[0];
const nanoSecondDiff = endTime[1] - startTime[1];
const diffInNanoSecond = secondDiff * NS_PER_SEC + nanoSecondDiff;
return diffInNanoSecond / MS_PER_NS
}