Source Code: lib/diagnostics_channel.js
The node:diagnostics_channel module provides an API to create named channels\nto report arbitrary message data for diagnostics purposes.
node:diagnostics_channel
It can be accessed using:
import diagnostics_channel from 'node:diagnostics_channel';\n
const diagnostics_channel = require('node:diagnostics_channel');\n
It is intended that a module writer wanting to report diagnostics messages\nwill create one or many top-level channels to report messages through.\nChannels may also be acquired at runtime but it is not encouraged\ndue to the additional overhead of doing so. Channels may be exported for\nconvenience, but as long as the name is known it can be acquired anywhere.
If you intend for your module to produce diagnostics data for others to\nconsume it is recommended that you include documentation of what named\nchannels are used along with the shape of the message data. Channel names\nshould generally include the module name to avoid collisions with data from\nother modules.
Following is a simple overview of the public API.
import diagnostics_channel from 'node:diagnostics_channel';\n\n// Get a reusable channel object\nconst channel = diagnostics_channel.channel('my-channel');\n\nfunction onMessage(message, name) {\n // Received data\n}\n\n// Subscribe to the channel\ndiagnostics_channel.subscribe('my-channel', onMessage);\n\n// Check if the channel has an active subscriber\nif (channel.hasSubscribers) {\n // Publish data to the channel\n channel.publish({\n some: 'data'\n });\n}\n\n// Unsubscribe from the channel\ndiagnostics_channel.unsubscribe('my-channel', onMessage);\n
const diagnostics_channel = require('node:diagnostics_channel');\n\n// Get a reusable channel object\nconst channel = diagnostics_channel.channel('my-channel');\n\nfunction onMessage(message, name) {\n // Received data\n}\n\n// Subscribe to the channel\ndiagnostics_channel.subscribe('my-channel', onMessage);\n\n// Check if the channel has an active subscriber\nif (channel.hasSubscribers) {\n // Publish data to the channel\n channel.publish({\n some: 'data'\n });\n}\n\n// Unsubscribe from the channel\ndiagnostics_channel.unsubscribe('my-channel', onMessage);\n
Check if there are active subscribers to the named channel. This is helpful if\nthe message you want to send might be expensive to prepare.
This API is optional but helpful when trying to publish messages from very\nperformance-sensitive code.
import diagnostics_channel from 'node:diagnostics_channel';\n\nif (diagnostics_channel.hasSubscribers('my-channel')) {\n // There are subscribers, prepare and publish message\n}\n
const diagnostics_channel = require('node:diagnostics_channel');\n\nif (diagnostics_channel.hasSubscribers('my-channel')) {\n // There are subscribers, prepare and publish message\n}\n
This is the primary entry-point for anyone wanting to publish to a named\nchannel. It produces a channel object which is optimized to reduce overhead at\npublish time as much as possible.
import diagnostics_channel from 'node:diagnostics_channel';\n\nconst channel = diagnostics_channel.channel('my-channel');\n
const diagnostics_channel = require('node:diagnostics_channel');\n\nconst channel = diagnostics_channel.channel('my-channel');\n
Register a message handler to subscribe to this channel. This message handler\nwill be run synchronously whenever a message is published to the channel. Any\nerrors thrown in the message handler will trigger an 'uncaughtException'.
'uncaughtException'
import diagnostics_channel from 'diagnostics_channel';\n\ndiagnostics_channel.subscribe('my-channel', (message, name) => {\n // Received data\n});\n
const diagnostics_channel = require('diagnostics_channel');\n\ndiagnostics_channel.subscribe('my-channel', (message, name) => {\n // Received data\n});\n
Remove a message handler previously registered to this channel with\ndiagnostics_channel.subscribe(name, onMessage).
diagnostics_channel.subscribe(name, onMessage)
import diagnostics_channel from 'diagnostics_channel';\n\nfunction onMessage(message, name) {\n // Received data\n}\n\ndiagnostics_channel.subscribe('my-channel', onMessage);\n\ndiagnostics_channel.unsubscribe('my-channel', onMessage);\n
const diagnostics_channel = require('diagnostics_channel');\n\nfunction onMessage(message, name) {\n // Received data\n}\n\ndiagnostics_channel.subscribe('my-channel', onMessage);\n\ndiagnostics_channel.unsubscribe('my-channel', onMessage);\n
http.client.request.start
request
Emitted when client starts a request.
http.client.response.finish
response
Emitted when client receives a response.
http.server.request.start
socket
server
Emitted when server receives a request.
http.server.response.finish
Emitted when server sends a response.
net.client.socket
Emitted when a new TCP or pipe client socket is created.
net.server.socket
Emitted when a new TCP or pipe connection is received.
udp.socket
Emitted when a new UDP socket is created.
child_process
process
Emitted when a new process is created.
worker_threads
worker
Worker
Emitted when a new thread is created.
The class Channel represents an individual named channel within the data\npipeline. It is used to track subscribers and to publish messages when there\nare subscribers present. It exists as a separate object to avoid channel\nlookups at publish time, enabling very fast publish speeds and allowing\nfor heavy use while incurring very minimal cost. Channels are created with\ndiagnostics_channel.channel(name), constructing a channel directly\nwith new Channel(name) is not supported.
Channel
diagnostics_channel.channel(name)
new Channel(name)
Check if there are active subscribers to this channel. This is helpful if\nthe message you want to send might be expensive to prepare.
import diagnostics_channel from 'node:diagnostics_channel';\n\nconst channel = diagnostics_channel.channel('my-channel');\n\nif (channel.hasSubscribers) {\n // There are subscribers, prepare and publish message\n}\n
const diagnostics_channel = require('node:diagnostics_channel');\n\nconst channel = diagnostics_channel.channel('my-channel');\n\nif (channel.hasSubscribers) {\n // There are subscribers, prepare and publish message\n}\n
Publish a message to any subscribers to the channel. This will trigger\nmessage handlers synchronously so they will execute within the same context.
import diagnostics_channel from 'node:diagnostics_channel';\n\nconst channel = diagnostics_channel.channel('my-channel');\n\nchannel.publish({\n some: 'message'\n});\n
const diagnostics_channel = require('node:diagnostics_channel');\n\nconst channel = diagnostics_channel.channel('my-channel');\n\nchannel.publish({\n some: 'message'\n});\n
import diagnostics_channel from 'node:diagnostics_channel';\n\nconst channel = diagnostics_channel.channel('my-channel');\n\nchannel.subscribe((message, name) => {\n // Received data\n});\n
const diagnostics_channel = require('node:diagnostics_channel');\n\nconst channel = diagnostics_channel.channel('my-channel');\n\nchannel.subscribe((message, name) => {\n // Received data\n});\n
Remove a message handler previously registered to this channel with\nchannel.subscribe(onMessage).
channel.subscribe(onMessage)
import diagnostics_channel from 'node:diagnostics_channel';\n\nconst channel = diagnostics_channel.channel('my-channel');\n\nfunction onMessage(message, name) {\n // Received data\n}\n\nchannel.subscribe(onMessage);\n\nchannel.unsubscribe(onMessage);\n
const diagnostics_channel = require('node:diagnostics_channel');\n\nconst channel = diagnostics_channel.channel('my-channel');\n\nfunction onMessage(message, name) {\n // Received data\n}\n\nchannel.subscribe(onMessage);\n\nchannel.unsubscribe(onMessage);\n