home / documentation / v19 / diagnostics_channel

Diagnostics Channel

目录

自 v15.1.0 版本开始新增

稳定级别:1 - Experimental

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:

MJS
CJS

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.

MJS
CJS
M diagnostics_channel.hasSubscribers(name)

自 v15.1.0, v14.17.0 版本开始新增

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.

MJS
CJS
M diagnostics_channel.channel(name)

自 v15.1.0, v14.17.0 版本开始新增

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.

MJS
CJS
M diagnostics_channel.subscribe(name, onMessage)

自 v18.7.0, v16.17.0 版本开始新增

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'.

MJS
CJS
M diagnostics_channel.unsubscribe(name, onMessage)

自 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).

MJS
CJS

C Channel

自 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.

M channel.hasSubscribers

自 v15.1.0, v14.17.0 版本开始新增

  • Returns: boolean If there are active subscribers

Check if there are active subscribers to this 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.

MJS
CJS
M channel.publish(message)

自 v15.1.0, v14.17.0 版本开始新增

  • message any The message to send to the channel subscribers

Publish a message to any subscribers to the channel. This will trigger message handlers synchronously so they will execute within the same context.

MJS
CJS
M channel.subscribe(onMessage)

自 v18.7.0, v16.17.0 版本开始弃用

稳定级别:0 - Deprecated: Use `diagnostics_channel.subscribe(name, onMessage)`
  • onMessage Function The handler to receive channel messages

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'.

MJS
CJS
M channel.unsubscribe(onMessage)
历史
版本历史变更
v17.1.0, v16.14.0, v14.19.0Added return value. Added to channels without subscribers.
v18.7.0, v16.17.0自 v18.7.0, v16.17.0 版本开始新增
稳定级别:0 - Deprecated: Use `diagnostics_channel.unsubscribe(name, onMessage)`
  • 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 channel.subscribe(onMessage).

MJS
CJS

Built-in Channels

HTTP

http.client.request.start

Emitted when client starts a request.

http.client.response.finish

Emitted when client receives a response.

http.server.request.start

Emitted when server receives a request.

http.server.response.finish

Emitted when server sends a response.

NET

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

udp.socket

Emitted when a new UDP socket is created.

Process

自 v16.18.0 版本开始新增

child_process

Emitted when a new process is created.

Worker Thread

自 v16.18.0 版本开始新增

worker_threads

Emitted when a new thread is created.