11cb0ef41Sopenharmony_ci# Diagnostics Channel 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_ci<!-- YAML 41cb0ef41Sopenharmony_ciadded: 51cb0ef41Sopenharmony_ci - v15.1.0 61cb0ef41Sopenharmony_ci - v14.17.0 71cb0ef41Sopenharmony_cichanges: 81cb0ef41Sopenharmony_ci - version: v18.13.0 91cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/45290 101cb0ef41Sopenharmony_ci description: diagnostics_channel is now Stable. 111cb0ef41Sopenharmony_ci--> 121cb0ef41Sopenharmony_ci 131cb0ef41Sopenharmony_ci<!--introduced_in=v15.1.0--> 141cb0ef41Sopenharmony_ci 151cb0ef41Sopenharmony_ci> Stability: 2 - Stable 161cb0ef41Sopenharmony_ci 171cb0ef41Sopenharmony_ci<!-- source_link=lib/diagnostics_channel.js --> 181cb0ef41Sopenharmony_ci 191cb0ef41Sopenharmony_ciThe `node:diagnostics_channel` module provides an API to create named channels 201cb0ef41Sopenharmony_cito report arbitrary message data for diagnostics purposes. 211cb0ef41Sopenharmony_ci 221cb0ef41Sopenharmony_ciIt can be accessed using: 231cb0ef41Sopenharmony_ci 241cb0ef41Sopenharmony_ci```mjs 251cb0ef41Sopenharmony_ciimport diagnostics_channel from 'node:diagnostics_channel'; 261cb0ef41Sopenharmony_ci``` 271cb0ef41Sopenharmony_ci 281cb0ef41Sopenharmony_ci```cjs 291cb0ef41Sopenharmony_ciconst diagnostics_channel = require('node:diagnostics_channel'); 301cb0ef41Sopenharmony_ci``` 311cb0ef41Sopenharmony_ci 321cb0ef41Sopenharmony_ciIt is intended that a module writer wanting to report diagnostics messages 331cb0ef41Sopenharmony_ciwill create one or many top-level channels to report messages through. 341cb0ef41Sopenharmony_ciChannels may also be acquired at runtime but it is not encouraged 351cb0ef41Sopenharmony_cidue to the additional overhead of doing so. Channels may be exported for 361cb0ef41Sopenharmony_ciconvenience, but as long as the name is known it can be acquired anywhere. 371cb0ef41Sopenharmony_ci 381cb0ef41Sopenharmony_ciIf you intend for your module to produce diagnostics data for others to 391cb0ef41Sopenharmony_ciconsume it is recommended that you include documentation of what named 401cb0ef41Sopenharmony_cichannels are used along with the shape of the message data. Channel names 411cb0ef41Sopenharmony_cishould generally include the module name to avoid collisions with data from 421cb0ef41Sopenharmony_ciother modules. 431cb0ef41Sopenharmony_ci 441cb0ef41Sopenharmony_ci## Public API 451cb0ef41Sopenharmony_ci 461cb0ef41Sopenharmony_ci### Overview 471cb0ef41Sopenharmony_ci 481cb0ef41Sopenharmony_ciFollowing is a simple overview of the public API. 491cb0ef41Sopenharmony_ci 501cb0ef41Sopenharmony_ci```mjs 511cb0ef41Sopenharmony_ciimport diagnostics_channel from 'node:diagnostics_channel'; 521cb0ef41Sopenharmony_ci 531cb0ef41Sopenharmony_ci// Get a reusable channel object 541cb0ef41Sopenharmony_ciconst channel = diagnostics_channel.channel('my-channel'); 551cb0ef41Sopenharmony_ci 561cb0ef41Sopenharmony_cifunction onMessage(message, name) { 571cb0ef41Sopenharmony_ci // Received data 581cb0ef41Sopenharmony_ci} 591cb0ef41Sopenharmony_ci 601cb0ef41Sopenharmony_ci// Subscribe to the channel 611cb0ef41Sopenharmony_cidiagnostics_channel.subscribe('my-channel', onMessage); 621cb0ef41Sopenharmony_ci 631cb0ef41Sopenharmony_ci// Check if the channel has an active subscriber 641cb0ef41Sopenharmony_ciif (channel.hasSubscribers) { 651cb0ef41Sopenharmony_ci // Publish data to the channel 661cb0ef41Sopenharmony_ci channel.publish({ 671cb0ef41Sopenharmony_ci some: 'data', 681cb0ef41Sopenharmony_ci }); 691cb0ef41Sopenharmony_ci} 701cb0ef41Sopenharmony_ci 711cb0ef41Sopenharmony_ci// Unsubscribe from the channel 721cb0ef41Sopenharmony_cidiagnostics_channel.unsubscribe('my-channel', onMessage); 731cb0ef41Sopenharmony_ci``` 741cb0ef41Sopenharmony_ci 751cb0ef41Sopenharmony_ci```cjs 761cb0ef41Sopenharmony_ciconst diagnostics_channel = require('node:diagnostics_channel'); 771cb0ef41Sopenharmony_ci 781cb0ef41Sopenharmony_ci// Get a reusable channel object 791cb0ef41Sopenharmony_ciconst channel = diagnostics_channel.channel('my-channel'); 801cb0ef41Sopenharmony_ci 811cb0ef41Sopenharmony_cifunction onMessage(message, name) { 821cb0ef41Sopenharmony_ci // Received data 831cb0ef41Sopenharmony_ci} 841cb0ef41Sopenharmony_ci 851cb0ef41Sopenharmony_ci// Subscribe to the channel 861cb0ef41Sopenharmony_cidiagnostics_channel.subscribe('my-channel', onMessage); 871cb0ef41Sopenharmony_ci 881cb0ef41Sopenharmony_ci// Check if the channel has an active subscriber 891cb0ef41Sopenharmony_ciif (channel.hasSubscribers) { 901cb0ef41Sopenharmony_ci // Publish data to the channel 911cb0ef41Sopenharmony_ci channel.publish({ 921cb0ef41Sopenharmony_ci some: 'data', 931cb0ef41Sopenharmony_ci }); 941cb0ef41Sopenharmony_ci} 951cb0ef41Sopenharmony_ci 961cb0ef41Sopenharmony_ci// Unsubscribe from the channel 971cb0ef41Sopenharmony_cidiagnostics_channel.unsubscribe('my-channel', onMessage); 981cb0ef41Sopenharmony_ci``` 991cb0ef41Sopenharmony_ci 1001cb0ef41Sopenharmony_ci#### `diagnostics_channel.hasSubscribers(name)` 1011cb0ef41Sopenharmony_ci 1021cb0ef41Sopenharmony_ci<!-- YAML 1031cb0ef41Sopenharmony_ciadded: 1041cb0ef41Sopenharmony_ci - v15.1.0 1051cb0ef41Sopenharmony_ci - v14.17.0 1061cb0ef41Sopenharmony_ci--> 1071cb0ef41Sopenharmony_ci 1081cb0ef41Sopenharmony_ci* `name` {string|symbol} The channel name 1091cb0ef41Sopenharmony_ci* Returns: {boolean} If there are active subscribers 1101cb0ef41Sopenharmony_ci 1111cb0ef41Sopenharmony_ciCheck if there are active subscribers to the named channel. This is helpful if 1121cb0ef41Sopenharmony_cithe message you want to send might be expensive to prepare. 1131cb0ef41Sopenharmony_ci 1141cb0ef41Sopenharmony_ciThis API is optional but helpful when trying to publish messages from very 1151cb0ef41Sopenharmony_ciperformance-sensitive code. 1161cb0ef41Sopenharmony_ci 1171cb0ef41Sopenharmony_ci```mjs 1181cb0ef41Sopenharmony_ciimport diagnostics_channel from 'node:diagnostics_channel'; 1191cb0ef41Sopenharmony_ci 1201cb0ef41Sopenharmony_ciif (diagnostics_channel.hasSubscribers('my-channel')) { 1211cb0ef41Sopenharmony_ci // There are subscribers, prepare and publish message 1221cb0ef41Sopenharmony_ci} 1231cb0ef41Sopenharmony_ci``` 1241cb0ef41Sopenharmony_ci 1251cb0ef41Sopenharmony_ci```cjs 1261cb0ef41Sopenharmony_ciconst diagnostics_channel = require('node:diagnostics_channel'); 1271cb0ef41Sopenharmony_ci 1281cb0ef41Sopenharmony_ciif (diagnostics_channel.hasSubscribers('my-channel')) { 1291cb0ef41Sopenharmony_ci // There are subscribers, prepare and publish message 1301cb0ef41Sopenharmony_ci} 1311cb0ef41Sopenharmony_ci``` 1321cb0ef41Sopenharmony_ci 1331cb0ef41Sopenharmony_ci#### `diagnostics_channel.channel(name)` 1341cb0ef41Sopenharmony_ci 1351cb0ef41Sopenharmony_ci<!-- YAML 1361cb0ef41Sopenharmony_ciadded: 1371cb0ef41Sopenharmony_ci - v15.1.0 1381cb0ef41Sopenharmony_ci - v14.17.0 1391cb0ef41Sopenharmony_ci--> 1401cb0ef41Sopenharmony_ci 1411cb0ef41Sopenharmony_ci* `name` {string|symbol} The channel name 1421cb0ef41Sopenharmony_ci* Returns: {Channel} The named channel object 1431cb0ef41Sopenharmony_ci 1441cb0ef41Sopenharmony_ciThis is the primary entry-point for anyone wanting to publish to a named 1451cb0ef41Sopenharmony_cichannel. It produces a channel object which is optimized to reduce overhead at 1461cb0ef41Sopenharmony_cipublish time as much as possible. 1471cb0ef41Sopenharmony_ci 1481cb0ef41Sopenharmony_ci```mjs 1491cb0ef41Sopenharmony_ciimport diagnostics_channel from 'node:diagnostics_channel'; 1501cb0ef41Sopenharmony_ci 1511cb0ef41Sopenharmony_ciconst channel = diagnostics_channel.channel('my-channel'); 1521cb0ef41Sopenharmony_ci``` 1531cb0ef41Sopenharmony_ci 1541cb0ef41Sopenharmony_ci```cjs 1551cb0ef41Sopenharmony_ciconst diagnostics_channel = require('node:diagnostics_channel'); 1561cb0ef41Sopenharmony_ci 1571cb0ef41Sopenharmony_ciconst channel = diagnostics_channel.channel('my-channel'); 1581cb0ef41Sopenharmony_ci``` 1591cb0ef41Sopenharmony_ci 1601cb0ef41Sopenharmony_ci#### `diagnostics_channel.subscribe(name, onMessage)` 1611cb0ef41Sopenharmony_ci 1621cb0ef41Sopenharmony_ci<!-- YAML 1631cb0ef41Sopenharmony_ciadded: 1641cb0ef41Sopenharmony_ci - v18.7.0 1651cb0ef41Sopenharmony_ci--> 1661cb0ef41Sopenharmony_ci 1671cb0ef41Sopenharmony_ci* `name` {string|symbol} The channel name 1681cb0ef41Sopenharmony_ci* `onMessage` {Function} The handler to receive channel messages 1691cb0ef41Sopenharmony_ci * `message` {any} The message data 1701cb0ef41Sopenharmony_ci * `name` {string|symbol} The name of the channel 1711cb0ef41Sopenharmony_ci 1721cb0ef41Sopenharmony_ciRegister a message handler to subscribe to this channel. This message handler 1731cb0ef41Sopenharmony_ciwill be run synchronously whenever a message is published to the channel. Any 1741cb0ef41Sopenharmony_cierrors thrown in the message handler will trigger an [`'uncaughtException'`][]. 1751cb0ef41Sopenharmony_ci 1761cb0ef41Sopenharmony_ci```mjs 1771cb0ef41Sopenharmony_ciimport diagnostics_channel from 'node:diagnostics_channel'; 1781cb0ef41Sopenharmony_ci 1791cb0ef41Sopenharmony_cidiagnostics_channel.subscribe('my-channel', (message, name) => { 1801cb0ef41Sopenharmony_ci // Received data 1811cb0ef41Sopenharmony_ci}); 1821cb0ef41Sopenharmony_ci``` 1831cb0ef41Sopenharmony_ci 1841cb0ef41Sopenharmony_ci```cjs 1851cb0ef41Sopenharmony_ciconst diagnostics_channel = require('node:diagnostics_channel'); 1861cb0ef41Sopenharmony_ci 1871cb0ef41Sopenharmony_cidiagnostics_channel.subscribe('my-channel', (message, name) => { 1881cb0ef41Sopenharmony_ci // Received data 1891cb0ef41Sopenharmony_ci}); 1901cb0ef41Sopenharmony_ci``` 1911cb0ef41Sopenharmony_ci 1921cb0ef41Sopenharmony_ci#### `diagnostics_channel.unsubscribe(name, onMessage)` 1931cb0ef41Sopenharmony_ci 1941cb0ef41Sopenharmony_ci<!-- YAML 1951cb0ef41Sopenharmony_ciadded: 1961cb0ef41Sopenharmony_ci - v18.7.0 1971cb0ef41Sopenharmony_ci--> 1981cb0ef41Sopenharmony_ci 1991cb0ef41Sopenharmony_ci* `name` {string|symbol} The channel name 2001cb0ef41Sopenharmony_ci* `onMessage` {Function} The previous subscribed handler to remove 2011cb0ef41Sopenharmony_ci* Returns: {boolean} `true` if the handler was found, `false` otherwise. 2021cb0ef41Sopenharmony_ci 2031cb0ef41Sopenharmony_ciRemove a message handler previously registered to this channel with 2041cb0ef41Sopenharmony_ci[`diagnostics_channel.subscribe(name, onMessage)`][]. 2051cb0ef41Sopenharmony_ci 2061cb0ef41Sopenharmony_ci```mjs 2071cb0ef41Sopenharmony_ciimport diagnostics_channel from 'node:diagnostics_channel'; 2081cb0ef41Sopenharmony_ci 2091cb0ef41Sopenharmony_cifunction onMessage(message, name) { 2101cb0ef41Sopenharmony_ci // Received data 2111cb0ef41Sopenharmony_ci} 2121cb0ef41Sopenharmony_ci 2131cb0ef41Sopenharmony_cidiagnostics_channel.subscribe('my-channel', onMessage); 2141cb0ef41Sopenharmony_ci 2151cb0ef41Sopenharmony_cidiagnostics_channel.unsubscribe('my-channel', onMessage); 2161cb0ef41Sopenharmony_ci``` 2171cb0ef41Sopenharmony_ci 2181cb0ef41Sopenharmony_ci```cjs 2191cb0ef41Sopenharmony_ciconst diagnostics_channel = require('node:diagnostics_channel'); 2201cb0ef41Sopenharmony_ci 2211cb0ef41Sopenharmony_cifunction onMessage(message, name) { 2221cb0ef41Sopenharmony_ci // Received data 2231cb0ef41Sopenharmony_ci} 2241cb0ef41Sopenharmony_ci 2251cb0ef41Sopenharmony_cidiagnostics_channel.subscribe('my-channel', onMessage); 2261cb0ef41Sopenharmony_ci 2271cb0ef41Sopenharmony_cidiagnostics_channel.unsubscribe('my-channel', onMessage); 2281cb0ef41Sopenharmony_ci``` 2291cb0ef41Sopenharmony_ci 2301cb0ef41Sopenharmony_ci#### `diagnostics_channel.tracingChannel(nameOrChannels)` 2311cb0ef41Sopenharmony_ci 2321cb0ef41Sopenharmony_ci<!-- YAML 2331cb0ef41Sopenharmony_ciadded: 2341cb0ef41Sopenharmony_ci - v18.19.0 2351cb0ef41Sopenharmony_ci--> 2361cb0ef41Sopenharmony_ci 2371cb0ef41Sopenharmony_ci> Stability: 1 - Experimental 2381cb0ef41Sopenharmony_ci 2391cb0ef41Sopenharmony_ci* `nameOrChannels` {string|TracingChannel} Channel name or 2401cb0ef41Sopenharmony_ci object containing all the [TracingChannel Channels][] 2411cb0ef41Sopenharmony_ci* Returns: {TracingChannel} Collection of channels to trace with 2421cb0ef41Sopenharmony_ci 2431cb0ef41Sopenharmony_ciCreates a [`TracingChannel`][] wrapper for the given 2441cb0ef41Sopenharmony_ci[TracingChannel Channels][]. If a name is given, the corresponding tracing 2451cb0ef41Sopenharmony_cichannels will be created in the form of `tracing:${name}:${eventType}` where 2461cb0ef41Sopenharmony_ci`eventType` corresponds to the types of [TracingChannel Channels][]. 2471cb0ef41Sopenharmony_ci 2481cb0ef41Sopenharmony_ci```mjs 2491cb0ef41Sopenharmony_ciimport diagnostics_channel from 'node:diagnostics_channel'; 2501cb0ef41Sopenharmony_ci 2511cb0ef41Sopenharmony_ciconst channelsByName = diagnostics_channel.tracingChannel('my-channel'); 2521cb0ef41Sopenharmony_ci 2531cb0ef41Sopenharmony_ci// or... 2541cb0ef41Sopenharmony_ci 2551cb0ef41Sopenharmony_ciconst channelsByCollection = diagnostics_channel.tracingChannel({ 2561cb0ef41Sopenharmony_ci start: diagnostics_channel.channel('tracing:my-channel:start'), 2571cb0ef41Sopenharmony_ci end: diagnostics_channel.channel('tracing:my-channel:end'), 2581cb0ef41Sopenharmony_ci asyncStart: diagnostics_channel.channel('tracing:my-channel:asyncStart'), 2591cb0ef41Sopenharmony_ci asyncEnd: diagnostics_channel.channel('tracing:my-channel:asyncEnd'), 2601cb0ef41Sopenharmony_ci error: diagnostics_channel.channel('tracing:my-channel:error'), 2611cb0ef41Sopenharmony_ci}); 2621cb0ef41Sopenharmony_ci``` 2631cb0ef41Sopenharmony_ci 2641cb0ef41Sopenharmony_ci```cjs 2651cb0ef41Sopenharmony_ciconst diagnostics_channel = require('node:diagnostics_channel'); 2661cb0ef41Sopenharmony_ci 2671cb0ef41Sopenharmony_ciconst channelsByName = diagnostics_channel.tracingChannel('my-channel'); 2681cb0ef41Sopenharmony_ci 2691cb0ef41Sopenharmony_ci// or... 2701cb0ef41Sopenharmony_ci 2711cb0ef41Sopenharmony_ciconst channelsByCollection = diagnostics_channel.tracingChannel({ 2721cb0ef41Sopenharmony_ci start: diagnostics_channel.channel('tracing:my-channel:start'), 2731cb0ef41Sopenharmony_ci end: diagnostics_channel.channel('tracing:my-channel:end'), 2741cb0ef41Sopenharmony_ci asyncStart: diagnostics_channel.channel('tracing:my-channel:asyncStart'), 2751cb0ef41Sopenharmony_ci asyncEnd: diagnostics_channel.channel('tracing:my-channel:asyncEnd'), 2761cb0ef41Sopenharmony_ci error: diagnostics_channel.channel('tracing:my-channel:error'), 2771cb0ef41Sopenharmony_ci}); 2781cb0ef41Sopenharmony_ci``` 2791cb0ef41Sopenharmony_ci 2801cb0ef41Sopenharmony_ci### Class: `Channel` 2811cb0ef41Sopenharmony_ci 2821cb0ef41Sopenharmony_ci<!-- YAML 2831cb0ef41Sopenharmony_ciadded: 2841cb0ef41Sopenharmony_ci - v15.1.0 2851cb0ef41Sopenharmony_ci - v14.17.0 2861cb0ef41Sopenharmony_ci--> 2871cb0ef41Sopenharmony_ci 2881cb0ef41Sopenharmony_ciThe class `Channel` represents an individual named channel within the data 2891cb0ef41Sopenharmony_cipipeline. It is used to track subscribers and to publish messages when there 2901cb0ef41Sopenharmony_ciare subscribers present. It exists as a separate object to avoid channel 2911cb0ef41Sopenharmony_cilookups at publish time, enabling very fast publish speeds and allowing 2921cb0ef41Sopenharmony_cifor heavy use while incurring very minimal cost. Channels are created with 2931cb0ef41Sopenharmony_ci[`diagnostics_channel.channel(name)`][], constructing a channel directly 2941cb0ef41Sopenharmony_ciwith `new Channel(name)` is not supported. 2951cb0ef41Sopenharmony_ci 2961cb0ef41Sopenharmony_ci#### `channel.hasSubscribers` 2971cb0ef41Sopenharmony_ci 2981cb0ef41Sopenharmony_ci<!-- YAML 2991cb0ef41Sopenharmony_ciadded: 3001cb0ef41Sopenharmony_ci - v15.1.0 3011cb0ef41Sopenharmony_ci - v14.17.0 3021cb0ef41Sopenharmony_ci--> 3031cb0ef41Sopenharmony_ci 3041cb0ef41Sopenharmony_ci* Returns: {boolean} If there are active subscribers 3051cb0ef41Sopenharmony_ci 3061cb0ef41Sopenharmony_ciCheck if there are active subscribers to this channel. This is helpful if 3071cb0ef41Sopenharmony_cithe message you want to send might be expensive to prepare. 3081cb0ef41Sopenharmony_ci 3091cb0ef41Sopenharmony_ciThis API is optional but helpful when trying to publish messages from very 3101cb0ef41Sopenharmony_ciperformance-sensitive code. 3111cb0ef41Sopenharmony_ci 3121cb0ef41Sopenharmony_ci```mjs 3131cb0ef41Sopenharmony_ciimport diagnostics_channel from 'node:diagnostics_channel'; 3141cb0ef41Sopenharmony_ci 3151cb0ef41Sopenharmony_ciconst channel = diagnostics_channel.channel('my-channel'); 3161cb0ef41Sopenharmony_ci 3171cb0ef41Sopenharmony_ciif (channel.hasSubscribers) { 3181cb0ef41Sopenharmony_ci // There are subscribers, prepare and publish message 3191cb0ef41Sopenharmony_ci} 3201cb0ef41Sopenharmony_ci``` 3211cb0ef41Sopenharmony_ci 3221cb0ef41Sopenharmony_ci```cjs 3231cb0ef41Sopenharmony_ciconst diagnostics_channel = require('node:diagnostics_channel'); 3241cb0ef41Sopenharmony_ci 3251cb0ef41Sopenharmony_ciconst channel = diagnostics_channel.channel('my-channel'); 3261cb0ef41Sopenharmony_ci 3271cb0ef41Sopenharmony_ciif (channel.hasSubscribers) { 3281cb0ef41Sopenharmony_ci // There are subscribers, prepare and publish message 3291cb0ef41Sopenharmony_ci} 3301cb0ef41Sopenharmony_ci``` 3311cb0ef41Sopenharmony_ci 3321cb0ef41Sopenharmony_ci#### `channel.publish(message)` 3331cb0ef41Sopenharmony_ci 3341cb0ef41Sopenharmony_ci<!-- YAML 3351cb0ef41Sopenharmony_ciadded: 3361cb0ef41Sopenharmony_ci - v15.1.0 3371cb0ef41Sopenharmony_ci - v14.17.0 3381cb0ef41Sopenharmony_ci--> 3391cb0ef41Sopenharmony_ci 3401cb0ef41Sopenharmony_ci* `message` {any} The message to send to the channel subscribers 3411cb0ef41Sopenharmony_ci 3421cb0ef41Sopenharmony_ciPublish a message to any subscribers to the channel. This will trigger 3431cb0ef41Sopenharmony_cimessage handlers synchronously so they will execute within the same context. 3441cb0ef41Sopenharmony_ci 3451cb0ef41Sopenharmony_ci```mjs 3461cb0ef41Sopenharmony_ciimport diagnostics_channel from 'node:diagnostics_channel'; 3471cb0ef41Sopenharmony_ci 3481cb0ef41Sopenharmony_ciconst channel = diagnostics_channel.channel('my-channel'); 3491cb0ef41Sopenharmony_ci 3501cb0ef41Sopenharmony_cichannel.publish({ 3511cb0ef41Sopenharmony_ci some: 'message', 3521cb0ef41Sopenharmony_ci}); 3531cb0ef41Sopenharmony_ci``` 3541cb0ef41Sopenharmony_ci 3551cb0ef41Sopenharmony_ci```cjs 3561cb0ef41Sopenharmony_ciconst diagnostics_channel = require('node:diagnostics_channel'); 3571cb0ef41Sopenharmony_ci 3581cb0ef41Sopenharmony_ciconst channel = diagnostics_channel.channel('my-channel'); 3591cb0ef41Sopenharmony_ci 3601cb0ef41Sopenharmony_cichannel.publish({ 3611cb0ef41Sopenharmony_ci some: 'message', 3621cb0ef41Sopenharmony_ci}); 3631cb0ef41Sopenharmony_ci``` 3641cb0ef41Sopenharmony_ci 3651cb0ef41Sopenharmony_ci#### `channel.subscribe(onMessage)` 3661cb0ef41Sopenharmony_ci 3671cb0ef41Sopenharmony_ci<!-- YAML 3681cb0ef41Sopenharmony_ciadded: 3691cb0ef41Sopenharmony_ci - v15.1.0 3701cb0ef41Sopenharmony_ci - v14.17.0 3711cb0ef41Sopenharmony_cideprecated: v18.7.0 3721cb0ef41Sopenharmony_ci--> 3731cb0ef41Sopenharmony_ci 3741cb0ef41Sopenharmony_ci> Stability: 0 - Deprecated: Use [`diagnostics_channel.subscribe(name, onMessage)`][] 3751cb0ef41Sopenharmony_ci 3761cb0ef41Sopenharmony_ci* `onMessage` {Function} The handler to receive channel messages 3771cb0ef41Sopenharmony_ci * `message` {any} The message data 3781cb0ef41Sopenharmony_ci * `name` {string|symbol} The name of the channel 3791cb0ef41Sopenharmony_ci 3801cb0ef41Sopenharmony_ciRegister a message handler to subscribe to this channel. This message handler 3811cb0ef41Sopenharmony_ciwill be run synchronously whenever a message is published to the channel. Any 3821cb0ef41Sopenharmony_cierrors thrown in the message handler will trigger an [`'uncaughtException'`][]. 3831cb0ef41Sopenharmony_ci 3841cb0ef41Sopenharmony_ci```mjs 3851cb0ef41Sopenharmony_ciimport diagnostics_channel from 'node:diagnostics_channel'; 3861cb0ef41Sopenharmony_ci 3871cb0ef41Sopenharmony_ciconst channel = diagnostics_channel.channel('my-channel'); 3881cb0ef41Sopenharmony_ci 3891cb0ef41Sopenharmony_cichannel.subscribe((message, name) => { 3901cb0ef41Sopenharmony_ci // Received data 3911cb0ef41Sopenharmony_ci}); 3921cb0ef41Sopenharmony_ci``` 3931cb0ef41Sopenharmony_ci 3941cb0ef41Sopenharmony_ci```cjs 3951cb0ef41Sopenharmony_ciconst diagnostics_channel = require('node:diagnostics_channel'); 3961cb0ef41Sopenharmony_ci 3971cb0ef41Sopenharmony_ciconst channel = diagnostics_channel.channel('my-channel'); 3981cb0ef41Sopenharmony_ci 3991cb0ef41Sopenharmony_cichannel.subscribe((message, name) => { 4001cb0ef41Sopenharmony_ci // Received data 4011cb0ef41Sopenharmony_ci}); 4021cb0ef41Sopenharmony_ci``` 4031cb0ef41Sopenharmony_ci 4041cb0ef41Sopenharmony_ci#### `channel.unsubscribe(onMessage)` 4051cb0ef41Sopenharmony_ci 4061cb0ef41Sopenharmony_ci<!-- YAML 4071cb0ef41Sopenharmony_ciadded: 4081cb0ef41Sopenharmony_ci - v15.1.0 4091cb0ef41Sopenharmony_ci - v14.17.0 4101cb0ef41Sopenharmony_cideprecated: v18.7.0 4111cb0ef41Sopenharmony_cichanges: 4121cb0ef41Sopenharmony_ci - version: 4131cb0ef41Sopenharmony_ci - v17.1.0 4141cb0ef41Sopenharmony_ci - v16.14.0 4151cb0ef41Sopenharmony_ci - v14.19.0 4161cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/40433 4171cb0ef41Sopenharmony_ci description: Added return value. Added to channels without subscribers. 4181cb0ef41Sopenharmony_ci--> 4191cb0ef41Sopenharmony_ci 4201cb0ef41Sopenharmony_ci> Stability: 0 - Deprecated: Use [`diagnostics_channel.unsubscribe(name, onMessage)`][] 4211cb0ef41Sopenharmony_ci 4221cb0ef41Sopenharmony_ci* `onMessage` {Function} The previous subscribed handler to remove 4231cb0ef41Sopenharmony_ci* Returns: {boolean} `true` if the handler was found, `false` otherwise. 4241cb0ef41Sopenharmony_ci 4251cb0ef41Sopenharmony_ciRemove a message handler previously registered to this channel with 4261cb0ef41Sopenharmony_ci[`channel.subscribe(onMessage)`][]. 4271cb0ef41Sopenharmony_ci 4281cb0ef41Sopenharmony_ci```mjs 4291cb0ef41Sopenharmony_ciimport diagnostics_channel from 'node:diagnostics_channel'; 4301cb0ef41Sopenharmony_ci 4311cb0ef41Sopenharmony_ciconst channel = diagnostics_channel.channel('my-channel'); 4321cb0ef41Sopenharmony_ci 4331cb0ef41Sopenharmony_cifunction onMessage(message, name) { 4341cb0ef41Sopenharmony_ci // Received data 4351cb0ef41Sopenharmony_ci} 4361cb0ef41Sopenharmony_ci 4371cb0ef41Sopenharmony_cichannel.subscribe(onMessage); 4381cb0ef41Sopenharmony_ci 4391cb0ef41Sopenharmony_cichannel.unsubscribe(onMessage); 4401cb0ef41Sopenharmony_ci``` 4411cb0ef41Sopenharmony_ci 4421cb0ef41Sopenharmony_ci```cjs 4431cb0ef41Sopenharmony_ciconst diagnostics_channel = require('node:diagnostics_channel'); 4441cb0ef41Sopenharmony_ci 4451cb0ef41Sopenharmony_ciconst channel = diagnostics_channel.channel('my-channel'); 4461cb0ef41Sopenharmony_ci 4471cb0ef41Sopenharmony_cifunction onMessage(message, name) { 4481cb0ef41Sopenharmony_ci // Received data 4491cb0ef41Sopenharmony_ci} 4501cb0ef41Sopenharmony_ci 4511cb0ef41Sopenharmony_cichannel.subscribe(onMessage); 4521cb0ef41Sopenharmony_ci 4531cb0ef41Sopenharmony_cichannel.unsubscribe(onMessage); 4541cb0ef41Sopenharmony_ci``` 4551cb0ef41Sopenharmony_ci 4561cb0ef41Sopenharmony_ci#### `channel.bindStore(store[, transform])` 4571cb0ef41Sopenharmony_ci 4581cb0ef41Sopenharmony_ci<!-- YAML 4591cb0ef41Sopenharmony_ciadded: 4601cb0ef41Sopenharmony_ci - v18.19.0 4611cb0ef41Sopenharmony_ci--> 4621cb0ef41Sopenharmony_ci 4631cb0ef41Sopenharmony_ci> Stability: 1 - Experimental 4641cb0ef41Sopenharmony_ci 4651cb0ef41Sopenharmony_ci* `store` {AsyncLocalStorage} The store to which to bind the context data 4661cb0ef41Sopenharmony_ci* `transform` {Function} Transform context data before setting the store context 4671cb0ef41Sopenharmony_ci 4681cb0ef41Sopenharmony_ciWhen [`channel.runStores(context, ...)`][] is called, the given context data 4691cb0ef41Sopenharmony_ciwill be applied to any store bound to the channel. If the store has already been 4701cb0ef41Sopenharmony_cibound the previous `transform` function will be replaced with the new one. 4711cb0ef41Sopenharmony_ciThe `transform` function may be omitted to set the given context data as the 4721cb0ef41Sopenharmony_cicontext directly. 4731cb0ef41Sopenharmony_ci 4741cb0ef41Sopenharmony_ci```mjs 4751cb0ef41Sopenharmony_ciimport diagnostics_channel from 'node:diagnostics_channel'; 4761cb0ef41Sopenharmony_ciimport { AsyncLocalStorage } from 'node:async_hooks'; 4771cb0ef41Sopenharmony_ci 4781cb0ef41Sopenharmony_ciconst store = new AsyncLocalStorage(); 4791cb0ef41Sopenharmony_ci 4801cb0ef41Sopenharmony_ciconst channel = diagnostics_channel.channel('my-channel'); 4811cb0ef41Sopenharmony_ci 4821cb0ef41Sopenharmony_cichannel.bindStore(store, (data) => { 4831cb0ef41Sopenharmony_ci return { data }; 4841cb0ef41Sopenharmony_ci}); 4851cb0ef41Sopenharmony_ci``` 4861cb0ef41Sopenharmony_ci 4871cb0ef41Sopenharmony_ci```cjs 4881cb0ef41Sopenharmony_ciconst diagnostics_channel = require('node:diagnostics_channel'); 4891cb0ef41Sopenharmony_ciconst { AsyncLocalStorage } = require('node:async_hooks'); 4901cb0ef41Sopenharmony_ci 4911cb0ef41Sopenharmony_ciconst store = new AsyncLocalStorage(); 4921cb0ef41Sopenharmony_ci 4931cb0ef41Sopenharmony_ciconst channel = diagnostics_channel.channel('my-channel'); 4941cb0ef41Sopenharmony_ci 4951cb0ef41Sopenharmony_cichannel.bindStore(store, (data) => { 4961cb0ef41Sopenharmony_ci return { data }; 4971cb0ef41Sopenharmony_ci}); 4981cb0ef41Sopenharmony_ci``` 4991cb0ef41Sopenharmony_ci 5001cb0ef41Sopenharmony_ci#### `channel.unbindStore(store)` 5011cb0ef41Sopenharmony_ci 5021cb0ef41Sopenharmony_ci<!-- YAML 5031cb0ef41Sopenharmony_ciadded: 5041cb0ef41Sopenharmony_ci - v18.19.0 5051cb0ef41Sopenharmony_ci--> 5061cb0ef41Sopenharmony_ci 5071cb0ef41Sopenharmony_ci> Stability: 1 - Experimental 5081cb0ef41Sopenharmony_ci 5091cb0ef41Sopenharmony_ci* `store` {AsyncLocalStorage} The store to unbind from the channel. 5101cb0ef41Sopenharmony_ci* Returns: {boolean} `true` if the store was found, `false` otherwise. 5111cb0ef41Sopenharmony_ci 5121cb0ef41Sopenharmony_ciRemove a message handler previously registered to this channel with 5131cb0ef41Sopenharmony_ci[`channel.bindStore(store)`][]. 5141cb0ef41Sopenharmony_ci 5151cb0ef41Sopenharmony_ci```mjs 5161cb0ef41Sopenharmony_ciimport diagnostics_channel from 'node:diagnostics_channel'; 5171cb0ef41Sopenharmony_ciimport { AsyncLocalStorage } from 'node:async_hooks'; 5181cb0ef41Sopenharmony_ci 5191cb0ef41Sopenharmony_ciconst store = new AsyncLocalStorage(); 5201cb0ef41Sopenharmony_ci 5211cb0ef41Sopenharmony_ciconst channel = diagnostics_channel.channel('my-channel'); 5221cb0ef41Sopenharmony_ci 5231cb0ef41Sopenharmony_cichannel.bindStore(store); 5241cb0ef41Sopenharmony_cichannel.unbindStore(store); 5251cb0ef41Sopenharmony_ci``` 5261cb0ef41Sopenharmony_ci 5271cb0ef41Sopenharmony_ci```cjs 5281cb0ef41Sopenharmony_ciconst diagnostics_channel = require('node:diagnostics_channel'); 5291cb0ef41Sopenharmony_ciconst { AsyncLocalStorage } = require('node:async_hooks'); 5301cb0ef41Sopenharmony_ci 5311cb0ef41Sopenharmony_ciconst store = new AsyncLocalStorage(); 5321cb0ef41Sopenharmony_ci 5331cb0ef41Sopenharmony_ciconst channel = diagnostics_channel.channel('my-channel'); 5341cb0ef41Sopenharmony_ci 5351cb0ef41Sopenharmony_cichannel.bindStore(store); 5361cb0ef41Sopenharmony_cichannel.unbindStore(store); 5371cb0ef41Sopenharmony_ci``` 5381cb0ef41Sopenharmony_ci 5391cb0ef41Sopenharmony_ci#### `channel.runStores(context, fn[, thisArg[, ...args]])` 5401cb0ef41Sopenharmony_ci 5411cb0ef41Sopenharmony_ci<!-- YAML 5421cb0ef41Sopenharmony_ciadded: 5431cb0ef41Sopenharmony_ci - v18.19.0 5441cb0ef41Sopenharmony_ci--> 5451cb0ef41Sopenharmony_ci 5461cb0ef41Sopenharmony_ci> Stability: 1 - Experimental 5471cb0ef41Sopenharmony_ci 5481cb0ef41Sopenharmony_ci* `context` {any} Message to send to subscribers and bind to stores 5491cb0ef41Sopenharmony_ci* `fn` {Function} Handler to run within the entered storage context 5501cb0ef41Sopenharmony_ci* `thisArg` {any} The receiver to be used for the function call. 5511cb0ef41Sopenharmony_ci* `...args` {any} Optional arguments to pass to the function. 5521cb0ef41Sopenharmony_ci 5531cb0ef41Sopenharmony_ciApplies the given data to any AsyncLocalStorage instances bound to the channel 5541cb0ef41Sopenharmony_cifor the duration of the given function, then publishes to the channel within 5551cb0ef41Sopenharmony_cithe scope of that data is applied to the stores. 5561cb0ef41Sopenharmony_ci 5571cb0ef41Sopenharmony_ciIf a transform function was given to [`channel.bindStore(store)`][] it will be 5581cb0ef41Sopenharmony_ciapplied to transform the message data before it becomes the context value for 5591cb0ef41Sopenharmony_cithe store. The prior storage context is accessible from within the transform 5601cb0ef41Sopenharmony_cifunction in cases where context linking is required. 5611cb0ef41Sopenharmony_ci 5621cb0ef41Sopenharmony_ciThe context applied to the store should be accesible in any async code which 5631cb0ef41Sopenharmony_cicontinues from execution which began during the given function, however 5641cb0ef41Sopenharmony_cithere are some situations in which [context loss][] may occur. 5651cb0ef41Sopenharmony_ci 5661cb0ef41Sopenharmony_ci```mjs 5671cb0ef41Sopenharmony_ciimport diagnostics_channel from 'node:diagnostics_channel'; 5681cb0ef41Sopenharmony_ciimport { AsyncLocalStorage } from 'node:async_hooks'; 5691cb0ef41Sopenharmony_ci 5701cb0ef41Sopenharmony_ciconst store = new AsyncLocalStorage(); 5711cb0ef41Sopenharmony_ci 5721cb0ef41Sopenharmony_ciconst channel = diagnostics_channel.channel('my-channel'); 5731cb0ef41Sopenharmony_ci 5741cb0ef41Sopenharmony_cichannel.bindStore(store, (message) => { 5751cb0ef41Sopenharmony_ci const parent = store.getStore(); 5761cb0ef41Sopenharmony_ci return new Span(message, parent); 5771cb0ef41Sopenharmony_ci}); 5781cb0ef41Sopenharmony_cichannel.runStores({ some: 'message' }, () => { 5791cb0ef41Sopenharmony_ci store.getStore(); // Span({ some: 'message' }) 5801cb0ef41Sopenharmony_ci}); 5811cb0ef41Sopenharmony_ci``` 5821cb0ef41Sopenharmony_ci 5831cb0ef41Sopenharmony_ci```cjs 5841cb0ef41Sopenharmony_ciconst diagnostics_channel = require('node:diagnostics_channel'); 5851cb0ef41Sopenharmony_ciconst { AsyncLocalStorage } = require('node:async_hooks'); 5861cb0ef41Sopenharmony_ci 5871cb0ef41Sopenharmony_ciconst store = new AsyncLocalStorage(); 5881cb0ef41Sopenharmony_ci 5891cb0ef41Sopenharmony_ciconst channel = diagnostics_channel.channel('my-channel'); 5901cb0ef41Sopenharmony_ci 5911cb0ef41Sopenharmony_cichannel.bindStore(store, (message) => { 5921cb0ef41Sopenharmony_ci const parent = store.getStore(); 5931cb0ef41Sopenharmony_ci return new Span(message, parent); 5941cb0ef41Sopenharmony_ci}); 5951cb0ef41Sopenharmony_cichannel.runStores({ some: 'message' }, () => { 5961cb0ef41Sopenharmony_ci store.getStore(); // Span({ some: 'message' }) 5971cb0ef41Sopenharmony_ci}); 5981cb0ef41Sopenharmony_ci``` 5991cb0ef41Sopenharmony_ci 6001cb0ef41Sopenharmony_ci### Class: `TracingChannel` 6011cb0ef41Sopenharmony_ci 6021cb0ef41Sopenharmony_ci<!-- YAML 6031cb0ef41Sopenharmony_ciadded: 6041cb0ef41Sopenharmony_ci - v18.19.0 6051cb0ef41Sopenharmony_ci--> 6061cb0ef41Sopenharmony_ci 6071cb0ef41Sopenharmony_ci> Stability: 1 - Experimental 6081cb0ef41Sopenharmony_ci 6091cb0ef41Sopenharmony_ciThe class `TracingChannel` is a collection of [TracingChannel Channels][] which 6101cb0ef41Sopenharmony_citogether express a single traceable action. It is used to formalize and 6111cb0ef41Sopenharmony_cisimplify the process of producing events for tracing application flow. 6121cb0ef41Sopenharmony_ci[`diagnostics_channel.tracingChannel()`][] is used to construct a 6131cb0ef41Sopenharmony_ci`TracingChannel`. As with `Channel` it is recommended to create and reuse a 6141cb0ef41Sopenharmony_cisingle `TracingChannel` at the top-level of the file rather than creating them 6151cb0ef41Sopenharmony_cidynamically. 6161cb0ef41Sopenharmony_ci 6171cb0ef41Sopenharmony_ci#### `tracingChannel.subscribe(subscribers)` 6181cb0ef41Sopenharmony_ci 6191cb0ef41Sopenharmony_ci<!-- YAML 6201cb0ef41Sopenharmony_ciadded: 6211cb0ef41Sopenharmony_ci - v18.19.0 6221cb0ef41Sopenharmony_ci--> 6231cb0ef41Sopenharmony_ci 6241cb0ef41Sopenharmony_ci> Stability: 1 - Experimental 6251cb0ef41Sopenharmony_ci 6261cb0ef41Sopenharmony_ci* `subscribers` {Object} Set of [TracingChannel Channels][] subscribers 6271cb0ef41Sopenharmony_ci * `start` {Function} The [`start` event][] subscriber 6281cb0ef41Sopenharmony_ci * `end` {Function} The [`end` event][] subscriber 6291cb0ef41Sopenharmony_ci * `asyncStart` {Function} The [`asyncStart` event][] subscriber 6301cb0ef41Sopenharmony_ci * `asyncEnd` {Function} The [`asyncEnd` event][] subscriber 6311cb0ef41Sopenharmony_ci * `error` {Function} The [`error` event][] subscriber 6321cb0ef41Sopenharmony_ci 6331cb0ef41Sopenharmony_ciHelper to subscribe a collection of functions to the corresponding channels. 6341cb0ef41Sopenharmony_ciThis is the same as calling [`channel.subscribe(onMessage)`][] on each channel 6351cb0ef41Sopenharmony_ciindividually. 6361cb0ef41Sopenharmony_ci 6371cb0ef41Sopenharmony_ci```mjs 6381cb0ef41Sopenharmony_ciimport diagnostics_channel from 'node:diagnostics_channel'; 6391cb0ef41Sopenharmony_ci 6401cb0ef41Sopenharmony_ciconst channels = diagnostics_channel.tracingChannel('my-channel'); 6411cb0ef41Sopenharmony_ci 6421cb0ef41Sopenharmony_cichannels.subscribe({ 6431cb0ef41Sopenharmony_ci start(message) { 6441cb0ef41Sopenharmony_ci // Handle start message 6451cb0ef41Sopenharmony_ci }, 6461cb0ef41Sopenharmony_ci end(message) { 6471cb0ef41Sopenharmony_ci // Handle end message 6481cb0ef41Sopenharmony_ci }, 6491cb0ef41Sopenharmony_ci asyncStart(message) { 6501cb0ef41Sopenharmony_ci // Handle asyncStart message 6511cb0ef41Sopenharmony_ci }, 6521cb0ef41Sopenharmony_ci asyncEnd(message) { 6531cb0ef41Sopenharmony_ci // Handle asyncEnd message 6541cb0ef41Sopenharmony_ci }, 6551cb0ef41Sopenharmony_ci error(message) { 6561cb0ef41Sopenharmony_ci // Handle error message 6571cb0ef41Sopenharmony_ci }, 6581cb0ef41Sopenharmony_ci}); 6591cb0ef41Sopenharmony_ci``` 6601cb0ef41Sopenharmony_ci 6611cb0ef41Sopenharmony_ci```cjs 6621cb0ef41Sopenharmony_ciconst diagnostics_channel = require('node:diagnostics_channel'); 6631cb0ef41Sopenharmony_ci 6641cb0ef41Sopenharmony_ciconst channels = diagnostics_channel.tracingChannel('my-channel'); 6651cb0ef41Sopenharmony_ci 6661cb0ef41Sopenharmony_cichannels.subscribe({ 6671cb0ef41Sopenharmony_ci start(message) { 6681cb0ef41Sopenharmony_ci // Handle start message 6691cb0ef41Sopenharmony_ci }, 6701cb0ef41Sopenharmony_ci end(message) { 6711cb0ef41Sopenharmony_ci // Handle end message 6721cb0ef41Sopenharmony_ci }, 6731cb0ef41Sopenharmony_ci asyncStart(message) { 6741cb0ef41Sopenharmony_ci // Handle asyncStart message 6751cb0ef41Sopenharmony_ci }, 6761cb0ef41Sopenharmony_ci asyncEnd(message) { 6771cb0ef41Sopenharmony_ci // Handle asyncEnd message 6781cb0ef41Sopenharmony_ci }, 6791cb0ef41Sopenharmony_ci error(message) { 6801cb0ef41Sopenharmony_ci // Handle error message 6811cb0ef41Sopenharmony_ci }, 6821cb0ef41Sopenharmony_ci}); 6831cb0ef41Sopenharmony_ci``` 6841cb0ef41Sopenharmony_ci 6851cb0ef41Sopenharmony_ci#### `tracingChannel.unsubscribe(subscribers)` 6861cb0ef41Sopenharmony_ci 6871cb0ef41Sopenharmony_ci<!-- YAML 6881cb0ef41Sopenharmony_ciadded: 6891cb0ef41Sopenharmony_ci - v18.19.0 6901cb0ef41Sopenharmony_ci--> 6911cb0ef41Sopenharmony_ci 6921cb0ef41Sopenharmony_ci> Stability: 1 - Experimental 6931cb0ef41Sopenharmony_ci 6941cb0ef41Sopenharmony_ci* `subscribers` {Object} Set of [TracingChannel Channels][] subscribers 6951cb0ef41Sopenharmony_ci * `start` {Function} The [`start` event][] subscriber 6961cb0ef41Sopenharmony_ci * `end` {Function} The [`end` event][] subscriber 6971cb0ef41Sopenharmony_ci * `asyncStart` {Function} The [`asyncStart` event][] subscriber 6981cb0ef41Sopenharmony_ci * `asyncEnd` {Function} The [`asyncEnd` event][] subscriber 6991cb0ef41Sopenharmony_ci * `error` {Function} The [`error` event][] subscriber 7001cb0ef41Sopenharmony_ci* Returns: {boolean} `true` if all handlers were successfully unsubscribed, 7011cb0ef41Sopenharmony_ci and `false` otherwise. 7021cb0ef41Sopenharmony_ci 7031cb0ef41Sopenharmony_ciHelper to unsubscribe a collection of functions from the corresponding channels. 7041cb0ef41Sopenharmony_ciThis is the same as calling [`channel.unsubscribe(onMessage)`][] on each channel 7051cb0ef41Sopenharmony_ciindividually. 7061cb0ef41Sopenharmony_ci 7071cb0ef41Sopenharmony_ci```mjs 7081cb0ef41Sopenharmony_ciimport diagnostics_channel from 'node:diagnostics_channel'; 7091cb0ef41Sopenharmony_ci 7101cb0ef41Sopenharmony_ciconst channels = diagnostics_channel.tracingChannel('my-channel'); 7111cb0ef41Sopenharmony_ci 7121cb0ef41Sopenharmony_cichannels.unsubscribe({ 7131cb0ef41Sopenharmony_ci start(message) { 7141cb0ef41Sopenharmony_ci // Handle start message 7151cb0ef41Sopenharmony_ci }, 7161cb0ef41Sopenharmony_ci end(message) { 7171cb0ef41Sopenharmony_ci // Handle end message 7181cb0ef41Sopenharmony_ci }, 7191cb0ef41Sopenharmony_ci asyncStart(message) { 7201cb0ef41Sopenharmony_ci // Handle asyncStart message 7211cb0ef41Sopenharmony_ci }, 7221cb0ef41Sopenharmony_ci asyncEnd(message) { 7231cb0ef41Sopenharmony_ci // Handle asyncEnd message 7241cb0ef41Sopenharmony_ci }, 7251cb0ef41Sopenharmony_ci error(message) { 7261cb0ef41Sopenharmony_ci // Handle error message 7271cb0ef41Sopenharmony_ci }, 7281cb0ef41Sopenharmony_ci}); 7291cb0ef41Sopenharmony_ci``` 7301cb0ef41Sopenharmony_ci 7311cb0ef41Sopenharmony_ci```cjs 7321cb0ef41Sopenharmony_ciconst diagnostics_channel = require('node:diagnostics_channel'); 7331cb0ef41Sopenharmony_ci 7341cb0ef41Sopenharmony_ciconst channels = diagnostics_channel.tracingChannel('my-channel'); 7351cb0ef41Sopenharmony_ci 7361cb0ef41Sopenharmony_cichannels.unsubscribe({ 7371cb0ef41Sopenharmony_ci start(message) { 7381cb0ef41Sopenharmony_ci // Handle start message 7391cb0ef41Sopenharmony_ci }, 7401cb0ef41Sopenharmony_ci end(message) { 7411cb0ef41Sopenharmony_ci // Handle end message 7421cb0ef41Sopenharmony_ci }, 7431cb0ef41Sopenharmony_ci asyncStart(message) { 7441cb0ef41Sopenharmony_ci // Handle asyncStart message 7451cb0ef41Sopenharmony_ci }, 7461cb0ef41Sopenharmony_ci asyncEnd(message) { 7471cb0ef41Sopenharmony_ci // Handle asyncEnd message 7481cb0ef41Sopenharmony_ci }, 7491cb0ef41Sopenharmony_ci error(message) { 7501cb0ef41Sopenharmony_ci // Handle error message 7511cb0ef41Sopenharmony_ci }, 7521cb0ef41Sopenharmony_ci}); 7531cb0ef41Sopenharmony_ci``` 7541cb0ef41Sopenharmony_ci 7551cb0ef41Sopenharmony_ci#### `tracingChannel.traceSync(fn[, context[, thisArg[, ...args]]])` 7561cb0ef41Sopenharmony_ci 7571cb0ef41Sopenharmony_ci<!-- YAML 7581cb0ef41Sopenharmony_ciadded: 7591cb0ef41Sopenharmony_ci - v18.19.0 7601cb0ef41Sopenharmony_ci--> 7611cb0ef41Sopenharmony_ci 7621cb0ef41Sopenharmony_ci> Stability: 1 - Experimental 7631cb0ef41Sopenharmony_ci 7641cb0ef41Sopenharmony_ci* `fn` {Function} Function to wrap a trace around 7651cb0ef41Sopenharmony_ci* `context` {Object} Shared object to correlate events through 7661cb0ef41Sopenharmony_ci* `thisArg` {any} The receiver to be used for the function call 7671cb0ef41Sopenharmony_ci* `...args` {any} Optional arguments to pass to the function 7681cb0ef41Sopenharmony_ci* Returns: {any} The return value of the given function 7691cb0ef41Sopenharmony_ci 7701cb0ef41Sopenharmony_ciTrace a synchronous function call. This will always produce a [`start` event][] 7711cb0ef41Sopenharmony_ciand [`end` event][] around the execution and may produce an [`error` event][] 7721cb0ef41Sopenharmony_ciif the given function throws an error. This will run the given function using 7731cb0ef41Sopenharmony_ci[`channel.runStores(context, ...)`][] on the `start` channel which ensures all 7741cb0ef41Sopenharmony_cievents should have any bound stores set to match this trace context. 7751cb0ef41Sopenharmony_ci 7761cb0ef41Sopenharmony_ci```mjs 7771cb0ef41Sopenharmony_ciimport diagnostics_channel from 'node:diagnostics_channel'; 7781cb0ef41Sopenharmony_ci 7791cb0ef41Sopenharmony_ciconst channels = diagnostics_channel.tracingChannel('my-channel'); 7801cb0ef41Sopenharmony_ci 7811cb0ef41Sopenharmony_cichannels.traceSync(() => { 7821cb0ef41Sopenharmony_ci // Do something 7831cb0ef41Sopenharmony_ci}, { 7841cb0ef41Sopenharmony_ci some: 'thing', 7851cb0ef41Sopenharmony_ci}); 7861cb0ef41Sopenharmony_ci``` 7871cb0ef41Sopenharmony_ci 7881cb0ef41Sopenharmony_ci```cjs 7891cb0ef41Sopenharmony_ciconst diagnostics_channel = require('node:diagnostics_channel'); 7901cb0ef41Sopenharmony_ci 7911cb0ef41Sopenharmony_ciconst channels = diagnostics_channel.tracingChannel('my-channel'); 7921cb0ef41Sopenharmony_ci 7931cb0ef41Sopenharmony_cichannels.traceSync(() => { 7941cb0ef41Sopenharmony_ci // Do something 7951cb0ef41Sopenharmony_ci}, { 7961cb0ef41Sopenharmony_ci some: 'thing', 7971cb0ef41Sopenharmony_ci}); 7981cb0ef41Sopenharmony_ci``` 7991cb0ef41Sopenharmony_ci 8001cb0ef41Sopenharmony_ci#### `tracingChannel.tracePromise(fn[, context[, thisArg[, ...args]]])` 8011cb0ef41Sopenharmony_ci 8021cb0ef41Sopenharmony_ci<!-- YAML 8031cb0ef41Sopenharmony_ciadded: 8041cb0ef41Sopenharmony_ci - v18.19.0 8051cb0ef41Sopenharmony_ci--> 8061cb0ef41Sopenharmony_ci 8071cb0ef41Sopenharmony_ci> Stability: 1 - Experimental 8081cb0ef41Sopenharmony_ci 8091cb0ef41Sopenharmony_ci* `fn` {Function} Promise-returning function to wrap a trace around 8101cb0ef41Sopenharmony_ci* `context` {Object} Shared object to correlate trace events through 8111cb0ef41Sopenharmony_ci* `thisArg` {any} The receiver to be used for the function call 8121cb0ef41Sopenharmony_ci* `...args` {any} Optional arguments to pass to the function 8131cb0ef41Sopenharmony_ci* Returns: {Promise} Chained from promise returned by the given function 8141cb0ef41Sopenharmony_ci 8151cb0ef41Sopenharmony_ciTrace a promise-returning function call. This will always produce a 8161cb0ef41Sopenharmony_ci[`start` event][] and [`end` event][] around the synchronous portion of the 8171cb0ef41Sopenharmony_cifunction execution, and will produce an [`asyncStart` event][] and 8181cb0ef41Sopenharmony_ci[`asyncEnd` event][] when a promise continuation is reached. It may also 8191cb0ef41Sopenharmony_ciproduce an [`error` event][] if the given function throws an error or the 8201cb0ef41Sopenharmony_cireturned promise rejects. This will run the given function using 8211cb0ef41Sopenharmony_ci[`channel.runStores(context, ...)`][] on the `start` channel which ensures all 8221cb0ef41Sopenharmony_cievents should have any bound stores set to match this trace context. 8231cb0ef41Sopenharmony_ci 8241cb0ef41Sopenharmony_ci```mjs 8251cb0ef41Sopenharmony_ciimport diagnostics_channel from 'node:diagnostics_channel'; 8261cb0ef41Sopenharmony_ci 8271cb0ef41Sopenharmony_ciconst channels = diagnostics_channel.tracingChannel('my-channel'); 8281cb0ef41Sopenharmony_ci 8291cb0ef41Sopenharmony_cichannels.tracePromise(async () => { 8301cb0ef41Sopenharmony_ci // Do something 8311cb0ef41Sopenharmony_ci}, { 8321cb0ef41Sopenharmony_ci some: 'thing', 8331cb0ef41Sopenharmony_ci}); 8341cb0ef41Sopenharmony_ci``` 8351cb0ef41Sopenharmony_ci 8361cb0ef41Sopenharmony_ci```cjs 8371cb0ef41Sopenharmony_ciconst diagnostics_channel = require('node:diagnostics_channel'); 8381cb0ef41Sopenharmony_ci 8391cb0ef41Sopenharmony_ciconst channels = diagnostics_channel.tracingChannel('my-channel'); 8401cb0ef41Sopenharmony_ci 8411cb0ef41Sopenharmony_cichannels.tracePromise(async () => { 8421cb0ef41Sopenharmony_ci // Do something 8431cb0ef41Sopenharmony_ci}, { 8441cb0ef41Sopenharmony_ci some: 'thing', 8451cb0ef41Sopenharmony_ci}); 8461cb0ef41Sopenharmony_ci``` 8471cb0ef41Sopenharmony_ci 8481cb0ef41Sopenharmony_ci#### `tracingChannel.traceCallback(fn[, position[, context[, thisArg[, ...args]]]])` 8491cb0ef41Sopenharmony_ci 8501cb0ef41Sopenharmony_ci<!-- YAML 8511cb0ef41Sopenharmony_ciadded: 8521cb0ef41Sopenharmony_ci - v18.19.0 8531cb0ef41Sopenharmony_ci--> 8541cb0ef41Sopenharmony_ci 8551cb0ef41Sopenharmony_ci> Stability: 1 - Experimental 8561cb0ef41Sopenharmony_ci 8571cb0ef41Sopenharmony_ci* `fn` {Function} callback using function to wrap a trace around 8581cb0ef41Sopenharmony_ci* `position` {number} Zero-indexed argument position of expected callback 8591cb0ef41Sopenharmony_ci* `context` {Object} Shared object to correlate trace events through 8601cb0ef41Sopenharmony_ci* `thisArg` {any} The receiver to be used for the function call 8611cb0ef41Sopenharmony_ci* `...args` {any} Optional arguments to pass to the function 8621cb0ef41Sopenharmony_ci* Returns: {any} The return value of the given function 8631cb0ef41Sopenharmony_ci 8641cb0ef41Sopenharmony_ciTrace a callback-receiving function call. This will always produce a 8651cb0ef41Sopenharmony_ci[`start` event][] and [`end` event][] around the synchronous portion of the 8661cb0ef41Sopenharmony_cifunction execution, and will produce a [`asyncStart` event][] and 8671cb0ef41Sopenharmony_ci[`asyncEnd` event][] around the callback execution. It may also produce an 8681cb0ef41Sopenharmony_ci[`error` event][] if the given function throws an error or the returned 8691cb0ef41Sopenharmony_cipromise rejects. This will run the given function using 8701cb0ef41Sopenharmony_ci[`channel.runStores(context, ...)`][] on the `start` channel which ensures all 8711cb0ef41Sopenharmony_cievents should have any bound stores set to match this trace context. 8721cb0ef41Sopenharmony_ci 8731cb0ef41Sopenharmony_ciThe `position` will be -1 by default to indicate the final argument should 8741cb0ef41Sopenharmony_cibe used as the callback. 8751cb0ef41Sopenharmony_ci 8761cb0ef41Sopenharmony_ci```mjs 8771cb0ef41Sopenharmony_ciimport diagnostics_channel from 'node:diagnostics_channel'; 8781cb0ef41Sopenharmony_ci 8791cb0ef41Sopenharmony_ciconst channels = diagnostics_channel.tracingChannel('my-channel'); 8801cb0ef41Sopenharmony_ci 8811cb0ef41Sopenharmony_cichannels.traceCallback((arg1, callback) => { 8821cb0ef41Sopenharmony_ci // Do something 8831cb0ef41Sopenharmony_ci callback(null, 'result'); 8841cb0ef41Sopenharmony_ci}, 1, { 8851cb0ef41Sopenharmony_ci some: 'thing', 8861cb0ef41Sopenharmony_ci}, thisArg, arg1, callback); 8871cb0ef41Sopenharmony_ci``` 8881cb0ef41Sopenharmony_ci 8891cb0ef41Sopenharmony_ci```cjs 8901cb0ef41Sopenharmony_ciconst diagnostics_channel = require('node:diagnostics_channel'); 8911cb0ef41Sopenharmony_ci 8921cb0ef41Sopenharmony_ciconst channels = diagnostics_channel.tracingChannel('my-channel'); 8931cb0ef41Sopenharmony_ci 8941cb0ef41Sopenharmony_cichannels.traceCallback((arg1, callback) => { 8951cb0ef41Sopenharmony_ci // Do something 8961cb0ef41Sopenharmony_ci callback(null, 'result'); 8971cb0ef41Sopenharmony_ci}, { 8981cb0ef41Sopenharmony_ci some: 'thing', 8991cb0ef41Sopenharmony_ci}, thisArg, arg1, callback); 9001cb0ef41Sopenharmony_ci``` 9011cb0ef41Sopenharmony_ci 9021cb0ef41Sopenharmony_ciThe callback will also be run with [`channel.runStores(context, ...)`][] which 9031cb0ef41Sopenharmony_cienables context loss recovery in some cases. 9041cb0ef41Sopenharmony_ci 9051cb0ef41Sopenharmony_ci```mjs 9061cb0ef41Sopenharmony_ciimport diagnostics_channel from 'node:diagnostics_channel'; 9071cb0ef41Sopenharmony_ciimport { AsyncLocalStorage } from 'node:async_hooks'; 9081cb0ef41Sopenharmony_ci 9091cb0ef41Sopenharmony_ciconst channels = diagnostics_channel.tracingChannel('my-channel'); 9101cb0ef41Sopenharmony_ciconst myStore = new AsyncLocalStorage(); 9111cb0ef41Sopenharmony_ci 9121cb0ef41Sopenharmony_ci// The start channel sets the initial store data to something 9131cb0ef41Sopenharmony_ci// and stores that store data value on the trace context object 9141cb0ef41Sopenharmony_cichannels.start.bindStore(myStore, (data) => { 9151cb0ef41Sopenharmony_ci const span = new Span(data); 9161cb0ef41Sopenharmony_ci data.span = span; 9171cb0ef41Sopenharmony_ci return span; 9181cb0ef41Sopenharmony_ci}); 9191cb0ef41Sopenharmony_ci 9201cb0ef41Sopenharmony_ci// Then asyncStart can restore from that data it stored previously 9211cb0ef41Sopenharmony_cichannels.asyncStart.bindStore(myStore, (data) => { 9221cb0ef41Sopenharmony_ci return data.span; 9231cb0ef41Sopenharmony_ci}); 9241cb0ef41Sopenharmony_ci``` 9251cb0ef41Sopenharmony_ci 9261cb0ef41Sopenharmony_ci```cjs 9271cb0ef41Sopenharmony_ciconst diagnostics_channel = require('node:diagnostics_channel'); 9281cb0ef41Sopenharmony_ciconst { AsyncLocalStorage } = require('node:async_hooks'); 9291cb0ef41Sopenharmony_ci 9301cb0ef41Sopenharmony_ciconst channels = diagnostics_channel.tracingChannel('my-channel'); 9311cb0ef41Sopenharmony_ciconst myStore = new AsyncLocalStorage(); 9321cb0ef41Sopenharmony_ci 9331cb0ef41Sopenharmony_ci// The start channel sets the initial store data to something 9341cb0ef41Sopenharmony_ci// and stores that store data value on the trace context object 9351cb0ef41Sopenharmony_cichannels.start.bindStore(myStore, (data) => { 9361cb0ef41Sopenharmony_ci const span = new Span(data); 9371cb0ef41Sopenharmony_ci data.span = span; 9381cb0ef41Sopenharmony_ci return span; 9391cb0ef41Sopenharmony_ci}); 9401cb0ef41Sopenharmony_ci 9411cb0ef41Sopenharmony_ci// Then asyncStart can restore from that data it stored previously 9421cb0ef41Sopenharmony_cichannels.asyncStart.bindStore(myStore, (data) => { 9431cb0ef41Sopenharmony_ci return data.span; 9441cb0ef41Sopenharmony_ci}); 9451cb0ef41Sopenharmony_ci``` 9461cb0ef41Sopenharmony_ci 9471cb0ef41Sopenharmony_ci### TracingChannel Channels 9481cb0ef41Sopenharmony_ci 9491cb0ef41Sopenharmony_ciA TracingChannel is a collection of several diagnostics\_channels representing 9501cb0ef41Sopenharmony_cispecific points in the execution lifecycle of a single traceable action. The 9511cb0ef41Sopenharmony_cibehaviour is split into five diagnostics\_channels consisting of `start`, 9521cb0ef41Sopenharmony_ci`end`, `asyncStart`, `asyncEnd`, and `error`. A single traceable action will 9531cb0ef41Sopenharmony_cishare the same event object between all events, this can be helpful for 9541cb0ef41Sopenharmony_cimanaging correlation through a weakmap. 9551cb0ef41Sopenharmony_ci 9561cb0ef41Sopenharmony_ciThese event objects will be extended with `result` or `error` values when 9571cb0ef41Sopenharmony_cithe task "completes". In the case of a synchronous task the `result` will be 9581cb0ef41Sopenharmony_cithe return value and the `error` will be anything thrown from the function. 9591cb0ef41Sopenharmony_ciWith callback-based async functions the `result` will be the second argument 9601cb0ef41Sopenharmony_ciof the callback while the `error` will either be a thrown error visible in the 9611cb0ef41Sopenharmony_ci`end` event or the first callback argument in either of the `asyncStart` or 9621cb0ef41Sopenharmony_ci`asyncEnd` events. 9631cb0ef41Sopenharmony_ci 9641cb0ef41Sopenharmony_ciTracing channels should follow a naming pattern of: 9651cb0ef41Sopenharmony_ci 9661cb0ef41Sopenharmony_ci* `tracing:module.class.method:start` or `tracing:module.function:start` 9671cb0ef41Sopenharmony_ci* `tracing:module.class.method:end` or `tracing:module.function:end` 9681cb0ef41Sopenharmony_ci* `tracing:module.class.method:asyncStart` or `tracing:module.function:asyncStart` 9691cb0ef41Sopenharmony_ci* `tracing:module.class.method:asyncEnd` or `tracing:module.function:asyncEnd` 9701cb0ef41Sopenharmony_ci* `tracing:module.class.method:error` or `tracing:module.function:error` 9711cb0ef41Sopenharmony_ci 9721cb0ef41Sopenharmony_ci#### `start(event)` 9731cb0ef41Sopenharmony_ci 9741cb0ef41Sopenharmony_ci* Name: `tracing:${name}:start` 9751cb0ef41Sopenharmony_ci 9761cb0ef41Sopenharmony_ciThe `start` event represents the point at which a function is called. At this 9771cb0ef41Sopenharmony_cipoint the event data may contain function arguments or anything else available 9781cb0ef41Sopenharmony_ciat the very start of the execution of the function. 9791cb0ef41Sopenharmony_ci 9801cb0ef41Sopenharmony_ci#### `end(event)` 9811cb0ef41Sopenharmony_ci 9821cb0ef41Sopenharmony_ci* Name: `tracing:${name}:end` 9831cb0ef41Sopenharmony_ci 9841cb0ef41Sopenharmony_ciThe `end` event represents the point at which a function call returns a value. 9851cb0ef41Sopenharmony_ciIn the case of an async function this is when the promise returned not when the 9861cb0ef41Sopenharmony_cifunction itself makes a return statement internally. At this point, if the 9871cb0ef41Sopenharmony_citraced function was synchronous the `result` field will be set to the return 9881cb0ef41Sopenharmony_civalue of the function. Alternatively, the `error` field may be present to 9891cb0ef41Sopenharmony_cirepresent any thrown errors. 9901cb0ef41Sopenharmony_ci 9911cb0ef41Sopenharmony_ciIt is recommended to listen specifically to the `error` event to track errors 9921cb0ef41Sopenharmony_cias it may be possible for a traceable action to produce multiple errors. For 9931cb0ef41Sopenharmony_ciexample, an async task which fails may be started internally before the sync 9941cb0ef41Sopenharmony_cipart of the task then throws an error. 9951cb0ef41Sopenharmony_ci 9961cb0ef41Sopenharmony_ci#### `asyncStart(event)` 9971cb0ef41Sopenharmony_ci 9981cb0ef41Sopenharmony_ci* Name: `tracing:${name}:asyncStart` 9991cb0ef41Sopenharmony_ci 10001cb0ef41Sopenharmony_ciThe `asyncStart` event represents the callback or continuation of a traceable 10011cb0ef41Sopenharmony_cifunction being reached. At this point things like callback arguments may be 10021cb0ef41Sopenharmony_ciavailable, or anything else expressing the "result" of the action. 10031cb0ef41Sopenharmony_ci 10041cb0ef41Sopenharmony_ciFor callbacks-based functions, the first argument of the callback will be 10051cb0ef41Sopenharmony_ciassigned to the `error` field, if not `undefined` or `null`, and the second 10061cb0ef41Sopenharmony_ciargument will be assigned to the `result` field. 10071cb0ef41Sopenharmony_ci 10081cb0ef41Sopenharmony_ciFor promises, the argument to the `resolve` path will be assigned to `result` 10091cb0ef41Sopenharmony_cior the argument to the `reject` path will be assign to `error`. 10101cb0ef41Sopenharmony_ci 10111cb0ef41Sopenharmony_ciIt is recommended to listen specifically to the `error` event to track errors 10121cb0ef41Sopenharmony_cias it may be possible for a traceable action to produce multiple errors. For 10131cb0ef41Sopenharmony_ciexample, an async task which fails may be started internally before the sync 10141cb0ef41Sopenharmony_cipart of the task then throws an error. 10151cb0ef41Sopenharmony_ci 10161cb0ef41Sopenharmony_ci#### `asyncEnd(event)` 10171cb0ef41Sopenharmony_ci 10181cb0ef41Sopenharmony_ci* Name: `tracing:${name}:asyncEnd` 10191cb0ef41Sopenharmony_ci 10201cb0ef41Sopenharmony_ciThe `asyncEnd` event represents the callback of an asynchronous function 10211cb0ef41Sopenharmony_cireturning. It's not likely event data will change after the `asyncStart` event, 10221cb0ef41Sopenharmony_cihowever it may be useful to see the point where the callback completes. 10231cb0ef41Sopenharmony_ci 10241cb0ef41Sopenharmony_ci#### `error(event)` 10251cb0ef41Sopenharmony_ci 10261cb0ef41Sopenharmony_ci* Name: `tracing:${name}:error` 10271cb0ef41Sopenharmony_ci 10281cb0ef41Sopenharmony_ciThe `error` event represents any error produced by the traceable function 10291cb0ef41Sopenharmony_cieither synchronously or asynchronously. If an error is thrown in the 10301cb0ef41Sopenharmony_cisynchronous portion of the traced function the error will be assigned to the 10311cb0ef41Sopenharmony_ci`error` field of the event and the `error` event will be triggered. If an error 10321cb0ef41Sopenharmony_ciis received asynchronously through a callback or promise rejection it will also 10331cb0ef41Sopenharmony_cibe assigned to the `error` field of the event and trigger the `error` event. 10341cb0ef41Sopenharmony_ci 10351cb0ef41Sopenharmony_ciIt is possible for a single traceable function call to produce errors multiple 10361cb0ef41Sopenharmony_citimes so this should be considered when consuming this event. For example, if 10371cb0ef41Sopenharmony_cianother async task is triggered internally which fails and then the sync part 10381cb0ef41Sopenharmony_ciof the function then throws and error two `error` events will be emitted, one 10391cb0ef41Sopenharmony_cifor the sync error and one for the async error. 10401cb0ef41Sopenharmony_ci 10411cb0ef41Sopenharmony_ci### Built-in Channels 10421cb0ef41Sopenharmony_ci 10431cb0ef41Sopenharmony_ci> Stability: 1 - Experimental 10441cb0ef41Sopenharmony_ci 10451cb0ef41Sopenharmony_ciWhile the diagnostics\_channel API is now considered stable, the built-in 10461cb0ef41Sopenharmony_cichannels currently available are not. Each channel must be declared stable 10471cb0ef41Sopenharmony_ciindependently. 10481cb0ef41Sopenharmony_ci 10491cb0ef41Sopenharmony_ci#### HTTP 10501cb0ef41Sopenharmony_ci 10511cb0ef41Sopenharmony_ci`http.client.request.start` 10521cb0ef41Sopenharmony_ci 10531cb0ef41Sopenharmony_ci* `request` {http.ClientRequest} 10541cb0ef41Sopenharmony_ci 10551cb0ef41Sopenharmony_ciEmitted when client starts a request. 10561cb0ef41Sopenharmony_ci 10571cb0ef41Sopenharmony_ci`http.client.response.finish` 10581cb0ef41Sopenharmony_ci 10591cb0ef41Sopenharmony_ci* `request` {http.ClientRequest} 10601cb0ef41Sopenharmony_ci* `response` {http.IncomingMessage} 10611cb0ef41Sopenharmony_ci 10621cb0ef41Sopenharmony_ciEmitted when client receives a response. 10631cb0ef41Sopenharmony_ci 10641cb0ef41Sopenharmony_ci`http.server.request.start` 10651cb0ef41Sopenharmony_ci 10661cb0ef41Sopenharmony_ci* `request` {http.IncomingMessage} 10671cb0ef41Sopenharmony_ci* `response` {http.ServerResponse} 10681cb0ef41Sopenharmony_ci* `socket` {net.Socket} 10691cb0ef41Sopenharmony_ci* `server` {http.Server} 10701cb0ef41Sopenharmony_ci 10711cb0ef41Sopenharmony_ciEmitted when server receives a request. 10721cb0ef41Sopenharmony_ci 10731cb0ef41Sopenharmony_ci`http.server.response.finish` 10741cb0ef41Sopenharmony_ci 10751cb0ef41Sopenharmony_ci* `request` {http.IncomingMessage} 10761cb0ef41Sopenharmony_ci* `response` {http.ServerResponse} 10771cb0ef41Sopenharmony_ci* `socket` {net.Socket} 10781cb0ef41Sopenharmony_ci* `server` {http.Server} 10791cb0ef41Sopenharmony_ci 10801cb0ef41Sopenharmony_ciEmitted when server sends a response. 10811cb0ef41Sopenharmony_ci 10821cb0ef41Sopenharmony_ci`net.client.socket` 10831cb0ef41Sopenharmony_ci 10841cb0ef41Sopenharmony_ci* `socket` {net.Socket} 10851cb0ef41Sopenharmony_ci 10861cb0ef41Sopenharmony_ciEmitted when a new TCP or pipe client socket is created. 10871cb0ef41Sopenharmony_ci 10881cb0ef41Sopenharmony_ci`net.server.socket` 10891cb0ef41Sopenharmony_ci 10901cb0ef41Sopenharmony_ci* `socket` {net.Socket} 10911cb0ef41Sopenharmony_ci 10921cb0ef41Sopenharmony_ciEmitted when a new TCP or pipe connection is received. 10931cb0ef41Sopenharmony_ci 10941cb0ef41Sopenharmony_ci`udp.socket` 10951cb0ef41Sopenharmony_ci 10961cb0ef41Sopenharmony_ci* `socket` {dgram.Socket} 10971cb0ef41Sopenharmony_ci 10981cb0ef41Sopenharmony_ciEmitted when a new UDP socket is created. 10991cb0ef41Sopenharmony_ci 11001cb0ef41Sopenharmony_ci[TracingChannel Channels]: #tracingchannel-channels 11011cb0ef41Sopenharmony_ci[`'uncaughtException'`]: process.md#event-uncaughtexception 11021cb0ef41Sopenharmony_ci[`TracingChannel`]: #class-tracingchannel 11031cb0ef41Sopenharmony_ci[`asyncEnd` event]: #asyncendevent 11041cb0ef41Sopenharmony_ci[`asyncStart` event]: #asyncstartevent 11051cb0ef41Sopenharmony_ci[`channel.bindStore(store)`]: #channelbindstorestore-transform 11061cb0ef41Sopenharmony_ci[`channel.runStores(context, ...)`]: #channelrunstorescontext-fn-thisarg-args 11071cb0ef41Sopenharmony_ci[`channel.subscribe(onMessage)`]: #channelsubscribeonmessage 11081cb0ef41Sopenharmony_ci[`channel.unsubscribe(onMessage)`]: #channelunsubscribeonmessage 11091cb0ef41Sopenharmony_ci[`diagnostics_channel.channel(name)`]: #diagnostics_channelchannelname 11101cb0ef41Sopenharmony_ci[`diagnostics_channel.subscribe(name, onMessage)`]: #diagnostics_channelsubscribename-onmessage 11111cb0ef41Sopenharmony_ci[`diagnostics_channel.tracingChannel()`]: #diagnostics_channeltracingchannelnameorchannels 11121cb0ef41Sopenharmony_ci[`diagnostics_channel.unsubscribe(name, onMessage)`]: #diagnostics_channelunsubscribename-onmessage 11131cb0ef41Sopenharmony_ci[`end` event]: #endevent 11141cb0ef41Sopenharmony_ci[`error` event]: #errorevent 11151cb0ef41Sopenharmony_ci[`start` event]: #startevent 11161cb0ef41Sopenharmony_ci[context loss]: async_context.md#troubleshooting-context-loss 1117