Diagnostics Channel
Thenode:diagnostics_channel module provides an API to create named channels to report arbitrary message data for diagnostics purposes.
Stability
Stable (Stability: 2)Import
Overview
The diagnostics channel module enables pub/sub pattern for diagnostics data. Module writers can create channels to report messages, and consumers can subscribe to receive them.Public API
diagnostics_channel.hasSubscribers(name)
Check if there are active subscribers to the named channel.
Parameters:
name- The channel name
diagnostics_channel.channel(name)
Get or create a named channel for publishing.
Parameters:
name- The channel name
diagnostics_channel.subscribe(name, onMessage)
Register a message handler to subscribe to a channel.
Parameters:
name- The channel nameonMessage- Handler to receive messagesmessage- The message dataname- The channel name
uncaughtException.
diagnostics_channel.unsubscribe(name, onMessage)
Remove a previously registered message handler.
Parameters:
name- The channel nameonMessage- The handler to remove
true if handler was found, false otherwise
diagnostics_channel.tracingChannel(nameOrChannels)
Create a TracingChannel wrapper for tracing.
Parameters:
nameOrChannels- Channel name or object
Class: Channel
Represents an individual named channel for publishing messages.channel.hasSubscribers
Returns: - If there are active subscribers
Check if there are active subscribers before preparing expensive messages.
channel.publish(message)
Publish a message to all subscribers.
Parameters:
message- The message to send
channel.subscribe(onMessage)
Register a message handler for this channel.
Parameters:
onMessage- Handler to receive messagesmessage- The message dataname- The channel name
channel.unsubscribe(onMessage)
Remove a message handler from this channel.
Parameters:
onMessage- The handler to remove
true if handler was found, false otherwise
channel.bindStore(store[, transform])
Bind an AsyncLocalStorage to the channel.
Parameters:
store- Store to bind context datatransform- Optional transform for context data
runStores() is called, context data is applied to bound stores.
channel.unbindStore(store)
Unbind an AsyncLocalStorage from the channel.
Parameters:
store- The store to unbind
true if store was found, false otherwise
channel.runStores(context, fn[, thisArg[, ...args]])
Apply context to bound stores for the duration of a function.
Parameters:
context- Message to bind to storesfn- Handler to run within contextthisArg- The receiver for the function...args- Optional arguments to pass
Class: TracingChannel
Collection of channels for tracing a single action.tracingChannel.subscribe(subscribers)
Subscribe handlers to all tracing events.
Parameters:
subscribers- Set of event subscribersstart- Start event subscriberend- End event subscriberasyncStart- Async start subscriberasyncEnd- Async end subscribererror- Error event subscriber
tracingChannel.unsubscribe(subscribers)
Unsubscribe handlers from tracing events.
Parameters:
subscribers- Set of event subscribers
true if all handlers unsubscribed successfully
tracingChannel.traceSync(fn[, context[, thisArg[, ...args]]])
Trace a synchronous function call.
Parameters:
fn- Function to wrapcontext- Shared context objectthisArg- The receiver...args- Optional arguments
start and end events, and possibly error events.
tracingChannel.tracePromise(fn[, context[, thisArg[, ...args]]])
Trace a promise-returning function.
Parameters:
fn- Promise-returning functioncontext- Shared context objectthisArg- The receiver...args- Optional arguments
start, end, asyncStart, and asyncEnd events.
tracingChannel.traceCallback(fn[, position[, context[, thisArg[, ...args]]]])
Trace a callback-receiving function.
Parameters:
fn- Callback-using functionposition- Callback argument position (defaults to last)context- Shared context objectthisArg- The receiver...args- Arguments including callback
tracingChannel.hasSubscribers
Returns: - true if any channel has subscribers
Check if any of the tracing channels have active subscribers.
Built-in Channels
Console Events
console.log- Emitted whenconsole.log()is calledconsole.info- Emitted whenconsole.info()is calledconsole.debug- Emitted whenconsole.debug()is calledconsole.warn- Emitted whenconsole.warn()is calledconsole.error- Emitted whenconsole.error()is called
HTTP Events
http.client.request.created- Client creates request objecthttp.client.request.start- Client starts requesthttp.client.request.error- Client request errorhttp.client.response.finish- Client receives responsehttp.server.request.start- Server receives requesthttp.server.response.created- Server creates responsehttp.server.response.finish- Server sends response
HTTP/2 Events
http2.client.stream.created- Client stream createdhttp2.client.stream.start- Client stream startedhttp2.client.stream.error- Client stream errorhttp2.client.stream.finish- Client stream finishedhttp2.server.stream.created- Server stream createdhttp2.server.stream.start- Server stream startedhttp2.server.stream.error- Server stream errorhttp2.server.stream.finish- Server stream finished
Module Events
module.require.start-require()calledmodule.require.end-require()completedmodule.require.error-require()errormodule.import.asyncStart-import()invokedmodule.import.asyncEnd-import()completedmodule.import.error-import()error
Network Events
net.client.socket- TCP/pipe client socket creatednet.server.socket- TCP/pipe server socket createdtracing:net.server.listen:asyncStart- Server listen startedtracing:net.server.listen:asyncEnd- Server listen completedtracing:net.server.listen:error- Server listen error
UDP Events
udp.socket- UDP socket created
Process Events
child_process- Child process createdtracing:child_process.spawn:start- Spawn invokedtracing:child_process.spawn:end- Spawn completedtracing:child_process.spawn:error- Spawn errorexecve-process.execve()invoked
Worker Events
worker_threads- Worker thread created