Skip to content

Commit d03ef60

Browse files
committed
lib: add diagnostics channel and perf hooks detail
1 parent 10ee408 commit d03ef60

9 files changed

+125
-14
lines changed

doc/api/diagnostics_channel.md

+18
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,24 @@ Emitted when server receives a request.
428428

429429
Emitted when server sends a response.
430430

431+
`net.client.socket`
432+
433+
* `socket` {net.Socket}
434+
435+
Emitted when new a client socket.
436+
437+
`net.server.socket`
438+
439+
* `socket` {net.Socket}
440+
441+
Emitted when receive a connection.
442+
443+
`udp.socket`
444+
445+
* `socket` {dgram.Socket}
446+
447+
Emitted when call `'createSocket'`.
448+
431449
[`'uncaughtException'`]: process.md#event-uncaughtexception
432450
[`channel.subscribe(onMessage)`]: #channelsubscribeonmessage
433451
[`diagnostics_channel.channel(name)`]: #diagnostics_channelchannelname

doc/api/perf_hooks.md

+5-3
Original file line numberDiff line numberDiff line change
@@ -602,13 +602,15 @@ When `performanceEntry.type` is equal to `'dns'`, the
602602
additional information.
603603

604604
If `performanceEntry.name` is equal to `lookup`, the `detail`
605-
will contain the following properties: `hostname`, `family`, `hints`, `verbatim`.
605+
will contain the following properties: `hostname`, `family`, `hints`, `verbatim`,
606+
`addresses`.
606607

607608
If `performanceEntry.name` is equal to `lookupService`, the `detail` will
608-
contain the following properties: `host`, `port`.
609+
contain the following properties: `host`, `port`, `hostname`, `service`.
609610

610611
If `performanceEntry.name` is equal to `queryxxx` or `getHostByAddr`, the `detail` will
611-
contain the following properties: `host`, `ttl`.
612+
contain the following properties: `host`, `ttl`, `result`, the value of `result` is
613+
same with the result of `queryxxx` or `getHostByAddr`.
612614

613615
## Class: `PerformanceNodeTiming`
614616

lib/dgram.js

+8
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ const {
7474
SendWrap
7575
} = internalBinding('udp_wrap');
7676

77+
const dc = require('diagnostics_channel');
78+
const udpSocketChannel = dc.channel('udp.socket');
79+
7780
const BIND_STATE_UNBOUND = 0;
7881
const BIND_STATE_BINDING = 1;
7982
const BIND_STATE_BOUND = 2;
@@ -145,6 +148,11 @@ function Socket(type, listener) {
145148
this.once('close', () => signal.removeEventListener('abort', onAborted));
146149
}
147150
}
151+
if (udpSocketChannel.hasSubscribers) {
152+
udpSocketChannel.publish({
153+
socket: this,
154+
});
155+
}
148156
}
149157
ObjectSetPrototypeOf(Socket.prototype, EventEmitter.prototype);
150158
ObjectSetPrototypeOf(Socket, EventEmitter);

lib/dns.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ function onlookup(err, addresses) {
112112
}
113113
this.callback(null, addresses[0], this.family || isIP(addresses[0]));
114114
if (this[kPerfHooksDnsLookupContext] && hasObserver('dns')) {
115-
stopPerf(this, kPerfHooksDnsLookupContext);
115+
stopPerf(this, kPerfHooksDnsLookupContext, { detail: { addresses } });
116116
}
117117
}
118118

@@ -133,7 +133,7 @@ function onlookupall(err, addresses) {
133133

134134
this.callback(null, addresses);
135135
if (this[kPerfHooksDnsLookupContext] && hasObserver('dns')) {
136-
stopPerf(this, kPerfHooksDnsLookupContext);
136+
stopPerf(this, kPerfHooksDnsLookupContext, { detail: { addresses } });
137137
}
138138
}
139139

@@ -251,7 +251,7 @@ function onlookupservice(err, hostname, service) {
251251

252252
this.callback(null, hostname, service);
253253
if (this[kPerfHooksDnsLookupServiceContext] && hasObserver('dns')) {
254-
stopPerf(this, kPerfHooksDnsLookupServiceContext);
254+
stopPerf(this, kPerfHooksDnsLookupServiceContext, { detail: { hostname, service } });
255255
}
256256
}
257257

@@ -304,7 +304,7 @@ function onresolve(err, result, ttls) {
304304
else {
305305
this.callback(null, result);
306306
if (this[kPerfHooksDnsLookupResolveContext] && hasObserver('dns')) {
307-
stopPerf(this, kPerfHooksDnsLookupResolveContext);
307+
stopPerf(this, kPerfHooksDnsLookupResolveContext, { detail: { result } });
308308
}
309309
}
310310
}

lib/internal/dns/promises.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ function onlookup(err, addresses) {
8989
const family = this.family || isIP(addresses[0]);
9090
this.resolve({ address: addresses[0], family });
9191
if (this[kPerfHooksDnsLookupContext] && hasObserver('dns')) {
92-
stopPerf(this, kPerfHooksDnsLookupContext);
92+
stopPerf(this, kPerfHooksDnsLookupContext, { detail: { addresses } });
9393
}
9494
}
9595

@@ -112,7 +112,7 @@ function onlookupall(err, addresses) {
112112

113113
this.resolve(addresses);
114114
if (this[kPerfHooksDnsLookupContext] && hasObserver('dns')) {
115-
stopPerf(this, kPerfHooksDnsLookupContext);
115+
stopPerf(this, kPerfHooksDnsLookupContext, { detail: { addresses } });
116116
}
117117
}
118118

@@ -205,7 +205,7 @@ function onlookupservice(err, hostname, service) {
205205

206206
this.resolve({ hostname, service });
207207
if (this[kPerfHooksDnsLookupServiceContext] && hasObserver('dns')) {
208-
stopPerf(this, kPerfHooksDnsLookupServiceContext);
208+
stopPerf(this, kPerfHooksDnsLookupServiceContext, { detail: { hostname, service } });
209209
}
210210
}
211211

@@ -261,7 +261,7 @@ function onresolve(err, result, ttls) {
261261

262262
this.resolve(result);
263263
if (this[kPerfHooksDnsLookupResolveContext] && hasObserver('dns')) {
264-
stopPerf(this, kPerfHooksDnsLookupResolveContext);
264+
stopPerf(this, kPerfHooksDnsLookupResolveContext, { detail: { result } });
265265
}
266266
}
267267

lib/net.js

+15-2
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,11 @@ const isWindows = process.platform === 'win32';
129129
const noop = () => {};
130130

131131
const kPerfHooksNetConnectContext = Symbol('kPerfHooksNetConnectContext');
132+
133+
const dc = require('diagnostics_channel');
134+
const netClientSocketChannel = dc.channel('net.client.socket');
135+
const netServerSocketChannel = dc.channel('net.server.socket');
136+
132137
const {
133138
hasObserver,
134139
startPerf,
@@ -200,7 +205,11 @@ function connect(...args) {
200205
const options = normalized[0];
201206
debug('createConnection', normalized);
202207
const socket = new Socket(options);
203-
208+
if (netClientSocketChannel.hasSubscribers) {
209+
netClientSocketChannel.publish({
210+
socket,
211+
});
212+
}
204213
if (options.timeout) {
205214
socket.setTimeout(options.timeout);
206215
}
@@ -1706,8 +1715,12 @@ function onconnection(err, clientHandle) {
17061715
self._connections++;
17071716
socket.server = self;
17081717
socket._server = self;
1709-
17101718
self.emit('connection', socket);
1719+
if (netServerSocketChannel.hasSubscribers) {
1720+
netServerSocketChannel.publish({
1721+
socket,
1722+
});
1723+
}
17111724
}
17121725

17131726
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const net = require('net');
5+
const dc = require('diagnostics_channel');
6+
7+
const netClientSocketChannel = dc.channel('net.client.socket');
8+
const netServerSocketChannel = dc.channel('net.server.socket');
9+
10+
const isNetSocket = (socket) => socket instanceof net.Socket;
11+
12+
netClientSocketChannel.subscribe(common.mustCall(({ socket }) => {
13+
assert.strictEqual(isNetSocket(socket), true);
14+
}));
15+
16+
netServerSocketChannel.subscribe(common.mustCall(({ socket }) => {
17+
assert.strictEqual(isNetSocket(socket), true);
18+
}));
19+
20+
const server = net.createServer(common.mustCall((socket) => {
21+
socket.destroy();
22+
server.close();
23+
}));
24+
25+
server.listen(() => {
26+
const { port } = server.address();
27+
net.connect(port);
28+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const dgram = require('dgram');
5+
const dc = require('diagnostics_channel');
6+
7+
const udpSocketChannel = dc.channel('udp.socket');
8+
9+
const isUDPSocket = (socket) => socket instanceof dgram.Socket;
10+
11+
udpSocketChannel.subscribe(common.mustCall(({ socket }) => {
12+
assert.strictEqual(isUDPSocket(socket), true);
13+
}));
14+
15+
const socket = dgram.createSocket('udp4');
16+
socket.close();

test/parallel/test-dns-perf_hooks.js

+27-1
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,40 @@ const obs = new PerformanceObserver(common.mustCallAtLeast((items) => {
1313
obs.observe({ type: 'dns' });
1414

1515
dns.lookup('localhost', () => {});
16+
dns.lookupService('127.0.0.1', 80, () => {});
17+
dns.resolveAny('localhost', () => {});
18+
19+
dns.promises.lookup('localhost');
20+
dns.promises.lookupService('127.0.0.1', 80);
21+
dns.promises.resolveAny('localhost');
1622

1723
process.on('exit', () => {
18-
assert.strictEqual(entries.length, 1);
24+
assert.strictEqual(entries.length, 6);
1925
entries.forEach((entry) => {
2026
assert.strictEqual(!!entry.name, true);
2127
assert.strictEqual(entry.entryType, 'dns');
2228
assert.strictEqual(typeof entry.startTime, 'number');
2329
assert.strictEqual(typeof entry.duration, 'number');
2430
assert.strictEqual(typeof entry.detail, 'object');
31+
switch (entry.name) {
32+
case 'lookup':
33+
assert.strictEqual(typeof entry.detail.hostname, 'string');
34+
assert.strictEqual(typeof entry.detail.family, 'number');
35+
assert.strictEqual(typeof entry.detail.hints, 'number');
36+
assert.strictEqual(typeof entry.detail.verbatim, 'boolean');
37+
assert.strictEqual(Array.isArray(entry.detail.addresses), true);
38+
break;
39+
case 'lookupService':
40+
assert.strictEqual(typeof entry.detail.host, 'string');
41+
assert.strictEqual(typeof entry.detail.port, 'number');
42+
assert.strictEqual(typeof entry.detail.hostname, 'string');
43+
assert.strictEqual(typeof entry.detail.service, 'string');
44+
break;
45+
case 'queryAny':
46+
assert.strictEqual(typeof entry.detail.host, 'string');
47+
assert.strictEqual(typeof entry.detail.ttl, 'boolean');
48+
assert.strictEqual(Array.isArray(entry.detail.result), true);
49+
break;
50+
}
2551
});
2652
});

0 commit comments

Comments
 (0)