diff --git a/lib/net.js b/lib/net.js index 66fc3578ca57fa..96aea96cbf9a24 100644 --- a/lib/net.js +++ b/lib/net.js @@ -134,6 +134,22 @@ const isWindows = process.platform === 'win32'; const noop = () => {}; const kPerfHooksNetConnectContext = Symbol('kPerfHooksNetConnectContext'); + +let netClientSocketChannel; +let netServerSocketChannel; +function lazyChannels() { + // TODO(joyeecheung): support diagnostics channels in the snapshot. + // For now it is fine to create them lazily when there isn't a snapshot to + // build. If users need the channels they would have to create them first + // before invoking any built-ins that would publish to these channels + // anyway. + if (netClientSocketChannel === undefined) { + const dc = require('diagnostics_channel'); + netClientSocketChannel = dc.channel('net.client.socket'); + netServerSocketChannel = dc.channel('net.server.socket'); + } +} + const { hasObserver, startPerf, @@ -205,7 +221,7 @@ function connect(...args) { const options = normalized[0]; debug('createConnection', normalized); const socket = new Socket(options); - + lazyChannels(); if (options.timeout) { socket.setTimeout(options.timeout); } @@ -1737,6 +1753,7 @@ function onconnection(err, clientHandle) { DTRACE_NET_SERVER_CONNECTION(socket); self.emit('connection', socket); + lazyChannels(); } /**