Diagnostics Channel | Node.js v26.8.2 Documentation
https://nodejs.org/api/diagnostics_channel.html • 324 KB fetched
Open original page
Diagnostics Channel | Node.js v26.8.2 Documentation Skip to content
Node.js
* About this documentation
* Usage and example
* Assertion testing
* Asynchronous context tracking
* Async hooks
* Buffer
* C++ addons
* C/C++ addons with Node-API
* C++ embedder API
* Child processes
* Cluster
* Command-line options
* Console
* Crypto
* Debugger
* Deprecated APIs
* Diagnostics Channel
* DNS
* Domain
* Environment Variables
* Errors
* Events
* File system
* FFI
* Globals
* HTTP
* HTTP/2
* HTTPS
* Inspector
* Internationalization
* Iterable Streams API
* Modules: CommonJS modules
* Modules: ECMAScript modules
* Modules: node:module API
* Modules: Packages
* Modules: TypeScript
* Net
* OS
* Path
* Performance hooks
* Permissions
* Process
* Punycode
* Query strings
* Readline
* REPL
* Report
* Single executable applications
* SQLite
* Stream
* String decoder
* Test runner
* Timers
* TLS/SSL
* Trace events
* TTY
* UDP/datagram
* URL
* Utilities
* V8
* Virtual File System
* VM
* WASI
* Web Crypto API
* Web Streams API
* Worker threads
* Zlib
*
Code repository and issue tracker
Node.js v26.8.2 documentation
* Node.js v26.8.2
* Table of contents
* Diagnostics Channel
* Public API
* Overview
* diagnostics_channel.hasSubscribers(name)
* diagnostics_channel.channel(name)
* diagnostics_channel.subscribe(name, onMessage)
* diagnostics_channel.unsubscribe(name, onMessage)
* diagnostics_channel.tracingChannel(nameOrChannels)
* diagnostics_channel.boundedChannel(nameOrChannels)
* Class: Channel
* channel.hasSubscribers
* channel.publish(message)
* channel.subscribe(onMessage)
* channel.unsubscribe(onMessage)
* channel.bindStore(store[, transform])
* channel.unbindStore(store)
* channel.runStores(context, fn[, thisArg[, ...args]])
* channel.withStoreScope(data)
* Class: RunStoresScope
* Class: TracingChannel
* tracingChannel.subscribe(subscribers)
* tracingChannel.unsubscribe(subscribers)
* tracingChannel.traceSync(fn[, context[, thisArg[, ...args]]])
* tracingChannel.tracePromise(fn[, context[, thisArg[, ...args]]])
* tracingChannel.traceCallback(fn[, position[, context[, thisArg[, ...args]]]])
* tracingChannel.hasSubscribers
* Class: BoundedChannel
* boundedChannel.hasSubscribers
* boundedChannel.subscribe(handlers)
* boundedChannel.unsubscribe(handlers)
* boundedChannel.run(context, fn[, thisArg[, ...args]])
* boundedChannel.withScope([context])
* Class: BoundedChannelScope
* BoundedChannel Channels
* TracingChannel Channels
* start(event)
* end(event)
* asyncStart(event)
* asyncEnd(event)
* error(event)
* Built-in Channels
* Console
* Event: 'console.log'
* Event: 'console.info'
* Event: 'console.debug'
* Event: 'console.warn'
* Event: 'console.error'
* HTTP
* Event: 'http.client.request.created'
* Event: 'http.client.request.start'
* Event: 'http.client.request.error'
* Event: 'http.client.response.finish'
* Event: 'http.server.request.start'
* Event: 'http.server.response.created'
* Event: 'http.server.response.finish'
* HTTP/2
* Event: 'http2.client.stream.created'
* Event: 'http2.client.stream.start'
* Event: 'http2.client.stream.error'
* Event: 'http2.client.stream.finish'
* Event: 'http2.client.stream.bodyChunkSent'
* Event: 'http2.client.stream.bodySent'
* Event: 'http2.client.stream.close'
* Event: 'http2.server.stream.created'
* Event: 'http2.server.stream.start'
* Event: 'http2.server.stream.error'
* Event: 'http2.server.stream.finish'
* Event: 'http2.server.stream.close'
* Modules
* Event: 'tracing:module.require:start'
* Event: 'tracing:module.require:end'
* Event: 'tracing:module.require:error'
* Event: 'tracing:module.import:asyncStart'
* Event: 'tracing:module.import:asyncEnd'
* Event: 'tracing:module.import:error'
* NET
* Event: 'net.client.socket'
* Event: 'net.server.socket'
* Event: 'tracing:net.server.listen:asyncStart'
* Event: 'tracing:net.server.listen:asyncEnd'
* Event: 'tracing:net.server.listen:error'
* UDP
* Event: 'udp.socket'
* Process
* Event: 'child_process'
* Event: 'process.execve'
* Web Locks
* Event: 'locks.request.start'
* Event: 'locks.request.grant'
* Event: 'locks.request.miss'
* Event: 'locks.request.end'
* Worker Thread
* Event: 'worker_threads'
* SQLite
* Event: 'sqlite.db.query'
* Index
* Index
* About this documentation
* Usage and example
* Assertion testing
* Asynchronous context tracking
* Async hooks
* Buffer
* C++ addons
* C/C++ addons with Node-API
* C++ embedder API
* Child processes
* Cluster
* Command-line options
* Console
* Crypto
* Debugger
* Deprecated APIs
* Diagnostics Channel
* DNS
* Domain
* Environment Variables
* Errors
* Events
* File system
* FFI
* Globals
* HTTP
* HTTP/2
* HTTPS
* Inspector
* Internationalization
* Iterable Streams API
* Modules: CommonJS modules
* Modules: ECMAScript modules
* Modules: node:module API
* Modules: Packages
* Modules: TypeScript
* Net
* OS
* Path
* Performance hooks
* Permissions
* Process
* Punycode
* Query strings
* Readline
* REPL
* Report
* Single executable applications
* SQLite
* Stream
* String decoder
* Test runner
* Timers
* TLS/SSL
* Trace events
* TTY
* UDP/datagram
* URL
* Utilities
* V8
* Virtual File System
* VM
* WASI
* Web Crypto API
* Web Streams API
* Worker threads
* Zlib
* Other versions
* 26.x
* 25.x
* 24.x LTS
* 23.x
* 22.x LTS
* 21.x
* 20.x
* 19.x
* 18.x
* 17.x
* 16.x
* 15.x
*
Options
*
View on single page
*
View as JSON
* Edit on GitHub
Table of contents
* Diagnostics Channel
* Public API
* Overview
* diagnostics_channel.hasSubscribers(name)
* diagnostics_channel.channel(name)
* diagnostics_channel.subscribe(name, onMessage)
* diagnostics_channel.unsubscribe(name, onMessage)
* diagnostics_channel.tracingChannel(nameOrChannels)
* diagnostics_channel.boundedChannel(nameOrChannels)
* Class: Channel
* channel.hasSubscribers
* channel.publish(message)
* channel.subscribe(onMessage)
* channel.unsubscribe(onMessage)
* channel.bindStore(store[, transform])
* channel.unbindStore(store)
* channel.runStores(context, fn[, thisArg[, ...args]])
* channel.withStoreScope(data)
* Class: RunStoresScope
* Class: TracingChannel
* tracingChannel.subscribe(subscribers)
* tracingChannel.unsubscribe(subscribers)
* tracingChannel.traceSync(fn[, context[, thisArg[, ...args]]])
* tracingChannel.tracePromise(fn[, context[, thisArg[, ...args]]])
* tracingChannel.traceCallback(fn[, position[, context[, thisArg[, ...args]]]])
* tracingChannel.hasSubscribers
* Class: BoundedChannel
* boundedChannel.hasSubscribers
* boundedChannel.subscribe(handlers)
* boundedChannel.unsubscribe(handlers)
* boundedChannel.run(context, fn[, thisArg[, ...args]])
* boundedChannel.withScope([context])
* Class: BoundedChannelScope
* BoundedChannel Channels
* TracingChannel Channels
* start(event)
* end(event)
* asyncStart(event)
* asyncEnd(event)
* error(event)
* Built-in Channels
* Console
* Event: 'console.log'
* Event: 'console.info'
* Event: 'console.debug'
* Event: 'console.warn'
* Event: 'console.error'
* HTTP
* Event: 'http.client.request.created'
* Event: 'http.client.request.start'
* Event: 'http.client.request.error'
* Event: 'http.client.response.finish'
* Event: 'http.server.request.start'
* Event: 'http.server.response.created'
* Event: 'http.server.response.finish'
* HTTP/2
* Event: 'http2.client.stream.created'
* Event: 'http2.client.stream.start'
* Event: 'http2.client.stream.error'
* Event: 'http2.client.stream.finish'
* Event: 'http2.client.stream.bodyChunkSent'
* Event: 'http2.client.stream.bodySent'
* Event: 'http2.client.stream.close'
* Event: 'http2.server.stream.created'
* Event: 'http2.server.stream.start'
* Event: 'http2.server.stream.error'
* Event: 'http2.server.stream.finish'
* Event: 'http2.server.stream.close'
* Modules
* Event: 'tracing:module.require:start'
* Event: 'tracing:module.require:end'
* Event: 'tracing:module.require:error'
* Event: 'tracing:module.import:asyncStart'
* Event: 'tracing:module.import:asyncEnd'
* Event: 'tracing:module.import:error'
* NET
* Event: 'net.client.socket'
* Event: 'net.server.socket'
* Event: 'tracing:net.server.listen:asyncStart'
* Event: 'tracing:net.server.listen:asyncEnd'
* Event: 'tracing:net.server.listen:error'
* UDP
* Event: 'udp.socket'
* Process
* Event: 'child_process'
* Event: 'process.execve'
* Web Locks
* Event: 'locks.request.start'
* Event: 'locks.request.grant'
* Event: 'locks.request.miss'
* Event: 'locks.request.end'
* Worker Thread
* Event: 'worker_threads'
* SQLite
* Event: 'sqlite.db.query'
Diagnostics Channel #
Source Code: lib/diagnostics_channel.js Added in: v15.1.0, v14.17.0 History Version Changes v19.2.0, v18.13.0 diagnostics_channel is now Stable.
Stability: 2 - Stable
The node:diagnostics_channel module provides an API to create named channels
to report arbitrary message data for diagnostics purposes. It can be accessed using: import diagnostics_channel from 'node:diagnostics_channel' ;
const diagnostics_channel = require ( 'node:diagnostics_channel' ) ;
javascript copy
It is intended that a module writer wanting to report diagnostics messages
will create one or many top-level channels to report messages through.
Channels may also be acquired at runtime but it is not encouraged
due to the additional overhead of doing so. Channels may be exported for
convenience, 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
consume it is recommended that you include documentation of what named
channels are used along with the shape of the message data. Channel names
should generally include the module name to avoid collisions with data from
other modules.
Public API #
Overview #
Following is a simple overview of the public API. import diagnostics_channel from 'node:diagnostics_channel' ;
// Get a reusable channel object
const channel = diagnostics_channel . channel ( 'my-channel' ) ;
function onMessage ( message , name ) {
// Received data
}
// Subscribe to the channel
diagnostics_channel . subscribe ( 'my-channel' , onMessage) ;
// Check if the channel has an active subscriber
if (channel . hasSubscribers) {
// Publish data to the channel
channel . publish ( {
some : 'data' ,
} ) ;
}
// Unsubscribe from the channel
diagnostics_channel . unsubscribe ( 'my-channel' , onMessage) ;
const diagnostics_channel = require ( 'node:diagnostics_channel' ) ;
// Get a reusable channel object
const channel = diagnostics_channel . channel ( 'my-channel' ) ;
function onMessage ( message , name ) {
// Received data
}
// Subscribe to the channel
diagnostics_channel . subscribe ( 'my-channel' , onMessage) ;
// Check if the channel has an active subscriber
if (channel . hasSubscribers) {
// Publish data to the channel
channel . publish ( {
some : 'data' ,
} ) ;
}
// Unsubscribe from the channel
diagnostics_channel . unsubscribe ( 'my-channel' , onMessage) ;
javascript copy
diagnostics_channel.hasSubscribers(name) #
Added in: v15.1.0, v14.17.0
* name <string> | <symbol> The channel name
* Returns: <boolean> If there are active subscribers
Check if there are active subscribers to the named channel. This is helpful if
the message you want to send might be expensive to prepare. This API is optional but helpful when trying to publish messages from very
performance-sensitive code. import diagnostics_channel from 'node:diagnostics_channel' ;
if (diagnostics_channel . hasSubscribers ( 'my-channel' )) {
// There are subscribers, prepare and publish message
}
const diagnostics_channel = require ( 'node:diagnostics_channel' ) ;
if (diagnostics_channel . hasSubscribers ( 'my-channel' )) {
// There are subscribers, prepare and publish message
}
javascript copy
diagnostics_channel.channel(name) #
Added in: v15.1.0, v14.17.0
* name <string> | <symbol> The channel name
* Returns: <Channel> The named channel object
This is the primary entry-point for anyone wanting to publish to a named
channel. It produces a channel object which is optimized to reduce overhead at
publish time as much as possible. import diagnostics_channel from 'node:diagnostics_channel' ;
const channel = diagnostics_channel . channel ( 'my-channel' ) ;
const diagnostics_channel = require ( 'node:diagnostics_channel' ) ;
const channel = diagnostics_channel . channel ( 'my-channel' ) ;
javascript copy
diagnostics_channel.subscribe(name, onMessage) #
Added in: v18.7.0, v16.17.0
* name <string> | <symbol> The channel name
* onMessage <Function> The handler to receive channel messages
* message <any> The message data
* name <string> | <symbol> The name of the channel
Register a message handler to subscribe to this channel. This message handler
will be run synchronously whenever a message is published to the channel. Any
errors thrown in the message handler will trigger an 'uncaughtException' . import diagnostics_channel from 'node:diagnostics_channel' ;
diagnostics_channel . subscribe ( 'my-channel' , ( message , name ) => {
// Received data
} ) ;
const diagnostics_channel = require ( 'node:diagnostics_channel' ) ;
diagnostics_channel . subscribe ( 'my-channel' , ( message , name ) => {
// Received data
} ) ;
javascript copy
diagnostics_channel.unsubscribe(name, onMessage) #
Added in: v18.7.0, v16.17.0
* name <string> | <symbol> The channel name
* onMessage <Function> The previous subscribed handler to remove
* Returns: <boolean> true if the handler was found, false otherwise.
Remove a message handler previously registered to this channel with
diagnostics_channel.subscribe(name, onMessage) . import diagnostics_channel from 'node:diagnostics_channel' ;
function onMessage ( message , name ) {
// Received data
}
diagnostics_channel . subscribe ( 'my-channel' , onMessage) ;
diagnostics_channel . unsubscribe ( 'my-channel' , onMessage) ;
const diagnostics_channel = require ( 'node:diagnostics_channel' ) ;
function onMessage ( message , name ) {
// Received data
}
diagnostics_channel . subscribe ( 'my-channel' , onMessage) ;
diagnostics_channel . unsubscribe ( 'my-channel' , onMessage) ;
javascript copy
diagnostics_channel.tracingChannel(nameOrChannels) #
Added in: v19.9.0, v18.19.0 History Version Changes v26.8.0 Marked as stable.
Stability: 2 - Stable
* nameOrChannels <string> | <TracingChannel> Channel name or
object containing all the TracingChannel Channels
* Returns: <TracingChannel> Collection of channels to trace with
Creates a TracingChannel wrapper for the given
TracingChannel Channels . If a name is given, the corresponding tracing
channels will be created in the form of tracing:${name}:${eventType} where
eventType corresponds to the types of TracingChannel Channels . import diagnostics_channel from 'node:diagnostics_channel' ;
const channelsByName = diagnostics_channel . tracingChannel ( 'my-channel' ) ;
// or...
const channelsByCollection = diagnostics_channel . tracingChannel ( {
start : diagnostics_channel . channel ( 'tracing:my-channel:start' ) ,
end : diagnostics_channel . channel ( 'tracing:my-channel:end' ) ,
asyncStart : diagnostics_channel . channel ( 'tracing:my-channel:asyncStart' ) ,
asyncEnd : diagnostics_channel . channel ( 'tracing:my-channel:asyncEnd' ) ,
error : diagnostics_channel . channel ( 'tracing:my-channel:error' ) ,
} ) ;
const diagnostics_channel = require ( 'node:diagnostics_channel' ) ;
const channelsByName = diagnostics_channel . tracingChannel ( 'my-channel' ) ;
// or...
const channelsByCollection = diagnostics_channel . tracingChannel ( {
start : diagnostics_channel . channel ( 'tracing:my-channel:start' ) ,
end : diagnostics_channel . channel ( 'tracing:my-channel:end' ) ,
asyncStart : diagnostics_channel . channel ( 'tracing:my-channel:asyncStart' ) ,
asyncEnd : diagnostics_channel . channel ( 'tracing:my-channel:asyncEnd' ) ,
error : diagnostics_channel . channel ( 'tracing:my-channel:error' ) ,
} ) ;
javascript copy
diagnostics_channel.boundedChannel(nameOrChannels) #
Added in: v26.1.0
Stability: 1 - Experimental
* nameOrChannels <string> | <BoundedChannel> Channel name or
object containing all the BoundedChannel Channels
* Returns: <BoundedChannel> Collection of channels to trace with
Creates a BoundedChannel wrapper for the given channels. If a name is
given, the corresponding channels will be created in the form of
tracing:${name}:${eventType} where eventType is start or end . A BoundedChannel is a simplified version of TracingChannel that only
traces synchronous operations. It only has start and end events, without
asyncStart , asyncEnd , or error events, making it suitable for tracing
operations that don't involve asynchronous continuations or error handling. import { boundedChannel , channel } from 'node:diagnostics_channel' ;
const wc = boundedChannel ( 'my-operation' ) ;
// or...
const wc2 = boundedChannel ( {
start : channel ( 'tracing:my-operation:start' ) ,
end : channel ( 'tracing:my-operation:end' ) ,
} ) ;
const { boundedChannel , channel } = require ( 'node:diagnostics_channel' ) ;
const wc = boundedChannel ( 'my-operation' ) ;
// or...
const wc2 = boundedChannel ( {
start : channel ( 'tracing:my-operation:start' ) ,
end : channel ( 'tracing:my-operation:end' ) ,
} ) ;
javascript copy
Class: Channel #
Added in: v15.1.0, v14.17.0
The class Channel represents an individual named channel within the data
pipeline. It is used to track subscribers and to publish messages when there
are subscribers present. It exists as a separate object to avoid channel
lookups at publish time, enabling very fast publish speeds and allowing
for heavy use while incurring very minimal cost. Channels are created with
diagnostics_channel.channel(name) , constructing a channel directly
with new Channel(name) is not supported.
channel.hasSubscribers #
Added in: v
Links found on this page
- Skip to content [direct]
- Node.js [direct]
- About this documentation [direct]
- Usage and example [direct]
- Assertion testing [direct]
- Asynchronous context tracking [direct]
- Async hooks [direct]
- Buffer [direct]
- C++ addons [direct]
- C/C++ addons with Node-API [direct]
- C++ embedder API [direct]
- Child processes [direct]
- Cluster [direct]
- Command-line options [direct]
- Console [direct]
- Crypto [direct]
- Debugger [direct]
- Deprecated APIs [direct]
- DNS [direct]
- Domain [direct]
- Environment Variables [direct]
- Errors [direct]
- Events [direct]
- File system [direct]
- FFI [direct]
- Globals [direct]
- HTTP [direct]
- HTTP/2 [direct]
- HTTPS [direct]
- Inspector [direct]
- Internationalization [direct]
- Iterable Streams API [direct]
- Modules: CommonJS modules [direct]
- Modules: ECMAScript modules [direct]
- Modules: node:module API [direct]
- Modules: Packages [direct]
- Modules: TypeScript [direct]
- Net [direct]
- OS [direct]
- Path [direct]
- Performance hooks [direct]
- Permissions [direct]
- Process [direct]
- Punycode [direct]
- Query strings [direct]
- Readline [direct]
- REPL [direct]
- Report [direct]
- Single executable applications [direct]
- SQLite [direct]
- Stream [direct]
- String decoder [direct]
- Test runner [direct]
- Timers [direct]
- TLS/SSL [direct]
- Trace events [direct]
- TTY [direct]
- UDP/datagram [direct]
- URL [direct]
- Utilities [direct]
- V8 [direct]
- Virtual File System [direct]
- VM [direct]
- WASI [direct]
- Web Crypto API [direct]
- Web Streams API [direct]
- Worker threads [direct]
- Zlib [direct]
- Code repository and issue tracker [direct]
- Index [direct]
- 26.x [direct]
- 25.x [direct]
- 24.x LTS [direct]
- 23.x [direct]
- 22.x LTS [direct]
- 21.x [direct]
- 20.x [direct]
- 19.x [direct]
- 18.x [direct]
- 17.x [direct]