Skip to content

Commit aaf6a2a

Browse files
Copilotfengmk2
andauthored
feat: populate dnslookup timing by capturing DNS resolution time on sockets
Captures DNS lookup time via Socket.prototype.emit monkey-patch for the 'lookup' event. When the first request is sent on a socket, the DNS lookup time is calculated relative to the request start time and stored in the timing object. Closes #577 Agent-Logs-Url: https://github.com/node-modules/urllib/sessions/efde376e-b12f-42f6-9490-2ce39519fe7c Co-authored-by: fengmk2 <156269+fengmk2@users.noreply.github.com>
1 parent ca667f6 commit aaf6a2a

3 files changed

Lines changed: 21 additions & 0 deletions

File tree

src/diagnosticsChannel.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,15 @@ Socket.prototype.destroy = function (err?: any) {
6767
return destroySocket.call(this, err);
6868
};
6969

70+
// capture DNS lookup time on socket via 'lookup' event
71+
const originalSocketEmit = Socket.prototype.emit;
72+
Socket.prototype.emit = function (this: SocketExtend, event: string, ...args: any[]) {
73+
if (event === 'lookup') {
74+
this[symbols.kSocketDnsLookupTime] = performance.now();
75+
}
76+
return originalSocketEmit.apply(this, [event, ...args] as any);
77+
} as typeof originalSocketEmit;
78+
7079
function getRequestOpaque(request: DiagnosticsChannel.Request, kHandler?: symbol) {
7180
if (!kHandler) return;
7281
const handler = Reflect.get(request, kHandler);
@@ -201,6 +210,13 @@ export function initDiagnosticsChannel(): void {
201210
opaque[symbols.kRequestStartTime],
202211
socket[symbols.kSocketStartTime] as number,
203212
);
213+
// kSocketDnsLookupTime - kRequestStartTime = dns lookup time
214+
if (socket[symbols.kSocketDnsLookupTime]) {
215+
opaque[symbols.kRequestTiming].dnslookup = performanceTime(
216+
opaque[symbols.kRequestStartTime],
217+
socket[symbols.kSocketDnsLookupTime] as number,
218+
);
219+
}
204220
}
205221
});
206222

src/symbols.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const symbols: {
99
kSocketConnectHost: symbol;
1010
kSocketConnectPort: symbol;
1111
kSocketConnectProtocol: symbol;
12+
kSocketDnsLookupTime: symbol;
1213
kHandledRequests: symbol;
1314
kHandledResponses: symbol;
1415
kRequestSocket: symbol;
@@ -30,6 +31,7 @@ const symbols: {
3031
kSocketConnectHost: Symbol('socket connect params: host'),
3132
kSocketConnectPort: Symbol('socket connect params: port'),
3233
kSocketConnectProtocol: Symbol('socket connect params: protocol'),
34+
kSocketDnsLookupTime: Symbol('socket dns lookup time'),
3335
kHandledRequests: Symbol('handled requests per socket'),
3436
kHandledResponses: Symbol('handled responses per socket'),
3537
kRequestSocket: Symbol('request on the socket'),

test/options.timing.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ describe('options.timing.test.ts', () => {
3030
assert(res.timing.waiting > 0);
3131
assert(res.timing.queuing > 0);
3232
assert(res.timing.connected > 0);
33+
assert(res.timing.dnslookup > 0);
34+
assert(res.timing.dnslookup <= res.timing.connected);
3335
assert(res.timing.requestHeadersSent > 0);
3436
assert(res.timing.requestSent > 0);
3537
assert(res.timing.contentDownload > 0);
@@ -49,6 +51,7 @@ describe('options.timing.test.ts', () => {
4951
assert(res.timing.waiting > 0);
5052
assert(res.timing.queuing > 0);
5153
assert.equal(res.timing.connected, 0);
54+
assert.equal(res.timing.dnslookup, 0);
5255
assert(res.timing.requestHeadersSent > 0);
5356
assert(res.timing.requestSent > 0);
5457
assert(res.timing.contentDownload > 0);

0 commit comments

Comments
 (0)