1<!DOCTYPE html> 2<html lang="en"> 3<head> 4 <meta charset="utf-8"> 5 <meta name="viewport" content="width=device-width"> 6 <meta name="nodejs.org:node-version" content="v18.20.1"> 7 <title>Diagnostics Channel | Node.js v18.20.1 Documentation</title> 8 <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:400,700,400italic&display=fallback"> 9 <link rel="stylesheet" href="assets/style.css"> 10 <link rel="stylesheet" href="assets/hljs.css"> 11 <link rel="canonical" href="https://nodejs.org/api/diagnostics_channel.html"> 12 <script async defer src="assets/api.js" type="text/javascript"></script> 13 <style>@media(max-width:734px){.with-64-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}</style> 14</head> 15<body class="alt apidoc" id="api-section-diagnostics_channel"> 16 <div id="content" class="clearfix"> 17 <div id="column2" class="interior"> 18 <div id="intro" class="interior"> 19 <a href="/" title="Go back to the home page"> 20 Node.js 21 </a> 22 </div> 23 <ul> 24<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li> 25<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li> 26</ul> 27<hr class="line"> 28<ul> 29<li><a href="assert.html" class="nav-assert">Assertion testing</a></li> 30<li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li> 31<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li> 32<li><a href="buffer.html" class="nav-buffer">Buffer</a></li> 33<li><a href="addons.html" class="nav-addons">C++ addons</a></li> 34<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li> 35<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li> 36<li><a href="child_process.html" class="nav-child_process">Child processes</a></li> 37<li><a href="cluster.html" class="nav-cluster">Cluster</a></li> 38<li><a href="cli.html" class="nav-cli">Command-line options</a></li> 39<li><a href="console.html" class="nav-console">Console</a></li> 40<li><a href="corepack.html" class="nav-corepack">Corepack</a></li> 41<li><a href="crypto.html" class="nav-crypto">Crypto</a></li> 42<li><a href="debugger.html" class="nav-debugger">Debugger</a></li> 43<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li> 44<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel active">Diagnostics Channel</a></li> 45<li><a href="dns.html" class="nav-dns">DNS</a></li> 46<li><a href="domain.html" class="nav-domain">Domain</a></li> 47<li><a href="errors.html" class="nav-errors">Errors</a></li> 48<li><a href="events.html" class="nav-events">Events</a></li> 49<li><a href="fs.html" class="nav-fs">File system</a></li> 50<li><a href="globals.html" class="nav-globals">Globals</a></li> 51<li><a href="http.html" class="nav-http">HTTP</a></li> 52<li><a href="http2.html" class="nav-http2">HTTP/2</a></li> 53<li><a href="https.html" class="nav-https">HTTPS</a></li> 54<li><a href="inspector.html" class="nav-inspector">Inspector</a></li> 55<li><a href="intl.html" class="nav-intl">Internationalization</a></li> 56<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li> 57<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li> 58<li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li> 59<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li> 60<li><a href="net.html" class="nav-net">Net</a></li> 61<li><a href="os.html" class="nav-os">OS</a></li> 62<li><a href="path.html" class="nav-path">Path</a></li> 63<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li> 64<li><a href="permissions.html" class="nav-permissions">Permissions</a></li> 65<li><a href="process.html" class="nav-process">Process</a></li> 66<li><a href="punycode.html" class="nav-punycode">Punycode</a></li> 67<li><a href="querystring.html" class="nav-querystring">Query strings</a></li> 68<li><a href="readline.html" class="nav-readline">Readline</a></li> 69<li><a href="repl.html" class="nav-repl">REPL</a></li> 70<li><a href="report.html" class="nav-report">Report</a></li> 71<li><a href="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</a></li> 72<li><a href="stream.html" class="nav-stream">Stream</a></li> 73<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li> 74<li><a href="test.html" class="nav-test">Test runner</a></li> 75<li><a href="timers.html" class="nav-timers">Timers</a></li> 76<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li> 77<li><a href="tracing.html" class="nav-tracing">Trace events</a></li> 78<li><a href="tty.html" class="nav-tty">TTY</a></li> 79<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li> 80<li><a href="url.html" class="nav-url">URL</a></li> 81<li><a href="util.html" class="nav-util">Utilities</a></li> 82<li><a href="v8.html" class="nav-v8">V8</a></li> 83<li><a href="vm.html" class="nav-vm">VM</a></li> 84<li><a href="wasi.html" class="nav-wasi">WASI</a></li> 85<li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li> 86<li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li> 87<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li> 88<li><a href="zlib.html" class="nav-zlib">Zlib</a></li> 89</ul> 90<hr class="line"> 91<ul> 92<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li> 93</ul> 94 </div> 95 96 <div id="column1" data-id="diagnostics_channel" class="interior"> 97 <header class="header"> 98 <div class="header-container"> 99 <h1>Node.js v18.20.1 documentation</h1> 100 <button class="theme-toggle-btn" id="theme-toggle-btn" title="Toggle dark mode/light mode" aria-label="Toggle dark mode/light mode" hidden> 101 <svg xmlns="http://www.w3.org/2000/svg" class="icon dark-icon" height="24" width="24"> 102 <path fill="none" d="M0 0h24v24H0z" /> 103 <path d="M11.1 12.08c-2.33-4.51-.5-8.48.53-10.07C6.27 2.2 1.98 6.59 1.98 12c0 .14.02.28.02.42.62-.27 1.29-.42 2-.42 1.66 0 3.18.83 4.1 2.15A4.01 4.01 0 0111 18c0 1.52-.87 2.83-2.12 3.51.98.32 2.03.5 3.11.5 3.5 0 6.58-1.8 8.37-4.52-2.36.23-6.98-.97-9.26-5.41z"/> 104 <path d="M7 16h-.18C6.4 14.84 5.3 14 4 14c-1.66 0-3 1.34-3 3s1.34 3 3 3h3c1.1 0 2-.9 2-2s-.9-2-2-2z"/> 105 </svg> 106 <svg xmlns="http://www.w3.org/2000/svg" class="icon light-icon" height="24" width="24"> 107 <path d="M0 0h24v24H0z" fill="none" /> 108 <path d="M6.76 4.84l-1.8-1.79-1.41 1.41 1.79 1.79 1.42-1.41zM4 10.5H1v2h3v-2zm9-9.95h-2V3.5h2V.55zm7.45 3.91l-1.41-1.41-1.79 1.79 1.41 1.41 1.79-1.79zm-3.21 13.7l1.79 1.8 1.41-1.41-1.8-1.79-1.4 1.4zM20 10.5v2h3v-2h-3zm-8-5c-3.31 0-6 2.69-6 6s2.69 6 6 6 6-2.69 6-6-2.69-6-6-6zm-1 16.95h2V19.5h-2v2.95zm-7.45-3.91l1.41 1.41 1.79-1.8-1.41-1.41-1.79 1.8z"/> 109 </svg> 110 </button> 111 </div> 112 <div id="gtoc"> 113 <ul> 114 <li class="pinned-header">Node.js v18.20.1</li> 115 116 <li class="picker-header"> 117 <a href="#"> 118 <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> 119 Table of contents 120 </a> 121 122 <div class="picker"><div class="toc"><ul> 123<li><span class="stability_2"><a href="#diagnostics-channel">Diagnostics Channel</a></span> 124<ul> 125<li><a href="#public-api">Public API</a> 126<ul> 127<li><a href="#overview">Overview</a> 128<ul> 129<li><a href="#diagnostics_channelhassubscribersname"><code>diagnostics_channel.hasSubscribers(name)</code></a></li> 130<li><a href="#diagnostics_channelchannelname"><code>diagnostics_channel.channel(name)</code></a></li> 131<li><a href="#diagnostics_channelsubscribename-onmessage"><code>diagnostics_channel.subscribe(name, onMessage)</code></a></li> 132<li><a href="#diagnostics_channelunsubscribename-onmessage"><code>diagnostics_channel.unsubscribe(name, onMessage)</code></a></li> 133<li><span class="stability_1"><a href="#diagnostics_channeltracingchannelnameorchannels"><code>diagnostics_channel.tracingChannel(nameOrChannels)</code></a></span></li> 134</ul> 135</li> 136<li><a href="#class-channel">Class: <code>Channel</code></a> 137<ul> 138<li><a href="#channelhassubscribers"><code>channel.hasSubscribers</code></a></li> 139<li><a href="#channelpublishmessage"><code>channel.publish(message)</code></a></li> 140<li><span class="stability_0"><a href="#channelsubscribeonmessage"><code>channel.subscribe(onMessage)</code></a></span></li> 141<li><span class="stability_0"><a href="#channelunsubscribeonmessage"><code>channel.unsubscribe(onMessage)</code></a></span></li> 142<li><span class="stability_1"><a href="#channelbindstorestore-transform"><code>channel.bindStore(store[, transform])</code></a></span></li> 143<li><span class="stability_1"><a href="#channelunbindstorestore"><code>channel.unbindStore(store)</code></a></span></li> 144<li><span class="stability_1"><a href="#channelrunstorescontext-fn-thisarg-args"><code>channel.runStores(context, fn[, thisArg[, ...args]])</code></a></span></li> 145</ul> 146</li> 147<li><span class="stability_1"><a href="#class-tracingchannel">Class: <code>TracingChannel</code></a></span> 148<ul> 149<li><span class="stability_1"><a href="#tracingchannelsubscribesubscribers"><code>tracingChannel.subscribe(subscribers)</code></a></span></li> 150<li><span class="stability_1"><a href="#tracingchannelunsubscribesubscribers"><code>tracingChannel.unsubscribe(subscribers)</code></a></span></li> 151<li><span class="stability_1"><a href="#tracingchanneltracesyncfn-context-thisarg-args"><code>tracingChannel.traceSync(fn[, context[, thisArg[, ...args]]])</code></a></span></li> 152<li><span class="stability_1"><a href="#tracingchanneltracepromisefn-context-thisarg-args"><code>tracingChannel.tracePromise(fn[, context[, thisArg[, ...args]]])</code></a></span></li> 153<li><span class="stability_1"><a href="#tracingchanneltracecallbackfn-position-context-thisarg-args"><code>tracingChannel.traceCallback(fn[, position[, context[, thisArg[, ...args]]]])</code></a></span></li> 154</ul> 155</li> 156<li><a href="#tracingchannel-channels">TracingChannel Channels</a> 157<ul> 158<li><a href="#startevent"><code>start(event)</code></a></li> 159<li><a href="#endevent"><code>end(event)</code></a></li> 160<li><a href="#asyncstartevent"><code>asyncStart(event)</code></a></li> 161<li><a href="#asyncendevent"><code>asyncEnd(event)</code></a></li> 162<li><a href="#errorevent"><code>error(event)</code></a></li> 163</ul> 164</li> 165<li><span class="stability_1"><a href="#built-in-channels">Built-in Channels</a></span> 166<ul> 167<li><a href="#http">HTTP</a></li> 168</ul> 169</li> 170</ul> 171</li> 172</ul> 173</li> 174</ul></div></div> 175 </li> 176 177 178 <li class="picker-header"> 179 <a href="#"> 180 <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> 181 Index 182 </a> 183 184 <div class="picker"><ul> 185<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li> 186<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li> 187 188 <li> 189 <a href="index.html">Index</a> 190 </li> 191 </ul> 192 193<hr class="line"> 194<ul> 195<li><a href="assert.html" class="nav-assert">Assertion testing</a></li> 196<li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li> 197<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li> 198<li><a href="buffer.html" class="nav-buffer">Buffer</a></li> 199<li><a href="addons.html" class="nav-addons">C++ addons</a></li> 200<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li> 201<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li> 202<li><a href="child_process.html" class="nav-child_process">Child processes</a></li> 203<li><a href="cluster.html" class="nav-cluster">Cluster</a></li> 204<li><a href="cli.html" class="nav-cli">Command-line options</a></li> 205<li><a href="console.html" class="nav-console">Console</a></li> 206<li><a href="corepack.html" class="nav-corepack">Corepack</a></li> 207<li><a href="crypto.html" class="nav-crypto">Crypto</a></li> 208<li><a href="debugger.html" class="nav-debugger">Debugger</a></li> 209<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li> 210<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel active">Diagnostics Channel</a></li> 211<li><a href="dns.html" class="nav-dns">DNS</a></li> 212<li><a href="domain.html" class="nav-domain">Domain</a></li> 213<li><a href="errors.html" class="nav-errors">Errors</a></li> 214<li><a href="events.html" class="nav-events">Events</a></li> 215<li><a href="fs.html" class="nav-fs">File system</a></li> 216<li><a href="globals.html" class="nav-globals">Globals</a></li> 217<li><a href="http.html" class="nav-http">HTTP</a></li> 218<li><a href="http2.html" class="nav-http2">HTTP/2</a></li> 219<li><a href="https.html" class="nav-https">HTTPS</a></li> 220<li><a href="inspector.html" class="nav-inspector">Inspector</a></li> 221<li><a href="intl.html" class="nav-intl">Internationalization</a></li> 222<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li> 223<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li> 224<li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li> 225<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li> 226<li><a href="net.html" class="nav-net">Net</a></li> 227<li><a href="os.html" class="nav-os">OS</a></li> 228<li><a href="path.html" class="nav-path">Path</a></li> 229<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li> 230<li><a href="permissions.html" class="nav-permissions">Permissions</a></li> 231<li><a href="process.html" class="nav-process">Process</a></li> 232<li><a href="punycode.html" class="nav-punycode">Punycode</a></li> 233<li><a href="querystring.html" class="nav-querystring">Query strings</a></li> 234<li><a href="readline.html" class="nav-readline">Readline</a></li> 235<li><a href="repl.html" class="nav-repl">REPL</a></li> 236<li><a href="report.html" class="nav-report">Report</a></li> 237<li><a href="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</a></li> 238<li><a href="stream.html" class="nav-stream">Stream</a></li> 239<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li> 240<li><a href="test.html" class="nav-test">Test runner</a></li> 241<li><a href="timers.html" class="nav-timers">Timers</a></li> 242<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li> 243<li><a href="tracing.html" class="nav-tracing">Trace events</a></li> 244<li><a href="tty.html" class="nav-tty">TTY</a></li> 245<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li> 246<li><a href="url.html" class="nav-url">URL</a></li> 247<li><a href="util.html" class="nav-util">Utilities</a></li> 248<li><a href="v8.html" class="nav-v8">V8</a></li> 249<li><a href="vm.html" class="nav-vm">VM</a></li> 250<li><a href="wasi.html" class="nav-wasi">WASI</a></li> 251<li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li> 252<li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li> 253<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li> 254<li><a href="zlib.html" class="nav-zlib">Zlib</a></li> 255</ul> 256<hr class="line"> 257<ul> 258<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li> 259</ul></div> 260 </li> 261 262 263 <li class="picker-header"> 264 <a href="#"> 265 <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> 266 Other versions 267 </a> 268 <div class="picker"><ol id="alt-docs"><li><a href="https://nodejs.org/docs/latest-v21.x/api/diagnostics_channel.html">21.x</a></li> 269<li><a href="https://nodejs.org/docs/latest-v20.x/api/diagnostics_channel.html">20.x <b>LTS</b></a></li> 270<li><a href="https://nodejs.org/docs/latest-v19.x/api/diagnostics_channel.html">19.x</a></li> 271<li><a href="https://nodejs.org/docs/latest-v18.x/api/diagnostics_channel.html">18.x <b>LTS</b></a></li> 272<li><a href="https://nodejs.org/docs/latest-v17.x/api/diagnostics_channel.html">17.x</a></li> 273<li><a href="https://nodejs.org/docs/latest-v16.x/api/diagnostics_channel.html">16.x</a></li> 274<li><a href="https://nodejs.org/docs/latest-v15.x/api/diagnostics_channel.html">15.x</a></li></ol></div> 275 </li> 276 277 <li class="picker-header"> 278 <a href="#"> 279 <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> 280 Options 281 </a> 282 283 <div class="picker"> 284 <ul> 285 <li> 286 <a href="all.html">View on single page</a> 287 </li> 288 <li> 289 <a href="diagnostics_channel.json">View as JSON</a> 290 </li> 291 <li class="edit_on_github"><a href="https://github.com/nodejs/node/edit/main/doc/api/diagnostics_channel.md">Edit on GitHub</a></li> 292 </ul> 293 </div> 294 </li> 295 </ul> 296 </div> 297 <hr> 298 </header> 299 300 <details id="toc" open><summary>Table of contents</summary><ul> 301<li><span class="stability_2"><a href="#diagnostics-channel">Diagnostics Channel</a></span> 302<ul> 303<li><a href="#public-api">Public API</a> 304<ul> 305<li><a href="#overview">Overview</a> 306<ul> 307<li><a href="#diagnostics_channelhassubscribersname"><code>diagnostics_channel.hasSubscribers(name)</code></a></li> 308<li><a href="#diagnostics_channelchannelname"><code>diagnostics_channel.channel(name)</code></a></li> 309<li><a href="#diagnostics_channelsubscribename-onmessage"><code>diagnostics_channel.subscribe(name, onMessage)</code></a></li> 310<li><a href="#diagnostics_channelunsubscribename-onmessage"><code>diagnostics_channel.unsubscribe(name, onMessage)</code></a></li> 311<li><span class="stability_1"><a href="#diagnostics_channeltracingchannelnameorchannels"><code>diagnostics_channel.tracingChannel(nameOrChannels)</code></a></span></li> 312</ul> 313</li> 314<li><a href="#class-channel">Class: <code>Channel</code></a> 315<ul> 316<li><a href="#channelhassubscribers"><code>channel.hasSubscribers</code></a></li> 317<li><a href="#channelpublishmessage"><code>channel.publish(message)</code></a></li> 318<li><span class="stability_0"><a href="#channelsubscribeonmessage"><code>channel.subscribe(onMessage)</code></a></span></li> 319<li><span class="stability_0"><a href="#channelunsubscribeonmessage"><code>channel.unsubscribe(onMessage)</code></a></span></li> 320<li><span class="stability_1"><a href="#channelbindstorestore-transform"><code>channel.bindStore(store[, transform])</code></a></span></li> 321<li><span class="stability_1"><a href="#channelunbindstorestore"><code>channel.unbindStore(store)</code></a></span></li> 322<li><span class="stability_1"><a href="#channelrunstorescontext-fn-thisarg-args"><code>channel.runStores(context, fn[, thisArg[, ...args]])</code></a></span></li> 323</ul> 324</li> 325<li><span class="stability_1"><a href="#class-tracingchannel">Class: <code>TracingChannel</code></a></span> 326<ul> 327<li><span class="stability_1"><a href="#tracingchannelsubscribesubscribers"><code>tracingChannel.subscribe(subscribers)</code></a></span></li> 328<li><span class="stability_1"><a href="#tracingchannelunsubscribesubscribers"><code>tracingChannel.unsubscribe(subscribers)</code></a></span></li> 329<li><span class="stability_1"><a href="#tracingchanneltracesyncfn-context-thisarg-args"><code>tracingChannel.traceSync(fn[, context[, thisArg[, ...args]]])</code></a></span></li> 330<li><span class="stability_1"><a href="#tracingchanneltracepromisefn-context-thisarg-args"><code>tracingChannel.tracePromise(fn[, context[, thisArg[, ...args]]])</code></a></span></li> 331<li><span class="stability_1"><a href="#tracingchanneltracecallbackfn-position-context-thisarg-args"><code>tracingChannel.traceCallback(fn[, position[, context[, thisArg[, ...args]]]])</code></a></span></li> 332</ul> 333</li> 334<li><a href="#tracingchannel-channels">TracingChannel Channels</a> 335<ul> 336<li><a href="#startevent"><code>start(event)</code></a></li> 337<li><a href="#endevent"><code>end(event)</code></a></li> 338<li><a href="#asyncstartevent"><code>asyncStart(event)</code></a></li> 339<li><a href="#asyncendevent"><code>asyncEnd(event)</code></a></li> 340<li><a href="#errorevent"><code>error(event)</code></a></li> 341</ul> 342</li> 343<li><span class="stability_1"><a href="#built-in-channels">Built-in Channels</a></span> 344<ul> 345<li><a href="#http">HTTP</a></li> 346</ul> 347</li> 348</ul> 349</li> 350</ul> 351</li> 352</ul></details> 353 354 <div id="apicontent"> 355 <h2>Diagnostics Channel<span><a class="mark" href="#diagnostics-channel" id="diagnostics-channel">#</a></span><a aria-hidden="true" class="legacy" id="diagnostics_channel_diagnostics_channel"></a></h2> 356<div class="api_metadata"> 357<details class="changelog"><summary>History</summary> 358<table> 359<tbody><tr><th>Version</th><th>Changes</th></tr> 360<tr><td>v18.13.0</td> 361<td><p>diagnostics_channel is now Stable.</p></td></tr> 362<tr><td>v15.1.0, v14.17.0</td> 363<td><p><span>Added in: v15.1.0, v14.17.0</span></p></td></tr> 364</tbody></table> 365</details> 366</div> 367 368<p></p><div class="api_stability api_stability_2"><a href="documentation.html#stability-index">Stability: 2</a> - Stable</div><p></p> 369<p><strong>Source Code:</strong> <a href="https://github.com/nodejs/node/blob/v18.20.1/lib/diagnostics_channel.js">lib/diagnostics_channel.js</a></p> 370<p>The <code>node:diagnostics_channel</code> module provides an API to create named channels 371to report arbitrary message data for diagnostics purposes.</p> 372<p>It can be accessed using:</p> 373 374<pre class="with-64-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> diagnostics_channel <span class="hljs-keyword">from</span> <span class="hljs-string">'node:diagnostics_channel'</span>;</code><code class="language-js cjs"><span class="hljs-keyword">const</span> diagnostics_channel = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:diagnostics_channel'</span>);</code><button class="copy-button">copy</button></pre> 375<p>It is intended that a module writer wanting to report diagnostics messages 376will create one or many top-level channels to report messages through. 377Channels may also be acquired at runtime but it is not encouraged 378due to the additional overhead of doing so. Channels may be exported for 379convenience, but as long as the name is known it can be acquired anywhere.</p> 380<p>If you intend for your module to produce diagnostics data for others to 381consume it is recommended that you include documentation of what named 382channels are used along with the shape of the message data. Channel names 383should generally include the module name to avoid collisions with data from 384other modules.</p> 385<section><h3>Public API<span><a class="mark" href="#public-api" id="public-api">#</a></span><a aria-hidden="true" class="legacy" id="diagnostics_channel_public_api"></a></h3> 386<h4>Overview<span><a class="mark" href="#overview" id="overview">#</a></span><a aria-hidden="true" class="legacy" id="diagnostics_channel_overview"></a></h4> 387<p>Following is a simple overview of the public API.</p> 388 389<pre class="with-64-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> diagnostics_channel <span class="hljs-keyword">from</span> <span class="hljs-string">'node:diagnostics_channel'</span>; 390 391<span class="hljs-comment">// Get a reusable channel object</span> 392<span class="hljs-keyword">const</span> channel = diagnostics_channel.<span class="hljs-title function_">channel</span>(<span class="hljs-string">'my-channel'</span>); 393 394<span class="hljs-keyword">function</span> <span class="hljs-title function_">onMessage</span>(<span class="hljs-params">message, name</span>) { 395 <span class="hljs-comment">// Received data</span> 396} 397 398<span class="hljs-comment">// Subscribe to the channel</span> 399diagnostics_channel.<span class="hljs-title function_">subscribe</span>(<span class="hljs-string">'my-channel'</span>, onMessage); 400 401<span class="hljs-comment">// Check if the channel has an active subscriber</span> 402<span class="hljs-keyword">if</span> (channel.<span class="hljs-property">hasSubscribers</span>) { 403 <span class="hljs-comment">// Publish data to the channel</span> 404 channel.<span class="hljs-title function_">publish</span>({ 405 <span class="hljs-attr">some</span>: <span class="hljs-string">'data'</span>, 406 }); 407} 408 409<span class="hljs-comment">// Unsubscribe from the channel</span> 410diagnostics_channel.<span class="hljs-title function_">unsubscribe</span>(<span class="hljs-string">'my-channel'</span>, onMessage);</code><code class="language-js cjs"><span class="hljs-keyword">const</span> diagnostics_channel = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:diagnostics_channel'</span>); 411 412<span class="hljs-comment">// Get a reusable channel object</span> 413<span class="hljs-keyword">const</span> channel = diagnostics_channel.<span class="hljs-title function_">channel</span>(<span class="hljs-string">'my-channel'</span>); 414 415<span class="hljs-keyword">function</span> <span class="hljs-title function_">onMessage</span>(<span class="hljs-params">message, name</span>) { 416 <span class="hljs-comment">// Received data</span> 417} 418 419<span class="hljs-comment">// Subscribe to the channel</span> 420diagnostics_channel.<span class="hljs-title function_">subscribe</span>(<span class="hljs-string">'my-channel'</span>, onMessage); 421 422<span class="hljs-comment">// Check if the channel has an active subscriber</span> 423<span class="hljs-keyword">if</span> (channel.<span class="hljs-property">hasSubscribers</span>) { 424 <span class="hljs-comment">// Publish data to the channel</span> 425 channel.<span class="hljs-title function_">publish</span>({ 426 <span class="hljs-attr">some</span>: <span class="hljs-string">'data'</span>, 427 }); 428} 429 430<span class="hljs-comment">// Unsubscribe from the channel</span> 431diagnostics_channel.<span class="hljs-title function_">unsubscribe</span>(<span class="hljs-string">'my-channel'</span>, onMessage);</code><button class="copy-button">copy</button></pre> 432<h5><code>diagnostics_channel.hasSubscribers(name)</code><span><a class="mark" href="#diagnostics_channelhassubscribersname" id="diagnostics_channelhassubscribersname">#</a></span><a aria-hidden="true" class="legacy" id="diagnostics_channel_diagnostics_channel_hassubscribers_name"></a></h5> 433<div class="api_metadata"> 434<span>Added in: v15.1.0, v14.17.0</span> 435</div> 436<ul> 437<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Symbol_type" class="type"><symbol></a> The channel name</li> 438<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> If there are active subscribers</li> 439</ul> 440<p>Check if there are active subscribers to the named channel. This is helpful if 441the message you want to send might be expensive to prepare.</p> 442<p>This API is optional but helpful when trying to publish messages from very 443performance-sensitive code.</p> 444 445<pre class="with-64-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> diagnostics_channel <span class="hljs-keyword">from</span> <span class="hljs-string">'node:diagnostics_channel'</span>; 446 447<span class="hljs-keyword">if</span> (diagnostics_channel.<span class="hljs-title function_">hasSubscribers</span>(<span class="hljs-string">'my-channel'</span>)) { 448 <span class="hljs-comment">// There are subscribers, prepare and publish message</span> 449}</code><code class="language-js cjs"><span class="hljs-keyword">const</span> diagnostics_channel = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:diagnostics_channel'</span>); 450 451<span class="hljs-keyword">if</span> (diagnostics_channel.<span class="hljs-title function_">hasSubscribers</span>(<span class="hljs-string">'my-channel'</span>)) { 452 <span class="hljs-comment">// There are subscribers, prepare and publish message</span> 453}</code><button class="copy-button">copy</button></pre> 454<h5><code>diagnostics_channel.channel(name)</code><span><a class="mark" href="#diagnostics_channelchannelname" id="diagnostics_channelchannelname">#</a></span><a aria-hidden="true" class="legacy" id="diagnostics_channel_diagnostics_channel_channel_name"></a></h5> 455<div class="api_metadata"> 456<span>Added in: v15.1.0, v14.17.0</span> 457</div> 458<ul> 459<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Symbol_type" class="type"><symbol></a> The channel name</li> 460<li>Returns: <a href="diagnostics_channel.html#class-channel" class="type"><Channel></a> The named channel object</li> 461</ul> 462<p>This is the primary entry-point for anyone wanting to publish to a named 463channel. It produces a channel object which is optimized to reduce overhead at 464publish time as much as possible.</p> 465 466<pre class="with-64-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> diagnostics_channel <span class="hljs-keyword">from</span> <span class="hljs-string">'node:diagnostics_channel'</span>; 467 468<span class="hljs-keyword">const</span> channel = diagnostics_channel.<span class="hljs-title function_">channel</span>(<span class="hljs-string">'my-channel'</span>);</code><code class="language-js cjs"><span class="hljs-keyword">const</span> diagnostics_channel = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:diagnostics_channel'</span>); 469 470<span class="hljs-keyword">const</span> channel = diagnostics_channel.<span class="hljs-title function_">channel</span>(<span class="hljs-string">'my-channel'</span>);</code><button class="copy-button">copy</button></pre> 471<h5><code>diagnostics_channel.subscribe(name, onMessage)</code><span><a class="mark" href="#diagnostics_channelsubscribename-onmessage" id="diagnostics_channelsubscribename-onmessage">#</a></span><a aria-hidden="true" class="legacy" id="diagnostics_channel_diagnostics_channel_subscribe_name_onmessage"></a></h5> 472<div class="api_metadata"> 473<span>Added in: v18.7.0</span> 474</div> 475<ul> 476<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Symbol_type" class="type"><symbol></a> The channel name</li> 477<li><code>onMessage</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> The handler to receive channel messages 478<ul> 479<li><code>message</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a> The message data</li> 480<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Symbol_type" class="type"><symbol></a> The name of the channel</li> 481</ul> 482</li> 483</ul> 484<p>Register a message handler to subscribe to this channel. This message handler 485will be run synchronously whenever a message is published to the channel. Any 486errors thrown in the message handler will trigger an <a href="process.html#event-uncaughtexception"><code>'uncaughtException'</code></a>.</p> 487 488<pre class="with-64-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> diagnostics_channel <span class="hljs-keyword">from</span> <span class="hljs-string">'node:diagnostics_channel'</span>; 489 490diagnostics_channel.<span class="hljs-title function_">subscribe</span>(<span class="hljs-string">'my-channel'</span>, <span class="hljs-function">(<span class="hljs-params">message, name</span>) =></span> { 491 <span class="hljs-comment">// Received data</span> 492});</code><code class="language-js cjs"><span class="hljs-keyword">const</span> diagnostics_channel = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:diagnostics_channel'</span>); 493 494diagnostics_channel.<span class="hljs-title function_">subscribe</span>(<span class="hljs-string">'my-channel'</span>, <span class="hljs-function">(<span class="hljs-params">message, name</span>) =></span> { 495 <span class="hljs-comment">// Received data</span> 496});</code><button class="copy-button">copy</button></pre> 497<h5><code>diagnostics_channel.unsubscribe(name, onMessage)</code><span><a class="mark" href="#diagnostics_channelunsubscribename-onmessage" id="diagnostics_channelunsubscribename-onmessage">#</a></span><a aria-hidden="true" class="legacy" id="diagnostics_channel_diagnostics_channel_unsubscribe_name_onmessage"></a></h5> 498<div class="api_metadata"> 499<span>Added in: v18.7.0</span> 500</div> 501<ul> 502<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Symbol_type" class="type"><symbol></a> The channel name</li> 503<li><code>onMessage</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> The previous subscribed handler to remove</li> 504<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <code>true</code> if the handler was found, <code>false</code> otherwise.</li> 505</ul> 506<p>Remove a message handler previously registered to this channel with 507<a href="#diagnostics_channelsubscribename-onmessage"><code>diagnostics_channel.subscribe(name, onMessage)</code></a>.</p> 508 509<pre class="with-64-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> diagnostics_channel <span class="hljs-keyword">from</span> <span class="hljs-string">'node:diagnostics_channel'</span>; 510 511<span class="hljs-keyword">function</span> <span class="hljs-title function_">onMessage</span>(<span class="hljs-params">message, name</span>) { 512 <span class="hljs-comment">// Received data</span> 513} 514 515diagnostics_channel.<span class="hljs-title function_">subscribe</span>(<span class="hljs-string">'my-channel'</span>, onMessage); 516 517diagnostics_channel.<span class="hljs-title function_">unsubscribe</span>(<span class="hljs-string">'my-channel'</span>, onMessage);</code><code class="language-js cjs"><span class="hljs-keyword">const</span> diagnostics_channel = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:diagnostics_channel'</span>); 518 519<span class="hljs-keyword">function</span> <span class="hljs-title function_">onMessage</span>(<span class="hljs-params">message, name</span>) { 520 <span class="hljs-comment">// Received data</span> 521} 522 523diagnostics_channel.<span class="hljs-title function_">subscribe</span>(<span class="hljs-string">'my-channel'</span>, onMessage); 524 525diagnostics_channel.<span class="hljs-title function_">unsubscribe</span>(<span class="hljs-string">'my-channel'</span>, onMessage);</code><button class="copy-button">copy</button></pre> 526<h5><code>diagnostics_channel.tracingChannel(nameOrChannels)</code><span><a class="mark" href="#diagnostics_channeltracingchannelnameorchannels" id="diagnostics_channeltracingchannelnameorchannels">#</a></span><a aria-hidden="true" class="legacy" id="diagnostics_channel_diagnostics_channel_tracingchannel_nameorchannels"></a></h5> 527<div class="api_metadata"> 528<span>Added in: v18.19.0</span> 529</div> 530<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p> 531<ul> 532<li><code>nameOrChannels</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="diagnostics_channel.html#class-tracingchannel" class="type"><TracingChannel></a> Channel name or 533object containing all the <a href="#tracingchannel-channels">TracingChannel Channels</a></li> 534<li>Returns: <a href="diagnostics_channel.html#class-tracingchannel" class="type"><TracingChannel></a> Collection of channels to trace with</li> 535</ul> 536<p>Creates a <a href="#class-tracingchannel"><code>TracingChannel</code></a> wrapper for the given 537<a href="#tracingchannel-channels">TracingChannel Channels</a>. If a name is given, the corresponding tracing 538channels will be created in the form of <code>tracing:${name}:${eventType}</code> where 539<code>eventType</code> corresponds to the types of <a href="#tracingchannel-channels">TracingChannel Channels</a>.</p> 540 541<pre class="with-64-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> diagnostics_channel <span class="hljs-keyword">from</span> <span class="hljs-string">'node:diagnostics_channel'</span>; 542 543<span class="hljs-keyword">const</span> channelsByName = diagnostics_channel.<span class="hljs-title function_">tracingChannel</span>(<span class="hljs-string">'my-channel'</span>); 544 545<span class="hljs-comment">// or...</span> 546 547<span class="hljs-keyword">const</span> channelsByCollection = diagnostics_channel.<span class="hljs-title function_">tracingChannel</span>({ 548 <span class="hljs-attr">start</span>: diagnostics_channel.<span class="hljs-title function_">channel</span>(<span class="hljs-string">'tracing:my-channel:start'</span>), 549 <span class="hljs-attr">end</span>: diagnostics_channel.<span class="hljs-title function_">channel</span>(<span class="hljs-string">'tracing:my-channel:end'</span>), 550 <span class="hljs-attr">asyncStart</span>: diagnostics_channel.<span class="hljs-title function_">channel</span>(<span class="hljs-string">'tracing:my-channel:asyncStart'</span>), 551 <span class="hljs-attr">asyncEnd</span>: diagnostics_channel.<span class="hljs-title function_">channel</span>(<span class="hljs-string">'tracing:my-channel:asyncEnd'</span>), 552 <span class="hljs-attr">error</span>: diagnostics_channel.<span class="hljs-title function_">channel</span>(<span class="hljs-string">'tracing:my-channel:error'</span>), 553});</code><code class="language-js cjs"><span class="hljs-keyword">const</span> diagnostics_channel = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:diagnostics_channel'</span>); 554 555<span class="hljs-keyword">const</span> channelsByName = diagnostics_channel.<span class="hljs-title function_">tracingChannel</span>(<span class="hljs-string">'my-channel'</span>); 556 557<span class="hljs-comment">// or...</span> 558 559<span class="hljs-keyword">const</span> channelsByCollection = diagnostics_channel.<span class="hljs-title function_">tracingChannel</span>({ 560 <span class="hljs-attr">start</span>: diagnostics_channel.<span class="hljs-title function_">channel</span>(<span class="hljs-string">'tracing:my-channel:start'</span>), 561 <span class="hljs-attr">end</span>: diagnostics_channel.<span class="hljs-title function_">channel</span>(<span class="hljs-string">'tracing:my-channel:end'</span>), 562 <span class="hljs-attr">asyncStart</span>: diagnostics_channel.<span class="hljs-title function_">channel</span>(<span class="hljs-string">'tracing:my-channel:asyncStart'</span>), 563 <span class="hljs-attr">asyncEnd</span>: diagnostics_channel.<span class="hljs-title function_">channel</span>(<span class="hljs-string">'tracing:my-channel:asyncEnd'</span>), 564 <span class="hljs-attr">error</span>: diagnostics_channel.<span class="hljs-title function_">channel</span>(<span class="hljs-string">'tracing:my-channel:error'</span>), 565});</code><button class="copy-button">copy</button></pre> 566<h4>Class: <code>Channel</code><span><a class="mark" href="#class-channel" id="class-channel">#</a></span><a aria-hidden="true" class="legacy" id="diagnostics_channel_class_channel"></a></h4> 567<div class="api_metadata"> 568<span>Added in: v15.1.0, v14.17.0</span> 569</div> 570<p>The class <code>Channel</code> represents an individual named channel within the data 571pipeline. It is used to track subscribers and to publish messages when there 572are subscribers present. It exists as a separate object to avoid channel 573lookups at publish time, enabling very fast publish speeds and allowing 574for heavy use while incurring very minimal cost. Channels are created with 575<a href="#diagnostics_channelchannelname"><code>diagnostics_channel.channel(name)</code></a>, constructing a channel directly 576with <code>new Channel(name)</code> is not supported.</p> 577<h5><code>channel.hasSubscribers</code><span><a class="mark" href="#channelhassubscribers" id="channelhassubscribers">#</a></span><a aria-hidden="true" class="legacy" id="diagnostics_channel_channel_hassubscribers"></a></h5> 578<div class="api_metadata"> 579<span>Added in: v15.1.0, v14.17.0</span> 580</div> 581<ul> 582<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> If there are active subscribers</li> 583</ul> 584<p>Check if there are active subscribers to this channel. This is helpful if 585the message you want to send might be expensive to prepare.</p> 586<p>This API is optional but helpful when trying to publish messages from very 587performance-sensitive code.</p> 588 589<pre class="with-64-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> diagnostics_channel <span class="hljs-keyword">from</span> <span class="hljs-string">'node:diagnostics_channel'</span>; 590 591<span class="hljs-keyword">const</span> channel = diagnostics_channel.<span class="hljs-title function_">channel</span>(<span class="hljs-string">'my-channel'</span>); 592 593<span class="hljs-keyword">if</span> (channel.<span class="hljs-property">hasSubscribers</span>) { 594 <span class="hljs-comment">// There are subscribers, prepare and publish message</span> 595}</code><code class="language-js cjs"><span class="hljs-keyword">const</span> diagnostics_channel = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:diagnostics_channel'</span>); 596 597<span class="hljs-keyword">const</span> channel = diagnostics_channel.<span class="hljs-title function_">channel</span>(<span class="hljs-string">'my-channel'</span>); 598 599<span class="hljs-keyword">if</span> (channel.<span class="hljs-property">hasSubscribers</span>) { 600 <span class="hljs-comment">// There are subscribers, prepare and publish message</span> 601}</code><button class="copy-button">copy</button></pre> 602<h5><code>channel.publish(message)</code><span><a class="mark" href="#channelpublishmessage" id="channelpublishmessage">#</a></span><a aria-hidden="true" class="legacy" id="diagnostics_channel_channel_publish_message"></a></h5> 603<div class="api_metadata"> 604<span>Added in: v15.1.0, v14.17.0</span> 605</div> 606<ul> 607<li><code>message</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a> The message to send to the channel subscribers</li> 608</ul> 609<p>Publish a message to any subscribers to the channel. This will trigger 610message handlers synchronously so they will execute within the same context.</p> 611 612<pre class="with-64-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> diagnostics_channel <span class="hljs-keyword">from</span> <span class="hljs-string">'node:diagnostics_channel'</span>; 613 614<span class="hljs-keyword">const</span> channel = diagnostics_channel.<span class="hljs-title function_">channel</span>(<span class="hljs-string">'my-channel'</span>); 615 616channel.<span class="hljs-title function_">publish</span>({ 617 <span class="hljs-attr">some</span>: <span class="hljs-string">'message'</span>, 618});</code><code class="language-js cjs"><span class="hljs-keyword">const</span> diagnostics_channel = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:diagnostics_channel'</span>); 619 620<span class="hljs-keyword">const</span> channel = diagnostics_channel.<span class="hljs-title function_">channel</span>(<span class="hljs-string">'my-channel'</span>); 621 622channel.<span class="hljs-title function_">publish</span>({ 623 <span class="hljs-attr">some</span>: <span class="hljs-string">'message'</span>, 624});</code><button class="copy-button">copy</button></pre> 625<h5><code>channel.subscribe(onMessage)</code><span><a class="mark" href="#channelsubscribeonmessage" id="channelsubscribeonmessage">#</a></span><a aria-hidden="true" class="legacy" id="diagnostics_channel_channel_subscribe_onmessage"></a></h5> 626<div class="api_metadata"> 627<span>Added in: v15.1.0, v14.17.0</span><span>Deprecated since: v18.7.0</span> 628</div> 629<p></p><div class="api_stability api_stability_0"><a href="documentation.html#stability-index">Stability: 0</a> - Deprecated: Use <a href="#diagnostics_channelsubscribename-onmessage"><code>diagnostics_channel.subscribe(name, onMessage)</code></a></div><p></p> 630<ul> 631<li><code>onMessage</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> The handler to receive channel messages 632<ul> 633<li><code>message</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a> The message data</li> 634<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Symbol_type" class="type"><symbol></a> The name of the channel</li> 635</ul> 636</li> 637</ul> 638<p>Register a message handler to subscribe to this channel. This message handler 639will be run synchronously whenever a message is published to the channel. Any 640errors thrown in the message handler will trigger an <a href="process.html#event-uncaughtexception"><code>'uncaughtException'</code></a>.</p> 641 642<pre class="with-64-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> diagnostics_channel <span class="hljs-keyword">from</span> <span class="hljs-string">'node:diagnostics_channel'</span>; 643 644<span class="hljs-keyword">const</span> channel = diagnostics_channel.<span class="hljs-title function_">channel</span>(<span class="hljs-string">'my-channel'</span>); 645 646channel.<span class="hljs-title function_">subscribe</span>(<span class="hljs-function">(<span class="hljs-params">message, name</span>) =></span> { 647 <span class="hljs-comment">// Received data</span> 648});</code><code class="language-js cjs"><span class="hljs-keyword">const</span> diagnostics_channel = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:diagnostics_channel'</span>); 649 650<span class="hljs-keyword">const</span> channel = diagnostics_channel.<span class="hljs-title function_">channel</span>(<span class="hljs-string">'my-channel'</span>); 651 652channel.<span class="hljs-title function_">subscribe</span>(<span class="hljs-function">(<span class="hljs-params">message, name</span>) =></span> { 653 <span class="hljs-comment">// Received data</span> 654});</code><button class="copy-button">copy</button></pre> 655<h5><code>channel.unsubscribe(onMessage)</code><span><a class="mark" href="#channelunsubscribeonmessage" id="channelunsubscribeonmessage">#</a></span><a aria-hidden="true" class="legacy" id="diagnostics_channel_channel_unsubscribe_onmessage"></a></h5> 656<div class="api_metadata"> 657<details class="changelog"><summary>History</summary> 658<table> 659<tbody><tr><th>Version</th><th>Changes</th></tr> 660<tr><td>v18.7.0</td> 661<td><p><span>Deprecated since: v18.7.0</span></p></td></tr> 662<tr><td>v17.1.0, v16.14.0, v14.19.0</td> 663<td><p>Added return value. Added to channels without subscribers.</p></td></tr> 664<tr><td>v15.1.0, v14.17.0</td> 665<td><p><span>Added in: v15.1.0, v14.17.0</span></p></td></tr> 666</tbody></table> 667</details> 668</div> 669<p></p><div class="api_stability api_stability_0"><a href="documentation.html#stability-index">Stability: 0</a> - Deprecated: Use <a href="#diagnostics_channelunsubscribename-onmessage"><code>diagnostics_channel.unsubscribe(name, onMessage)</code></a></div><p></p> 670<ul> 671<li><code>onMessage</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> The previous subscribed handler to remove</li> 672<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <code>true</code> if the handler was found, <code>false</code> otherwise.</li> 673</ul> 674<p>Remove a message handler previously registered to this channel with 675<a href="#channelsubscribeonmessage"><code>channel.subscribe(onMessage)</code></a>.</p> 676 677<pre class="with-64-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> diagnostics_channel <span class="hljs-keyword">from</span> <span class="hljs-string">'node:diagnostics_channel'</span>; 678 679<span class="hljs-keyword">const</span> channel = diagnostics_channel.<span class="hljs-title function_">channel</span>(<span class="hljs-string">'my-channel'</span>); 680 681<span class="hljs-keyword">function</span> <span class="hljs-title function_">onMessage</span>(<span class="hljs-params">message, name</span>) { 682 <span class="hljs-comment">// Received data</span> 683} 684 685channel.<span class="hljs-title function_">subscribe</span>(onMessage); 686 687channel.<span class="hljs-title function_">unsubscribe</span>(onMessage);</code><code class="language-js cjs"><span class="hljs-keyword">const</span> diagnostics_channel = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:diagnostics_channel'</span>); 688 689<span class="hljs-keyword">const</span> channel = diagnostics_channel.<span class="hljs-title function_">channel</span>(<span class="hljs-string">'my-channel'</span>); 690 691<span class="hljs-keyword">function</span> <span class="hljs-title function_">onMessage</span>(<span class="hljs-params">message, name</span>) { 692 <span class="hljs-comment">// Received data</span> 693} 694 695channel.<span class="hljs-title function_">subscribe</span>(onMessage); 696 697channel.<span class="hljs-title function_">unsubscribe</span>(onMessage);</code><button class="copy-button">copy</button></pre> 698<h5><code>channel.bindStore(store[, transform])</code><span><a class="mark" href="#channelbindstorestore-transform" id="channelbindstorestore-transform">#</a></span><a aria-hidden="true" class="legacy" id="diagnostics_channel_channel_bindstore_store_transform"></a></h5> 699<div class="api_metadata"> 700<span>Added in: v18.19.0</span> 701</div> 702<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p> 703<ul> 704<li><code>store</code> <a href="async_context.html#class-asynclocalstorage" class="type"><AsyncLocalStorage></a> The store to which to bind the context data</li> 705<li><code>transform</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> Transform context data before setting the store context</li> 706</ul> 707<p>When <a href="#channelrunstorescontext-fn-thisarg-args"><code>channel.runStores(context, ...)</code></a> is called, the given context data 708will be applied to any store bound to the channel. If the store has already been 709bound the previous <code>transform</code> function will be replaced with the new one. 710The <code>transform</code> function may be omitted to set the given context data as the 711context directly.</p> 712 713<pre class="with-64-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> diagnostics_channel <span class="hljs-keyword">from</span> <span class="hljs-string">'node:diagnostics_channel'</span>; 714<span class="hljs-keyword">import</span> { <span class="hljs-title class_">AsyncLocalStorage</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:async_hooks'</span>; 715 716<span class="hljs-keyword">const</span> store = <span class="hljs-keyword">new</span> <span class="hljs-title class_">AsyncLocalStorage</span>(); 717 718<span class="hljs-keyword">const</span> channel = diagnostics_channel.<span class="hljs-title function_">channel</span>(<span class="hljs-string">'my-channel'</span>); 719 720channel.<span class="hljs-title function_">bindStore</span>(store, <span class="hljs-function">(<span class="hljs-params">data</span>) =></span> { 721 <span class="hljs-keyword">return</span> { data }; 722});</code><code class="language-js cjs"><span class="hljs-keyword">const</span> diagnostics_channel = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:diagnostics_channel'</span>); 723<span class="hljs-keyword">const</span> { <span class="hljs-title class_">AsyncLocalStorage</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:async_hooks'</span>); 724 725<span class="hljs-keyword">const</span> store = <span class="hljs-keyword">new</span> <span class="hljs-title class_">AsyncLocalStorage</span>(); 726 727<span class="hljs-keyword">const</span> channel = diagnostics_channel.<span class="hljs-title function_">channel</span>(<span class="hljs-string">'my-channel'</span>); 728 729channel.<span class="hljs-title function_">bindStore</span>(store, <span class="hljs-function">(<span class="hljs-params">data</span>) =></span> { 730 <span class="hljs-keyword">return</span> { data }; 731});</code><button class="copy-button">copy</button></pre> 732<h5><code>channel.unbindStore(store)</code><span><a class="mark" href="#channelunbindstorestore" id="channelunbindstorestore">#</a></span><a aria-hidden="true" class="legacy" id="diagnostics_channel_channel_unbindstore_store"></a></h5> 733<div class="api_metadata"> 734<span>Added in: v18.19.0</span> 735</div> 736<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p> 737<ul> 738<li><code>store</code> <a href="async_context.html#class-asynclocalstorage" class="type"><AsyncLocalStorage></a> The store to unbind from the channel.</li> 739<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <code>true</code> if the store was found, <code>false</code> otherwise.</li> 740</ul> 741<p>Remove a message handler previously registered to this channel with 742<a href="#channelbindstorestore-transform"><code>channel.bindStore(store)</code></a>.</p> 743 744<pre class="with-64-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> diagnostics_channel <span class="hljs-keyword">from</span> <span class="hljs-string">'node:diagnostics_channel'</span>; 745<span class="hljs-keyword">import</span> { <span class="hljs-title class_">AsyncLocalStorage</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:async_hooks'</span>; 746 747<span class="hljs-keyword">const</span> store = <span class="hljs-keyword">new</span> <span class="hljs-title class_">AsyncLocalStorage</span>(); 748 749<span class="hljs-keyword">const</span> channel = diagnostics_channel.<span class="hljs-title function_">channel</span>(<span class="hljs-string">'my-channel'</span>); 750 751channel.<span class="hljs-title function_">bindStore</span>(store); 752channel.<span class="hljs-title function_">unbindStore</span>(store);</code><code class="language-js cjs"><span class="hljs-keyword">const</span> diagnostics_channel = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:diagnostics_channel'</span>); 753<span class="hljs-keyword">const</span> { <span class="hljs-title class_">AsyncLocalStorage</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:async_hooks'</span>); 754 755<span class="hljs-keyword">const</span> store = <span class="hljs-keyword">new</span> <span class="hljs-title class_">AsyncLocalStorage</span>(); 756 757<span class="hljs-keyword">const</span> channel = diagnostics_channel.<span class="hljs-title function_">channel</span>(<span class="hljs-string">'my-channel'</span>); 758 759channel.<span class="hljs-title function_">bindStore</span>(store); 760channel.<span class="hljs-title function_">unbindStore</span>(store);</code><button class="copy-button">copy</button></pre> 761<h5><code>channel.runStores(context, fn[, thisArg[, ...args]])</code><span><a class="mark" href="#channelrunstorescontext-fn-thisarg-args" id="channelrunstorescontext-fn-thisarg-args">#</a></span><a aria-hidden="true" class="legacy" id="diagnostics_channel_channel_runstores_context_fn_thisarg_args"></a></h5> 762<div class="api_metadata"> 763<span>Added in: v18.19.0</span> 764</div> 765<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p> 766<ul> 767<li><code>context</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a> Message to send to subscribers and bind to stores</li> 768<li><code>fn</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> Handler to run within the entered storage context</li> 769<li><code>thisArg</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a> The receiver to be used for the function call.</li> 770<li><code>...args</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a> Optional arguments to pass to the function.</li> 771</ul> 772<p>Applies the given data to any AsyncLocalStorage instances bound to the channel 773for the duration of the given function, then publishes to the channel within 774the scope of that data is applied to the stores.</p> 775<p>If a transform function was given to <a href="#channelbindstorestore-transform"><code>channel.bindStore(store)</code></a> it will be 776applied to transform the message data before it becomes the context value for 777the store. The prior storage context is accessible from within the transform 778function in cases where context linking is required.</p> 779<p>The context applied to the store should be accesible in any async code which 780continues from execution which began during the given function, however 781there are some situations in which <a href="async_context.html#troubleshooting-context-loss">context loss</a> may occur.</p> 782 783<pre class="with-64-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> diagnostics_channel <span class="hljs-keyword">from</span> <span class="hljs-string">'node:diagnostics_channel'</span>; 784<span class="hljs-keyword">import</span> { <span class="hljs-title class_">AsyncLocalStorage</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:async_hooks'</span>; 785 786<span class="hljs-keyword">const</span> store = <span class="hljs-keyword">new</span> <span class="hljs-title class_">AsyncLocalStorage</span>(); 787 788<span class="hljs-keyword">const</span> channel = diagnostics_channel.<span class="hljs-title function_">channel</span>(<span class="hljs-string">'my-channel'</span>); 789 790channel.<span class="hljs-title function_">bindStore</span>(store, <span class="hljs-function">(<span class="hljs-params">message</span>) =></span> { 791 <span class="hljs-keyword">const</span> parent = store.<span class="hljs-title function_">getStore</span>(); 792 <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Span</span>(message, parent); 793}); 794channel.<span class="hljs-title function_">runStores</span>({ <span class="hljs-attr">some</span>: <span class="hljs-string">'message'</span> }, <span class="hljs-function">() =></span> { 795 store.<span class="hljs-title function_">getStore</span>(); <span class="hljs-comment">// Span({ some: 'message' })</span> 796});</code><code class="language-js cjs"><span class="hljs-keyword">const</span> diagnostics_channel = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:diagnostics_channel'</span>); 797<span class="hljs-keyword">const</span> { <span class="hljs-title class_">AsyncLocalStorage</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:async_hooks'</span>); 798 799<span class="hljs-keyword">const</span> store = <span class="hljs-keyword">new</span> <span class="hljs-title class_">AsyncLocalStorage</span>(); 800 801<span class="hljs-keyword">const</span> channel = diagnostics_channel.<span class="hljs-title function_">channel</span>(<span class="hljs-string">'my-channel'</span>); 802 803channel.<span class="hljs-title function_">bindStore</span>(store, <span class="hljs-function">(<span class="hljs-params">message</span>) =></span> { 804 <span class="hljs-keyword">const</span> parent = store.<span class="hljs-title function_">getStore</span>(); 805 <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Span</span>(message, parent); 806}); 807channel.<span class="hljs-title function_">runStores</span>({ <span class="hljs-attr">some</span>: <span class="hljs-string">'message'</span> }, <span class="hljs-function">() =></span> { 808 store.<span class="hljs-title function_">getStore</span>(); <span class="hljs-comment">// Span({ some: 'message' })</span> 809});</code><button class="copy-button">copy</button></pre> 810<h4>Class: <code>TracingChannel</code><span><a class="mark" href="#class-tracingchannel" id="class-tracingchannel">#</a></span><a aria-hidden="true" class="legacy" id="diagnostics_channel_class_tracingchannel"></a></h4> 811<div class="api_metadata"> 812<span>Added in: v18.19.0</span> 813</div> 814<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p> 815<p>The class <code>TracingChannel</code> is a collection of <a href="#tracingchannel-channels">TracingChannel Channels</a> which 816together express a single traceable action. It is used to formalize and 817simplify the process of producing events for tracing application flow. 818<a href="#diagnostics_channeltracingchannelnameorchannels"><code>diagnostics_channel.tracingChannel()</code></a> is used to construct a 819<code>TracingChannel</code>. As with <code>Channel</code> it is recommended to create and reuse a 820single <code>TracingChannel</code> at the top-level of the file rather than creating them 821dynamically.</p> 822<h5><code>tracingChannel.subscribe(subscribers)</code><span><a class="mark" href="#tracingchannelsubscribesubscribers" id="tracingchannelsubscribesubscribers">#</a></span><a aria-hidden="true" class="legacy" id="diagnostics_channel_tracingchannel_subscribe_subscribers"></a></h5> 823<div class="api_metadata"> 824<span>Added in: v18.19.0</span> 825</div> 826<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p> 827<ul> 828<li><code>subscribers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> Set of <a href="#tracingchannel-channels">TracingChannel Channels</a> subscribers 829<ul> 830<li><code>start</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> The <a href="#startevent"><code>start</code> event</a> subscriber</li> 831<li><code>end</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> The <a href="#endevent"><code>end</code> event</a> subscriber</li> 832<li><code>asyncStart</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> The <a href="#asyncstartevent"><code>asyncStart</code> event</a> subscriber</li> 833<li><code>asyncEnd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> The <a href="#asyncendevent"><code>asyncEnd</code> event</a> subscriber</li> 834<li><code>error</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> The <a href="#errorevent"><code>error</code> event</a> subscriber</li> 835</ul> 836</li> 837</ul> 838<p>Helper to subscribe a collection of functions to the corresponding channels. 839This is the same as calling <a href="#channelsubscribeonmessage"><code>channel.subscribe(onMessage)</code></a> on each channel 840individually.</p> 841 842<pre class="with-64-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> diagnostics_channel <span class="hljs-keyword">from</span> <span class="hljs-string">'node:diagnostics_channel'</span>; 843 844<span class="hljs-keyword">const</span> channels = diagnostics_channel.<span class="hljs-title function_">tracingChannel</span>(<span class="hljs-string">'my-channel'</span>); 845 846channels.<span class="hljs-title function_">subscribe</span>({ 847 <span class="hljs-title function_">start</span>(<span class="hljs-params">message</span>) { 848 <span class="hljs-comment">// Handle start message</span> 849 }, 850 <span class="hljs-title function_">end</span>(<span class="hljs-params">message</span>) { 851 <span class="hljs-comment">// Handle end message</span> 852 }, 853 <span class="hljs-title function_">asyncStart</span>(<span class="hljs-params">message</span>) { 854 <span class="hljs-comment">// Handle asyncStart message</span> 855 }, 856 <span class="hljs-title function_">asyncEnd</span>(<span class="hljs-params">message</span>) { 857 <span class="hljs-comment">// Handle asyncEnd message</span> 858 }, 859 <span class="hljs-title function_">error</span>(<span class="hljs-params">message</span>) { 860 <span class="hljs-comment">// Handle error message</span> 861 }, 862});</code><code class="language-js cjs"><span class="hljs-keyword">const</span> diagnostics_channel = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:diagnostics_channel'</span>); 863 864<span class="hljs-keyword">const</span> channels = diagnostics_channel.<span class="hljs-title function_">tracingChannel</span>(<span class="hljs-string">'my-channel'</span>); 865 866channels.<span class="hljs-title function_">subscribe</span>({ 867 <span class="hljs-title function_">start</span>(<span class="hljs-params">message</span>) { 868 <span class="hljs-comment">// Handle start message</span> 869 }, 870 <span class="hljs-title function_">end</span>(<span class="hljs-params">message</span>) { 871 <span class="hljs-comment">// Handle end message</span> 872 }, 873 <span class="hljs-title function_">asyncStart</span>(<span class="hljs-params">message</span>) { 874 <span class="hljs-comment">// Handle asyncStart message</span> 875 }, 876 <span class="hljs-title function_">asyncEnd</span>(<span class="hljs-params">message</span>) { 877 <span class="hljs-comment">// Handle asyncEnd message</span> 878 }, 879 <span class="hljs-title function_">error</span>(<span class="hljs-params">message</span>) { 880 <span class="hljs-comment">// Handle error message</span> 881 }, 882});</code><button class="copy-button">copy</button></pre> 883<h5><code>tracingChannel.unsubscribe(subscribers)</code><span><a class="mark" href="#tracingchannelunsubscribesubscribers" id="tracingchannelunsubscribesubscribers">#</a></span><a aria-hidden="true" class="legacy" id="diagnostics_channel_tracingchannel_unsubscribe_subscribers"></a></h5> 884<div class="api_metadata"> 885<span>Added in: v18.19.0</span> 886</div> 887<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p> 888<ul> 889<li><code>subscribers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> Set of <a href="#tracingchannel-channels">TracingChannel Channels</a> subscribers 890<ul> 891<li><code>start</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> The <a href="#startevent"><code>start</code> event</a> subscriber</li> 892<li><code>end</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> The <a href="#endevent"><code>end</code> event</a> subscriber</li> 893<li><code>asyncStart</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> The <a href="#asyncstartevent"><code>asyncStart</code> event</a> subscriber</li> 894<li><code>asyncEnd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> The <a href="#asyncendevent"><code>asyncEnd</code> event</a> subscriber</li> 895<li><code>error</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> The <a href="#errorevent"><code>error</code> event</a> subscriber</li> 896</ul> 897</li> 898<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <code>true</code> if all handlers were successfully unsubscribed, 899and <code>false</code> otherwise.</li> 900</ul> 901<p>Helper to unsubscribe a collection of functions from the corresponding channels. 902This is the same as calling <a href="#channelunsubscribeonmessage"><code>channel.unsubscribe(onMessage)</code></a> on each channel 903individually.</p> 904 905<pre class="with-64-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> diagnostics_channel <span class="hljs-keyword">from</span> <span class="hljs-string">'node:diagnostics_channel'</span>; 906 907<span class="hljs-keyword">const</span> channels = diagnostics_channel.<span class="hljs-title function_">tracingChannel</span>(<span class="hljs-string">'my-channel'</span>); 908 909channels.<span class="hljs-title function_">unsubscribe</span>({ 910 <span class="hljs-title function_">start</span>(<span class="hljs-params">message</span>) { 911 <span class="hljs-comment">// Handle start message</span> 912 }, 913 <span class="hljs-title function_">end</span>(<span class="hljs-params">message</span>) { 914 <span class="hljs-comment">// Handle end message</span> 915 }, 916 <span class="hljs-title function_">asyncStart</span>(<span class="hljs-params">message</span>) { 917 <span class="hljs-comment">// Handle asyncStart message</span> 918 }, 919 <span class="hljs-title function_">asyncEnd</span>(<span class="hljs-params">message</span>) { 920 <span class="hljs-comment">// Handle asyncEnd message</span> 921 }, 922 <span class="hljs-title function_">error</span>(<span class="hljs-params">message</span>) { 923 <span class="hljs-comment">// Handle error message</span> 924 }, 925});</code><code class="language-js cjs"><span class="hljs-keyword">const</span> diagnostics_channel = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:diagnostics_channel'</span>); 926 927<span class="hljs-keyword">const</span> channels = diagnostics_channel.<span class="hljs-title function_">tracingChannel</span>(<span class="hljs-string">'my-channel'</span>); 928 929channels.<span class="hljs-title function_">unsubscribe</span>({ 930 <span class="hljs-title function_">start</span>(<span class="hljs-params">message</span>) { 931 <span class="hljs-comment">// Handle start message</span> 932 }, 933 <span class="hljs-title function_">end</span>(<span class="hljs-params">message</span>) { 934 <span class="hljs-comment">// Handle end message</span> 935 }, 936 <span class="hljs-title function_">asyncStart</span>(<span class="hljs-params">message</span>) { 937 <span class="hljs-comment">// Handle asyncStart message</span> 938 }, 939 <span class="hljs-title function_">asyncEnd</span>(<span class="hljs-params">message</span>) { 940 <span class="hljs-comment">// Handle asyncEnd message</span> 941 }, 942 <span class="hljs-title function_">error</span>(<span class="hljs-params">message</span>) { 943 <span class="hljs-comment">// Handle error message</span> 944 }, 945});</code><button class="copy-button">copy</button></pre> 946<h5><code>tracingChannel.traceSync(fn[, context[, thisArg[, ...args]]])</code><span><a class="mark" href="#tracingchanneltracesyncfn-context-thisarg-args" id="tracingchanneltracesyncfn-context-thisarg-args">#</a></span><a aria-hidden="true" class="legacy" id="diagnostics_channel_tracingchannel_tracesync_fn_context_thisarg_args"></a></h5> 947<div class="api_metadata"> 948<span>Added in: v18.19.0</span> 949</div> 950<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p> 951<ul> 952<li><code>fn</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> Function to wrap a trace around</li> 953<li><code>context</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> Shared object to correlate events through</li> 954<li><code>thisArg</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a> The receiver to be used for the function call</li> 955<li><code>...args</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a> Optional arguments to pass to the function</li> 956<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a> The return value of the given function</li> 957</ul> 958<p>Trace a synchronous function call. This will always produce a <a href="#startevent"><code>start</code> event</a> 959and <a href="#endevent"><code>end</code> event</a> around the execution and may produce an <a href="#errorevent"><code>error</code> event</a> 960if the given function throws an error. This will run the given function using 961<a href="#channelrunstorescontext-fn-thisarg-args"><code>channel.runStores(context, ...)</code></a> on the <code>start</code> channel which ensures all 962events should have any bound stores set to match this trace context.</p> 963 964<pre class="with-64-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> diagnostics_channel <span class="hljs-keyword">from</span> <span class="hljs-string">'node:diagnostics_channel'</span>; 965 966<span class="hljs-keyword">const</span> channels = diagnostics_channel.<span class="hljs-title function_">tracingChannel</span>(<span class="hljs-string">'my-channel'</span>); 967 968channels.<span class="hljs-title function_">traceSync</span>(<span class="hljs-function">() =></span> { 969 <span class="hljs-comment">// Do something</span> 970}, { 971 <span class="hljs-attr">some</span>: <span class="hljs-string">'thing'</span>, 972});</code><code class="language-js cjs"><span class="hljs-keyword">const</span> diagnostics_channel = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:diagnostics_channel'</span>); 973 974<span class="hljs-keyword">const</span> channels = diagnostics_channel.<span class="hljs-title function_">tracingChannel</span>(<span class="hljs-string">'my-channel'</span>); 975 976channels.<span class="hljs-title function_">traceSync</span>(<span class="hljs-function">() =></span> { 977 <span class="hljs-comment">// Do something</span> 978}, { 979 <span class="hljs-attr">some</span>: <span class="hljs-string">'thing'</span>, 980});</code><button class="copy-button">copy</button></pre> 981<h5><code>tracingChannel.tracePromise(fn[, context[, thisArg[, ...args]]])</code><span><a class="mark" href="#tracingchanneltracepromisefn-context-thisarg-args" id="tracingchanneltracepromisefn-context-thisarg-args">#</a></span><a aria-hidden="true" class="legacy" id="diagnostics_channel_tracingchannel_tracepromise_fn_context_thisarg_args"></a></h5> 982<div class="api_metadata"> 983<span>Added in: v18.19.0</span> 984</div> 985<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p> 986<ul> 987<li><code>fn</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> Promise-returning function to wrap a trace around</li> 988<li><code>context</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> Shared object to correlate trace events through</li> 989<li><code>thisArg</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a> The receiver to be used for the function call</li> 990<li><code>...args</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a> Optional arguments to pass to the function</li> 991<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a> Chained from promise returned by the given function</li> 992</ul> 993<p>Trace a promise-returning function call. This will always produce a 994<a href="#startevent"><code>start</code> event</a> and <a href="#endevent"><code>end</code> event</a> around the synchronous portion of the 995function execution, and will produce an <a href="#asyncstartevent"><code>asyncStart</code> event</a> and 996<a href="#asyncendevent"><code>asyncEnd</code> event</a> when a promise continuation is reached. It may also 997produce an <a href="#errorevent"><code>error</code> event</a> if the given function throws an error or the 998returned promise rejects. This will run the given function using 999<a href="#channelrunstorescontext-fn-thisarg-args"><code>channel.runStores(context, ...)</code></a> on the <code>start</code> channel which ensures all 1000events should have any bound stores set to match this trace context.</p> 1001 1002<pre class="with-64-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> diagnostics_channel <span class="hljs-keyword">from</span> <span class="hljs-string">'node:diagnostics_channel'</span>; 1003 1004<span class="hljs-keyword">const</span> channels = diagnostics_channel.<span class="hljs-title function_">tracingChannel</span>(<span class="hljs-string">'my-channel'</span>); 1005 1006channels.<span class="hljs-title function_">tracePromise</span>(<span class="hljs-keyword">async</span> () => { 1007 <span class="hljs-comment">// Do something</span> 1008}, { 1009 <span class="hljs-attr">some</span>: <span class="hljs-string">'thing'</span>, 1010});</code><code class="language-js cjs"><span class="hljs-keyword">const</span> diagnostics_channel = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:diagnostics_channel'</span>); 1011 1012<span class="hljs-keyword">const</span> channels = diagnostics_channel.<span class="hljs-title function_">tracingChannel</span>(<span class="hljs-string">'my-channel'</span>); 1013 1014channels.<span class="hljs-title function_">tracePromise</span>(<span class="hljs-keyword">async</span> () => { 1015 <span class="hljs-comment">// Do something</span> 1016}, { 1017 <span class="hljs-attr">some</span>: <span class="hljs-string">'thing'</span>, 1018});</code><button class="copy-button">copy</button></pre> 1019<h5><code>tracingChannel.traceCallback(fn[, position[, context[, thisArg[, ...args]]]])</code><span><a class="mark" href="#tracingchanneltracecallbackfn-position-context-thisarg-args" id="tracingchanneltracecallbackfn-position-context-thisarg-args">#</a></span><a aria-hidden="true" class="legacy" id="diagnostics_channel_tracingchannel_tracecallback_fn_position_context_thisarg_args"></a></h5> 1020<div class="api_metadata"> 1021<span>Added in: v18.19.0</span> 1022</div> 1023<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p> 1024<ul> 1025<li><code>fn</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> callback using function to wrap a trace around</li> 1026<li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Zero-indexed argument position of expected callback</li> 1027<li><code>context</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> Shared object to correlate trace events through</li> 1028<li><code>thisArg</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a> The receiver to be used for the function call</li> 1029<li><code>...args</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a> Optional arguments to pass to the function</li> 1030<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a> The return value of the given function</li> 1031</ul> 1032<p>Trace a callback-receiving function call. This will always produce a 1033<a href="#startevent"><code>start</code> event</a> and <a href="#endevent"><code>end</code> event</a> around the synchronous portion of the 1034function execution, and will produce a <a href="#asyncstartevent"><code>asyncStart</code> event</a> and 1035<a href="#asyncendevent"><code>asyncEnd</code> event</a> around the callback execution. It may also produce an 1036<a href="#errorevent"><code>error</code> event</a> if the given function throws an error or the returned 1037promise rejects. This will run the given function using 1038<a href="#channelrunstorescontext-fn-thisarg-args"><code>channel.runStores(context, ...)</code></a> on the <code>start</code> channel which ensures all 1039events should have any bound stores set to match this trace context.</p> 1040<p>The <code>position</code> will be -1 by default to indicate the final argument should 1041be used as the callback.</p> 1042 1043<pre class="with-64-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> diagnostics_channel <span class="hljs-keyword">from</span> <span class="hljs-string">'node:diagnostics_channel'</span>; 1044 1045<span class="hljs-keyword">const</span> channels = diagnostics_channel.<span class="hljs-title function_">tracingChannel</span>(<span class="hljs-string">'my-channel'</span>); 1046 1047channels.<span class="hljs-title function_">traceCallback</span>(<span class="hljs-function">(<span class="hljs-params">arg1, callback</span>) =></span> { 1048 <span class="hljs-comment">// Do something</span> 1049 <span class="hljs-title function_">callback</span>(<span class="hljs-literal">null</span>, <span class="hljs-string">'result'</span>); 1050}, <span class="hljs-number">1</span>, { 1051 <span class="hljs-attr">some</span>: <span class="hljs-string">'thing'</span>, 1052}, thisArg, arg1, callback);</code><code class="language-js cjs"><span class="hljs-keyword">const</span> diagnostics_channel = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:diagnostics_channel'</span>); 1053 1054<span class="hljs-keyword">const</span> channels = diagnostics_channel.<span class="hljs-title function_">tracingChannel</span>(<span class="hljs-string">'my-channel'</span>); 1055 1056channels.<span class="hljs-title function_">traceCallback</span>(<span class="hljs-function">(<span class="hljs-params">arg1, callback</span>) =></span> { 1057 <span class="hljs-comment">// Do something</span> 1058 <span class="hljs-title function_">callback</span>(<span class="hljs-literal">null</span>, <span class="hljs-string">'result'</span>); 1059}, { 1060 <span class="hljs-attr">some</span>: <span class="hljs-string">'thing'</span>, 1061}, thisArg, arg1, callback);</code><button class="copy-button">copy</button></pre> 1062<p>The callback will also be run with <a href="#channelrunstorescontext-fn-thisarg-args"><code>channel.runStores(context, ...)</code></a> which 1063enables context loss recovery in some cases.</p> 1064 1065<pre class="with-64-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> diagnostics_channel <span class="hljs-keyword">from</span> <span class="hljs-string">'node:diagnostics_channel'</span>; 1066<span class="hljs-keyword">import</span> { <span class="hljs-title class_">AsyncLocalStorage</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:async_hooks'</span>; 1067 1068<span class="hljs-keyword">const</span> channels = diagnostics_channel.<span class="hljs-title function_">tracingChannel</span>(<span class="hljs-string">'my-channel'</span>); 1069<span class="hljs-keyword">const</span> myStore = <span class="hljs-keyword">new</span> <span class="hljs-title class_">AsyncLocalStorage</span>(); 1070 1071<span class="hljs-comment">// The start channel sets the initial store data to something</span> 1072<span class="hljs-comment">// and stores that store data value on the trace context object</span> 1073channels.<span class="hljs-property">start</span>.<span class="hljs-title function_">bindStore</span>(myStore, <span class="hljs-function">(<span class="hljs-params">data</span>) =></span> { 1074 <span class="hljs-keyword">const</span> span = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Span</span>(data); 1075 data.<span class="hljs-property">span</span> = span; 1076 <span class="hljs-keyword">return</span> span; 1077}); 1078 1079<span class="hljs-comment">// Then asyncStart can restore from that data it stored previously</span> 1080channels.<span class="hljs-property">asyncStart</span>.<span class="hljs-title function_">bindStore</span>(myStore, <span class="hljs-function">(<span class="hljs-params">data</span>) =></span> { 1081 <span class="hljs-keyword">return</span> data.<span class="hljs-property">span</span>; 1082});</code><code class="language-js cjs"><span class="hljs-keyword">const</span> diagnostics_channel = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:diagnostics_channel'</span>); 1083<span class="hljs-keyword">const</span> { <span class="hljs-title class_">AsyncLocalStorage</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:async_hooks'</span>); 1084 1085<span class="hljs-keyword">const</span> channels = diagnostics_channel.<span class="hljs-title function_">tracingChannel</span>(<span class="hljs-string">'my-channel'</span>); 1086<span class="hljs-keyword">const</span> myStore = <span class="hljs-keyword">new</span> <span class="hljs-title class_">AsyncLocalStorage</span>(); 1087 1088<span class="hljs-comment">// The start channel sets the initial store data to something</span> 1089<span class="hljs-comment">// and stores that store data value on the trace context object</span> 1090channels.<span class="hljs-property">start</span>.<span class="hljs-title function_">bindStore</span>(myStore, <span class="hljs-function">(<span class="hljs-params">data</span>) =></span> { 1091 <span class="hljs-keyword">const</span> span = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Span</span>(data); 1092 data.<span class="hljs-property">span</span> = span; 1093 <span class="hljs-keyword">return</span> span; 1094}); 1095 1096<span class="hljs-comment">// Then asyncStart can restore from that data it stored previously</span> 1097channels.<span class="hljs-property">asyncStart</span>.<span class="hljs-title function_">bindStore</span>(myStore, <span class="hljs-function">(<span class="hljs-params">data</span>) =></span> { 1098 <span class="hljs-keyword">return</span> data.<span class="hljs-property">span</span>; 1099});</code><button class="copy-button">copy</button></pre> 1100<h4>TracingChannel Channels<span><a class="mark" href="#tracingchannel-channels" id="tracingchannel-channels">#</a></span><a aria-hidden="true" class="legacy" id="diagnostics_channel_tracingchannel_channels"></a></h4> 1101<p>A TracingChannel is a collection of several diagnostics_channels representing 1102specific points in the execution lifecycle of a single traceable action. The 1103behaviour is split into five diagnostics_channels consisting of <code>start</code>, 1104<code>end</code>, <code>asyncStart</code>, <code>asyncEnd</code>, and <code>error</code>. A single traceable action will 1105share the same event object between all events, this can be helpful for 1106managing correlation through a weakmap.</p> 1107<p>These event objects will be extended with <code>result</code> or <code>error</code> values when 1108the task "completes". In the case of a synchronous task the <code>result</code> will be 1109the return value and the <code>error</code> will be anything thrown from the function. 1110With callback-based async functions the <code>result</code> will be the second argument 1111of the callback while the <code>error</code> will either be a thrown error visible in the 1112<code>end</code> event or the first callback argument in either of the <code>asyncStart</code> or 1113<code>asyncEnd</code> events.</p> 1114<p>Tracing channels should follow a naming pattern of:</p> 1115<ul> 1116<li><code>tracing:module.class.method:start</code> or <code>tracing:module.function:start</code></li> 1117<li><code>tracing:module.class.method:end</code> or <code>tracing:module.function:end</code></li> 1118<li><code>tracing:module.class.method:asyncStart</code> or <code>tracing:module.function:asyncStart</code></li> 1119<li><code>tracing:module.class.method:asyncEnd</code> or <code>tracing:module.function:asyncEnd</code></li> 1120<li><code>tracing:module.class.method:error</code> or <code>tracing:module.function:error</code></li> 1121</ul> 1122<h5><code>start(event)</code><span><a class="mark" href="#startevent" id="startevent">#</a></span><a aria-hidden="true" class="legacy" id="diagnostics_channel_start_event"></a></h5> 1123<ul> 1124<li>Name: <code>tracing:${name}:start</code></li> 1125</ul> 1126<p>The <code>start</code> event represents the point at which a function is called. At this 1127point the event data may contain function arguments or anything else available 1128at the very start of the execution of the function.</p> 1129<h5><code>end(event)</code><span><a class="mark" href="#endevent" id="endevent">#</a></span><a aria-hidden="true" class="legacy" id="diagnostics_channel_end_event"></a></h5> 1130<ul> 1131<li>Name: <code>tracing:${name}:end</code></li> 1132</ul> 1133<p>The <code>end</code> event represents the point at which a function call returns a value. 1134In the case of an async function this is when the promise returned not when the 1135function itself makes a return statement internally. At this point, if the 1136traced function was synchronous the <code>result</code> field will be set to the return 1137value of the function. Alternatively, the <code>error</code> field may be present to 1138represent any thrown errors.</p> 1139<p>It is recommended to listen specifically to the <code>error</code> event to track errors 1140as it may be possible for a traceable action to produce multiple errors. For 1141example, an async task which fails may be started internally before the sync 1142part of the task then throws an error.</p> 1143<h5><code>asyncStart(event)</code><span><a class="mark" href="#asyncstartevent" id="asyncstartevent">#</a></span><a aria-hidden="true" class="legacy" id="diagnostics_channel_asyncstart_event"></a></h5> 1144<ul> 1145<li>Name: <code>tracing:${name}:asyncStart</code></li> 1146</ul> 1147<p>The <code>asyncStart</code> event represents the callback or continuation of a traceable 1148function being reached. At this point things like callback arguments may be 1149available, or anything else expressing the "result" of the action.</p> 1150<p>For callbacks-based functions, the first argument of the callback will be 1151assigned to the <code>error</code> field, if not <code>undefined</code> or <code>null</code>, and the second 1152argument will be assigned to the <code>result</code> field.</p> 1153<p>For promises, the argument to the <code>resolve</code> path will be assigned to <code>result</code> 1154or the argument to the <code>reject</code> path will be assign to <code>error</code>.</p> 1155<p>It is recommended to listen specifically to the <code>error</code> event to track errors 1156as it may be possible for a traceable action to produce multiple errors. For 1157example, an async task which fails may be started internally before the sync 1158part of the task then throws an error.</p> 1159<h5><code>asyncEnd(event)</code><span><a class="mark" href="#asyncendevent" id="asyncendevent">#</a></span><a aria-hidden="true" class="legacy" id="diagnostics_channel_asyncend_event"></a></h5> 1160<ul> 1161<li>Name: <code>tracing:${name}:asyncEnd</code></li> 1162</ul> 1163<p>The <code>asyncEnd</code> event represents the callback of an asynchronous function 1164returning. It's not likely event data will change after the <code>asyncStart</code> event, 1165however it may be useful to see the point where the callback completes.</p> 1166<h5><code>error(event)</code><span><a class="mark" href="#errorevent" id="errorevent">#</a></span><a aria-hidden="true" class="legacy" id="diagnostics_channel_error_event"></a></h5> 1167<ul> 1168<li>Name: <code>tracing:${name}:error</code></li> 1169</ul> 1170<p>The <code>error</code> event represents any error produced by the traceable function 1171either synchronously or asynchronously. If an error is thrown in the 1172synchronous portion of the traced function the error will be assigned to the 1173<code>error</code> field of the event and the <code>error</code> event will be triggered. If an error 1174is received asynchronously through a callback or promise rejection it will also 1175be assigned to the <code>error</code> field of the event and trigger the <code>error</code> event.</p> 1176<p>It is possible for a single traceable function call to produce errors multiple 1177times so this should be considered when consuming this event. For example, if 1178another async task is triggered internally which fails and then the sync part 1179of the function then throws and error two <code>error</code> events will be emitted, one 1180for the sync error and one for the async error.</p> 1181<h4>Built-in Channels<span><a class="mark" href="#built-in-channels" id="built-in-channels">#</a></span><a aria-hidden="true" class="legacy" id="diagnostics_channel_built_in_channels"></a></h4> 1182<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p> 1183<p>While the diagnostics_channel API is now considered stable, the built-in 1184channels currently available are not. Each channel must be declared stable 1185independently.</p> 1186<h5>HTTP<span><a class="mark" href="#http" id="http">#</a></span><a aria-hidden="true" class="legacy" id="diagnostics_channel_http"></a></h5> 1187<p><code>http.client.request.start</code></p> 1188<ul> 1189<li><code>request</code> <a href="http.html#class-httpclientrequest" class="type"><http.ClientRequest></a></li> 1190</ul> 1191<p>Emitted when client starts a request.</p> 1192<p><code>http.client.response.finish</code></p> 1193<ul> 1194<li><code>request</code> <a href="http.html#class-httpclientrequest" class="type"><http.ClientRequest></a></li> 1195<li><code>response</code> <a href="http.html#class-httpincomingmessage" class="type"><http.IncomingMessage></a></li> 1196</ul> 1197<p>Emitted when client receives a response.</p> 1198<p><code>http.server.request.start</code></p> 1199<ul> 1200<li><code>request</code> <a href="http.html#class-httpincomingmessage" class="type"><http.IncomingMessage></a></li> 1201<li><code>response</code> <a href="http.html#class-httpserverresponse" class="type"><http.ServerResponse></a></li> 1202<li><code>socket</code> <a href="net.html#class-netsocket" class="type"><net.Socket></a></li> 1203<li><code>server</code> <a href="http.html#class-httpserver" class="type"><http.Server></a></li> 1204</ul> 1205<p>Emitted when server receives a request.</p> 1206<p><code>http.server.response.finish</code></p> 1207<ul> 1208<li><code>request</code> <a href="http.html#class-httpincomingmessage" class="type"><http.IncomingMessage></a></li> 1209<li><code>response</code> <a href="http.html#class-httpserverresponse" class="type"><http.ServerResponse></a></li> 1210<li><code>socket</code> <a href="net.html#class-netsocket" class="type"><net.Socket></a></li> 1211<li><code>server</code> <a href="http.html#class-httpserver" class="type"><http.Server></a></li> 1212</ul> 1213<p>Emitted when server sends a response.</p> 1214<p><code>net.client.socket</code></p> 1215<ul> 1216<li><code>socket</code> <a href="net.html#class-netsocket" class="type"><net.Socket></a></li> 1217</ul> 1218<p>Emitted when a new TCP or pipe client socket is created.</p> 1219<p><code>net.server.socket</code></p> 1220<ul> 1221<li><code>socket</code> <a href="net.html#class-netsocket" class="type"><net.Socket></a></li> 1222</ul> 1223<p>Emitted when a new TCP or pipe connection is received.</p> 1224<p><code>udp.socket</code></p> 1225<ul> 1226<li><code>socket</code> <a href="dgram.html#class-dgramsocket" class="type"><dgram.Socket></a></li> 1227</ul> 1228<p>Emitted when a new UDP socket is created.</p></section> 1229 <!-- API END --> 1230 </div> 1231 </div> 1232 </div> 1233</body> 1234</html> 1235