{ "type": "module", "source": "doc/api/diagnostics_channel.md", "modules": [ { "textRaw": "Diagnostics Channel", "name": "diagnostics_channel", "meta": { "added": [ "v15.1.0", "v14.17.0" ], "changes": [ { "version": "v18.13.0", "pr-url": "https://github.com/nodejs/node/pull/45290", "description": "diagnostics_channel is now Stable." } ] }, "introduced_in": "v15.1.0", "stability": 2, "stabilityText": "Stable", "desc": "
Source Code: lib/diagnostics_channel.js
\nThe node:diagnostics_channel module provides an API to create named channels\nto report arbitrary message data for diagnostics purposes.
It can be accessed using:
\nimport diagnostics_channel from 'node:diagnostics_channel';\n\nconst diagnostics_channel = require('node:diagnostics_channel');\n\nIt 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.
\nIf 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.
", "modules": [ { "textRaw": "Public API", "name": "public_api", "modules": [ { "textRaw": "Overview", "name": "overview", "desc": "Following is a simple overview of the public API.
\nimport 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\nconst 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",
"methods": [
{
"textRaw": "`diagnostics_channel.hasSubscribers(name)`",
"type": "method",
"name": "hasSubscribers",
"meta": {
"added": [
"v15.1.0",
"v14.17.0"
],
"changes": []
},
"signatures": [
{
"return": {
"textRaw": "Returns: {boolean} If there are active subscribers",
"name": "return",
"type": "boolean",
"desc": "If there are active subscribers"
},
"params": [
{
"textRaw": "`name` {string|symbol} The channel name",
"name": "name",
"type": "string|symbol",
"desc": "The channel name"
}
]
}
],
"desc": "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.
\nThis API is optional but helpful when trying to publish messages from very\nperformance-sensitive code.
\nimport diagnostics_channel from 'node:diagnostics_channel';\n\nif (diagnostics_channel.hasSubscribers('my-channel')) {\n // There are subscribers, prepare and publish message\n}\n\nconst diagnostics_channel = require('node:diagnostics_channel');\n\nif (diagnostics_channel.hasSubscribers('my-channel')) {\n // There are subscribers, prepare and publish message\n}\n"
},
{
"textRaw": "`diagnostics_channel.channel(name)`",
"type": "method",
"name": "channel",
"meta": {
"added": [
"v15.1.0",
"v14.17.0"
],
"changes": []
},
"signatures": [
{
"return": {
"textRaw": "Returns: {Channel} The named channel object",
"name": "return",
"type": "Channel",
"desc": "The named channel object"
},
"params": [
{
"textRaw": "`name` {string|symbol} The channel name",
"name": "name",
"type": "string|symbol",
"desc": "The channel name"
}
]
}
],
"desc": "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.
\nimport diagnostics_channel from 'node:diagnostics_channel';\n\nconst channel = diagnostics_channel.channel('my-channel');\n\nconst diagnostics_channel = require('node:diagnostics_channel');\n\nconst channel = diagnostics_channel.channel('my-channel');\n"
},
{
"textRaw": "`diagnostics_channel.subscribe(name, onMessage)`",
"type": "method",
"name": "subscribe",
"meta": {
"added": [
"v18.7.0"
],
"changes": []
},
"signatures": [
{
"params": [
{
"textRaw": "`name` {string|symbol} The channel name",
"name": "name",
"type": "string|symbol",
"desc": "The channel name"
},
{
"textRaw": "`onMessage` {Function} The handler to receive channel messages",
"name": "onMessage",
"type": "Function",
"desc": "The handler to receive channel messages",
"options": [
{
"textRaw": "`message` {any} The message data",
"name": "message",
"type": "any",
"desc": "The message data"
},
{
"textRaw": "`name` {string|symbol} The name of the channel",
"name": "name",
"type": "string|symbol",
"desc": "The name of the channel"
}
]
}
]
}
],
"desc": "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'.
import diagnostics_channel from 'node:diagnostics_channel';\n\ndiagnostics_channel.subscribe('my-channel', (message, name) => {\n // Received data\n});\n\nconst diagnostics_channel = require('node:diagnostics_channel');\n\ndiagnostics_channel.subscribe('my-channel', (message, name) => {\n // Received data\n});\n"
},
{
"textRaw": "`diagnostics_channel.unsubscribe(name, onMessage)`",
"type": "method",
"name": "unsubscribe",
"meta": {
"added": [
"v18.7.0"
],
"changes": []
},
"signatures": [
{
"return": {
"textRaw": "Returns: {boolean} `true` if the handler was found, `false` otherwise.",
"name": "return",
"type": "boolean",
"desc": "`true` if the handler was found, `false` otherwise."
},
"params": [
{
"textRaw": "`name` {string|symbol} The channel name",
"name": "name",
"type": "string|symbol",
"desc": "The channel name"
},
{
"textRaw": "`onMessage` {Function} The previous subscribed handler to remove",
"name": "onMessage",
"type": "Function",
"desc": "The previous subscribed handler to remove"
}
]
}
],
"desc": "Remove a message handler previously registered to this channel with\ndiagnostics_channel.subscribe(name, onMessage).
import diagnostics_channel from 'node: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\nconst diagnostics_channel = require('node: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"
}
],
"type": "module",
"displayName": "Overview"
},
{
"textRaw": "Built-in Channels",
"name": "built-in_channels",
"stability": 1,
"stabilityText": "Experimental",
"desc": "While the diagnostics_channel API is now considered stable, the built-in\nchannels currently available are not. Each channel must be declared stable\nindependently.
", "modules": [ { "textRaw": "HTTP", "name": "http", "desc": "http.client.request.start
request <http.ClientRequest>Emitted when client starts a request.
\nhttp.client.response.finish
request <http.ClientRequest>response <http.IncomingMessage>Emitted when client receives a response.
\nhttp.server.request.start
request <http.IncomingMessage>response <http.ServerResponse>socket <net.Socket>server <http.Server>Emitted when server receives a request.
\nhttp.server.response.finish
request <http.IncomingMessage>response <http.ServerResponse>socket <net.Socket>server <http.Server>Emitted when server sends a response.
\nnet.client.socket
socket <net.Socket>Emitted when a new TCP or pipe client socket is created.
\nnet.server.socket
socket <net.Socket>Emitted when a new TCP or pipe connection is received.
\nudp.socket
socket <dgram.Socket>Emitted when a new UDP socket is created.
", "type": "module", "displayName": "HTTP" } ], "type": "module", "displayName": "Built-in Channels" } ], "classes": [ { "textRaw": "Class: `Channel`", "type": "class", "name": "Channel", "meta": { "added": [ "v15.1.0", "v14.17.0" ], "changes": [] }, "desc": "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.
Check if there are active subscribers to this channel. This is helpful if\nthe message you want to send might be expensive to prepare.
\nThis API is optional but helpful when trying to publish messages from very\nperformance-sensitive code.
\nimport 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\nconst 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",
"shortDesc": "If there are active subscribers"
}
],
"methods": [
{
"textRaw": "`channel.publish(message)`",
"type": "method",
"name": "publish",
"meta": {
"added": [
"v15.1.0",
"v14.17.0"
],
"changes": []
},
"signatures": [
{
"params": [
{
"textRaw": "`message` {any} The message to send to the channel subscribers",
"name": "message",
"type": "any",
"desc": "The message to send to the channel subscribers"
}
]
}
],
"desc": "Publish a message to any subscribers to the channel. This will trigger\nmessage handlers synchronously so they will execute within the same context.
\nimport diagnostics_channel from 'node:diagnostics_channel';\n\nconst channel = diagnostics_channel.channel('my-channel');\n\nchannel.publish({\n some: 'message',\n});\n\nconst diagnostics_channel = require('node:diagnostics_channel');\n\nconst channel = diagnostics_channel.channel('my-channel');\n\nchannel.publish({\n some: 'message',\n});\n"
},
{
"textRaw": "`channel.subscribe(onMessage)`",
"type": "method",
"name": "subscribe",
"meta": {
"added": [
"v15.1.0",
"v14.17.0"
],
"deprecated": [
"v18.7.0"
],
"changes": []
},
"stability": 0,
"stabilityText": "Deprecated: Use [`diagnostics_channel.subscribe(name, onMessage)`][]",
"signatures": [
{
"params": [
{
"textRaw": "`onMessage` {Function} The handler to receive channel messages",
"name": "onMessage",
"type": "Function",
"desc": "The handler to receive channel messages",
"options": [
{
"textRaw": "`message` {any} The message data",
"name": "message",
"type": "any",
"desc": "The message data"
},
{
"textRaw": "`name` {string|symbol} The name of the channel",
"name": "name",
"type": "string|symbol",
"desc": "The name of the channel"
}
]
}
]
}
],
"desc": "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'.
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\nconst diagnostics_channel = require('node:diagnostics_channel');\n\nconst channel = diagnostics_channel.channel('my-channel');\n\nchannel.subscribe((message, name) => {\n // Received data\n});\n"
},
{
"textRaw": "`channel.unsubscribe(onMessage)`",
"type": "method",
"name": "unsubscribe",
"meta": {
"added": [
"v15.1.0",
"v14.17.0"
],
"deprecated": [
"v18.7.0"
],
"changes": [
{
"version": [
"v17.1.0",
"v16.14.0",
"v14.19.0"
],
"pr-url": "https://github.com/nodejs/node/pull/40433",
"description": "Added return value. Added to channels without subscribers."
}
]
},
"stability": 0,
"stabilityText": "Deprecated: Use [`diagnostics_channel.unsubscribe(name, onMessage)`][]",
"signatures": [
{
"return": {
"textRaw": "Returns: {boolean} `true` if the handler was found, `false` otherwise.",
"name": "return",
"type": "boolean",
"desc": "`true` if the handler was found, `false` otherwise."
},
"params": [
{
"textRaw": "`onMessage` {Function} The previous subscribed handler to remove",
"name": "onMessage",
"type": "Function",
"desc": "The previous subscribed handler to remove"
}
]
}
],
"desc": "Remove a message handler previously registered to this channel with\nchannel.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\nconst 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"
}
]
}
],
"type": "module",
"displayName": "Public API"
}
],
"type": "module",
"displayName": "Diagnostics Channel"
}
]
}