Skip to content

Commit 4028642

Browse files
committed
diagnostics_channel: fix diagnostics channel
1 parent 90c758c commit 4028642

File tree

7 files changed

+46
-40
lines changed

7 files changed

+46
-40
lines changed

lib/_http_client.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ const {
9393
const kClientRequestStatistics = Symbol('ClientRequestStatistics');
9494

9595
const dc = require('diagnostics_channel');
96-
const onClientRequestStartChannel = dc.channel('http.client.request.start');
97-
const onClientResponseFinishChannel = dc.channel('http.client.response.finish');
96+
const CLIENT_REQUEST_START_CHANNEL = 'http.client.request.start';
97+
const CLIENT_RESPONSE_FINISH_CHANNEL = 'http.client.response.finish';
9898

9999
const { addAbortSignal, finished } = require('stream');
100100

@@ -374,8 +374,8 @@ ClientRequest.prototype._finish = function _finish() {
374374
},
375375
});
376376
}
377-
if (onClientRequestStartChannel.hasSubscribers) {
378-
onClientRequestStartChannel.publish({
377+
if (dc.hasSubscribers(CLIENT_REQUEST_START_CHANNEL)) {
378+
dc.publish(CLIENT_REQUEST_START_CHANNEL, {
379379
request: this,
380380
});
381381
}
@@ -660,8 +660,8 @@ function parserOnIncomingClient(res, shouldKeepAlive) {
660660
},
661661
});
662662
}
663-
if (onClientResponseFinishChannel.hasSubscribers) {
664-
onClientResponseFinishChannel.publish({
663+
if (dc.hasSubscribers(CLIENT_RESPONSE_FINISH_CHANNEL)) {
664+
dc.publish(CLIENT_RESPONSE_FINISH_CHANNEL, {
665665
request: req,
666666
response: res,
667667
});

lib/_http_server.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ let debug = require('internal/util/debuglog').debuglog('http', (fn) => {
8989
});
9090

9191
const dc = require('diagnostics_channel');
92-
const onRequestStartChannel = dc.channel('http.server.request.start');
93-
const onResponseFinishChannel = dc.channel('http.server.response.finish');
92+
const SERVER_REQUEST_START_CHANNEL = 'http.server.request.start';
93+
const SERVER_RESPONSE_FINISH_CHANNEL = 'http.server.response.finish';
9494

9595
const kServerResponse = Symbol('ServerResponse');
9696
const kServerResponseStatistics = Symbol('ServerResponseStatistics');
@@ -870,8 +870,8 @@ function clearIncoming(req) {
870870
}
871871

872872
function resOnFinish(req, res, socket, state, server) {
873-
if (onResponseFinishChannel.hasSubscribers) {
874-
onResponseFinishChannel.publish({
873+
if (dc.hasSubscribers(SERVER_RESPONSE_FINISH_CHANNEL)) {
874+
dc.publish(SERVER_RESPONSE_FINISH_CHANNEL, {
875875
request: req,
876876
response: res,
877877
socket,
@@ -961,8 +961,8 @@ function parserOnIncoming(server, socket, state, req, keepAlive) {
961961
res.shouldKeepAlive = keepAlive;
962962
res[kUniqueHeaders] = server[kUniqueHeaders];
963963

964-
if (onRequestStartChannel.hasSubscribers) {
965-
onRequestStartChannel.publish({
964+
if (dc.hasSubscribers(SERVER_REQUEST_START_CHANNEL)) {
965+
dc.publish(SERVER_REQUEST_START_CHANNEL, {
966966
request: req,
967967
response: res,
968968
socket,

lib/dgram.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ const {
7575
} = internalBinding('udp_wrap');
7676

7777
const dc = require('diagnostics_channel');
78-
const udpSocketChannel = dc.channel('udp.socket');
78+
const UDP_SOCKET_CHANNEL = 'udp.socket';
7979

8080
const BIND_STATE_UNBOUND = 0;
8181
const BIND_STATE_BINDING = 1;
@@ -148,8 +148,8 @@ function Socket(type, listener) {
148148
this.once('close', () => signal.removeEventListener('abort', onAborted));
149149
}
150150
}
151-
if (udpSocketChannel.hasSubscribers) {
152-
udpSocketChannel.publish({
151+
if (dc.hasSubscribers(UDP_SOCKET_CHANNEL)) {
152+
dc.publish(UDP_SOCKET_CHANNEL, {
153153
socket: this,
154154
});
155155
}

lib/diagnostics_channel.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,10 +136,22 @@ function hasSubscribers(name) {
136136
return channel.hasSubscribers;
137137
}
138138

139+
function publish(name, data) {
140+
let channel;
141+
const ref = channels[name];
142+
if (ref) channel = ref.get();
143+
if (!channel) {
144+
return false;
145+
}
146+
channel.publish(data);
147+
return true;
148+
}
149+
139150
module.exports = {
140151
channel,
152+
Channel,
141153
hasSubscribers,
142154
subscribe,
143155
unsubscribe,
144-
Channel
156+
publish,
145157
};

lib/net.js

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -131,18 +131,17 @@ const noop = () => {};
131131

132132
const kPerfHooksNetConnectContext = Symbol('kPerfHooksNetConnectContext');
133133

134-
let netClientSocketChannel;
135-
let netServerSocketChannel;
134+
const CLIENT_SOCKET_CHANNEL = 'net.client.socket';
135+
const SERVER_SOCKET_CHANNEL = 'net.server.socket';
136+
let dc;
136137
function lazyChannels() {
137138
// TODO(joyeecheung): support diagnostics channels in the snapshot.
138139
// For now it is fine to create them lazily when there isn't a snapshot to
139140
// build. If users need the channels they would have to create them first
140141
// before invoking any built-ins that would publish to these channels
141142
// anyway.
142-
if (netClientSocketChannel === undefined) {
143-
const dc = require('diagnostics_channel');
144-
netClientSocketChannel = dc.channel('net.client.socket');
145-
netServerSocketChannel = dc.channel('net.server.socket');
143+
if (dc === undefined) {
144+
dc = require('diagnostics_channel');
146145
}
147146
}
148147

@@ -218,8 +217,8 @@ function connect(...args) {
218217
debug('createConnection', normalized);
219218
const socket = new Socket(options);
220219
lazyChannels();
221-
if (netClientSocketChannel.hasSubscribers) {
222-
netClientSocketChannel.publish({
220+
if (dc.hasSubscribers(CLIENT_SOCKET_CHANNEL)) {
221+
dc.publish(CLIENT_SOCKET_CHANNEL, {
223222
socket,
224223
});
225224
}
@@ -1762,8 +1761,8 @@ function onconnection(err, clientHandle) {
17621761
socket._server = self;
17631762
self.emit('connection', socket);
17641763
lazyChannels();
1765-
if (netServerSocketChannel.hasSubscribers) {
1766-
netServerSocketChannel.publish({
1764+
if (dc.hasSubscribers(SERVER_SOCKET_CHANNEL)) {
1765+
dc.publish(SERVER_SOCKET_CHANNEL, {
17671766
socket,
17681767
});
17691768
}

test/parallel/test-diagnostics-channel-http-server-start.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,18 @@ const dc = require('diagnostics_channel');
66
const assert = require('assert');
77
const http = require('http');
88

9-
const incomingStartChannel = dc.channel('http.server.request.start');
10-
const outgoingFinishChannel = dc.channel('http.server.response.finish');
11-
129
const als = new AsyncLocalStorage();
1310
let context;
1411

1512
// Bind requests to an AsyncLocalStorage context
16-
incomingStartChannel.subscribe(common.mustCall((message) => {
13+
dc.subscribe('http.server.request.start', common.mustCall((message) => {
1714
als.enterWith(message);
1815
context = message;
1916
}));
2017

2118
// When the request ends, verify the context has been maintained
2219
// and that the messages contain the expected data
23-
outgoingFinishChannel.subscribe(common.mustCall((message) => {
20+
dc.subscribe('http.server.response.finish', common.mustCall((message) => {
2421
const data = {
2522
request,
2623
response,

test/parallel/test-diagnostics-channel-http.js

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,24 @@ const http = require('http');
55
const net = require('net');
66
const dc = require('diagnostics_channel');
77

8-
const onClientRequestStart = dc.channel('http.client.request.start');
9-
const onClientResponseFinish = dc.channel('http.client.response.finish');
10-
const onServerRequestStart = dc.channel('http.server.request.start');
11-
const onServerResponseFinish = dc.channel('http.server.response.finish');
12-
138
const isHTTPServer = (server) => server instanceof http.Server;
149
const isIncomingMessage = (object) => object instanceof http.IncomingMessage;
1510
const isOutgoingMessage = (object) => object instanceof http.OutgoingMessage;
1611
const isNetSocket = (socket) => socket instanceof net.Socket;
1712

18-
onClientRequestStart.subscribe(common.mustCall(({ request }) => {
13+
dc.subscribe('http.client.request.start', common.mustCall(({ request }) => {
1914
assert.strictEqual(isOutgoingMessage(request), true);
2015
}));
2116

22-
onClientResponseFinish.subscribe(common.mustCall(({ request, response }) => {
17+
dc.subscribe('http.client.response.finish', common.mustCall(({
18+
request,
19+
response
20+
}) => {
2321
assert.strictEqual(isOutgoingMessage(request), true);
2422
assert.strictEqual(isIncomingMessage(response), true);
2523
}));
2624

27-
onServerRequestStart.subscribe(common.mustCall(({
25+
dc.subscribe('http.server.request.start', common.mustCall(({
2826
request,
2927
response,
3028
socket,
@@ -36,7 +34,7 @@ onServerRequestStart.subscribe(common.mustCall(({
3634
assert.strictEqual(isHTTPServer(server), true);
3735
}));
3836

39-
onServerResponseFinish.subscribe(common.mustCall(({
37+
dc.subscribe('http.server.response.finish', common.mustCall(({
4038
request,
4139
response,
4240
socket,

0 commit comments

Comments
 (0)