SOLFIND
Web Lens
Portal home

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

  1. Skip to content [direct]
  2. Node.js [direct]
  3. About this documentation [direct]
  4. Usage and example [direct]
  5. Assertion testing [direct]
  6. Asynchronous context tracking [direct]
  7. Async hooks [direct]
  8. Buffer [direct]
  9. C++ addons [direct]
  10. C/C++ addons with Node-API [direct]
  11. C++ embedder API [direct]
  12. Child processes [direct]
  13. Cluster [direct]
  14. Command-line options [direct]
  15. Console [direct]
  16. Crypto [direct]
  17. Debugger [direct]
  18. Deprecated APIs [direct]
  19. DNS [direct]
  20. Domain [direct]
  21. Environment Variables [direct]
  22. Errors [direct]
  23. Events [direct]
  24. File system [direct]
  25. FFI [direct]
  26. Globals [direct]
  27. HTTP [direct]
  28. HTTP/2 [direct]
  29. HTTPS [direct]
  30. Inspector [direct]
  31. Internationalization [direct]
  32. Iterable Streams API [direct]
  33. Modules: CommonJS modules [direct]
  34. Modules: ECMAScript modules [direct]
  35. Modules: node:module API [direct]
  36. Modules: Packages [direct]
  37. Modules: TypeScript [direct]
  38. Net [direct]
  39. OS [direct]
  40. Path [direct]
  41. Performance hooks [direct]
  42. Permissions [direct]
  43. Process [direct]
  44. Punycode [direct]
  45. Query strings [direct]
  46. Readline [direct]
  47. REPL [direct]
  48. Report [direct]
  49. Single executable applications [direct]
  50. SQLite [direct]
  51. Stream [direct]
  52. String decoder [direct]
  53. Test runner [direct]
  54. Timers [direct]
  55. TLS/SSL [direct]
  56. Trace events [direct]
  57. TTY [direct]
  58. UDP/datagram [direct]
  59. URL [direct]
  60. Utilities [direct]
  61. V8 [direct]
  62. Virtual File System [direct]
  63. VM [direct]
  64. WASI [direct]
  65. Web Crypto API [direct]
  66. Web Streams API [direct]
  67. Worker threads [direct]
  68. Zlib [direct]
  69. Code repository and issue tracker [direct]
  70. Index [direct]
  71. 26.x [direct]
  72. 25.x [direct]
  73. 24.x LTS [direct]
  74. 23.x [direct]
  75. 22.x LTS [direct]
  76. 21.x [direct]
  77. 20.x [direct]
  78. 19.x [direct]
  79. 18.x [direct]
  80. 17.x [direct]