Skip to content

Commit 69743d2

Browse files
author
Stephen Belanger
committed
doc: improve tracing channel docs
1 parent 83f5093 commit 69743d2

1 file changed

Lines changed: 75 additions & 21 deletions

File tree

doc/api/diagnostics_channel.md

Lines changed: 75 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -468,13 +468,13 @@ added:
468468

469469
> Stability: 1 - Experimental
470470
471-
* `store` {AsyncLocalStorage} The store to which to bind the event data
472-
* `transform` {Function} Transform event data before setting the store context
471+
* `store` {AsyncLocalStorage} The store to which to bind the context data
472+
* `transform` {Function} Transform context data before setting the store context
473473

474-
When [`channel.runStores(data, ...)`][] is called, the given event data will
474+
When [`channel.runStores(context, ...)`][] is called, the given context data will
475475
be applied to any store bound to the channel. If the store has already been
476476
bound the previous `transform` function will be replaced with the new one.
477-
The `transform` function may be omitted to have the event data be used as the
477+
The `transform` function may be omitted to set the given context data as the
478478
context directly.
479479

480480
```mjs
@@ -542,7 +542,7 @@ channel.bindStore(store);
542542
channel.unbindStore(store);
543543
```
544544

545-
#### `channel.runStores(data, fn[, thisArg[, ...args]])`
545+
#### `channel.runStores(context, fn[, thisArg[, ...args]])`
546546

547547
<!-- YAML
548548
added:
@@ -551,18 +551,23 @@ added:
551551

552552
> Stability: 1 - Experimental
553553
554-
* `data` {any} Message to send to subscribers and bind to stores
554+
* `context` {any} Message to send to subscribers and bind to stores
555555
* `fn` {Function} Handler to run within the entered storage context
556556
* `thisArg` {any} The receiver to be used for the function call.
557557
* `...args` {any} Optional arguments to pass to the function.
558558

559559
Applies the given data to any AsyncLocalStorage instances bound to the channel
560-
for the duration of the given function, then publishes the [`start` event][]
561-
for the channel within the context of the bound stores. If a transform function
562-
was given to [`channel.bindStore(store)`][] it will be applied to transform the
563-
message data before it becomes the context value for the store. The prior
564-
storage context is accessible from within the transform function in cases where
565-
context linking is required.
560+
for the duration of the given function, then publishes to the channel within
561+
the scope of that data is applied to the stores.
562+
563+
If a transform function was given to [`channel.bindStore(store)`][] it will be
564+
applied to transform the message data before it becomes the context value for
565+
the store. The prior storage context is accessible from within the transform
566+
function in cases where context linking is required.
567+
568+
The context applied to the store should be accesible in any async code which
569+
continues from execution which began during the given function, however
570+
there are some situations in which [context loss][] may occur.
566571

567572
```mjs
568573
import diagnostics_channel from 'node:diagnostics_channel';
@@ -753,7 +758,7 @@ channels.unsubscribe({
753758
});
754759
```
755760

756-
#### `tracingChannel.traceSync(fn[, data[, thisArg[, ...args]]])`
761+
#### `tracingChannel.traceSync(fn[, context[, thisArg[, ...args]]])`
757762

758763
<!-- YAML
759764
added:
@@ -763,15 +768,16 @@ added:
763768
> Stability: 1 - Experimental
764769
765770
* `fn` {Function} Function to wrap a trace around
766-
* `data` {Object} Shared object to correlate events through
771+
* `context` {Object} Shared object to correlate events through
767772
* `thisArg` {any} The receiver to be used for the function call
768773
* `...args` {any} Optional arguments to pass to the function
769774
* Returns: {any} The return value of the given function
770775

771776
Trace a synchronous function call. This will always produce a [`start` event][]
772777
and [`end` event][] around the execution and may produce an [`error` event][]
773778
if the given function throws an error. This will run the given function using
774-
[`channel.runStores(data, ...)`][] on the `start` channel.
779+
[`channel.runStores(context, ...)`][] on the `start` channel which ensures all
780+
events should have any bound stores set to match this trace context.
775781

776782
```mjs
777783
import diagnostics_channel from 'node:diagnostics_channel';
@@ -797,7 +803,7 @@ channels.traceSync(() => {
797803
});
798804
```
799805

800-
#### `tracingChannel.tracePromise(fn[, data[, thisArg[, ...args]]])`
806+
#### `tracingChannel.tracePromise(fn[, context[, thisArg[, ...args]]])`
801807

802808
<!-- YAML
803809
added:
@@ -807,7 +813,7 @@ added:
807813
> Stability: 1 - Experimental
808814
809815
* `fn` {Function} Promise-returning function to wrap a trace around
810-
* `data` {Object} Shared object to correlate trace events through
816+
* `context` {Object} Shared object to correlate trace events through
811817
* `thisArg` {any} The receiver to be used for the function call
812818
* `...args` {any} Optional arguments to pass to the function
813819
* Returns: {Promise} Chained from promise returned by the given function
@@ -818,7 +824,8 @@ function execution, and will produce an [`asyncStart` event][] and
818824
[`asyncEnd` event][] when a promise continuation is reached. It may also
819825
produce an [`error` event][] if the given function throws an error or the
820826
returned promise rejects. This will run the given function using
821-
[`channel.runStores(data, ...)`][] on the `start` channel.
827+
[`channel.runStores(context, ...)`][] on the `start` channel which ensures all
828+
events should have any bound stores set to match this trace context.
822829

823830
```mjs
824831
import diagnostics_channel from 'node:diagnostics_channel';
@@ -844,7 +851,7 @@ channels.tracePromise(async () => {
844851
});
845852
```
846853

847-
#### `tracingChannel.traceCallback(fn[, position[, data[, thisArg[, ...args]]]])`
854+
#### `tracingChannel.traceCallback(fn[, position[, context[, thisArg[, ...args]]]])`
848855

849856
<!-- YAML
850857
added:
@@ -855,7 +862,7 @@ added:
855862
856863
* `fn` {Function} callback using function to wrap a trace around
857864
* `position` {number} Zero-indexed argument position of expected callback
858-
* `data` {Object} Shared object to correlate trace events through
865+
* `context` {Object} Shared object to correlate trace events through
859866
* `thisArg` {any} The receiver to be used for the function call
860867
* `...args` {any} Optional arguments to pass to the function
861868
* Returns: {any} The return value of the given function
@@ -866,7 +873,8 @@ function execution, and will produce a [`asyncStart` event][] and
866873
[`asyncEnd` event][] around the callback execution. It may also produce an
867874
[`error` event][] if the given function throws an error or the returned
868875
promise rejects. This will run the given function using
869-
[`channel.runStores(data, ...)`][] on the `start` channel.
876+
[`channel.runStores(context, ...)`][] on the `start` channel which ensures all
877+
events should have any bound stores set to match this trace context.
870878

871879
The `position` will be -1 by default to indicate the final argument should
872880
be used as the callback.
@@ -897,6 +905,51 @@ channels.traceCallback((arg1, callback) => {
897905
}, thisArg, arg1, callback);
898906
```
899907

908+
The callback will also be run with [`channel.runStores(context, ...)`][] which
909+
enables context loss recovery in some cases.
910+
911+
```mjs
912+
import diagnostics_channel from 'node:diagnostics_channel';
913+
import { AsyncLocalStorage } from 'async_hooks';
914+
915+
const channels = diagnostics_channel.tracingChannel('my-channel');
916+
const myStore = new AsyncLocalStorage();
917+
918+
// The start channel sets the initial store data to something
919+
// and stores that store data value on the trace context object
920+
channels.start.bindStore(myStore, (data) => {
921+
const span = new Span(data);
922+
data.span = span;
923+
return span;
924+
});
925+
926+
// Then asyncStart can restore from that data it stored previously
927+
channels.asyncStart.bindStore(myStore, (data) => {
928+
return data.span;
929+
});
930+
```
931+
932+
```cjs
933+
const diagnostics_channel = require('node:diagnostics_channel');
934+
const { AsyncLocalStorage } = require('async_hooks');
935+
936+
const channels = diagnostics_channel.tracingChannel('my-channel');
937+
const myStore = new AsyncLocalStorage();
938+
939+
// The start channel sets the initial store data to something
940+
// and stores that store data value on the trace context object
941+
channels.start.bindStore(myStore, (data) => {
942+
const span = new Span(data);
943+
data.span = span;
944+
return span;
945+
});
946+
947+
// Then asyncStart can restore from that data it stored previously
948+
channels.asyncStart.bindStore(myStore, (data) => {
949+
return data.span;
950+
});
951+
```
952+
900953
### TracingChannel Channels
901954

902955
A TracingChannel is a collection of several diagnostics\_channels representing
@@ -1079,6 +1132,7 @@ added: v16.18.0
10791132
Emitted when a new thread is created.
10801133

10811134
[TracingChannel Channels]: #tracingchannel-channels
1135+
[context loss]: async_context.md#troubleshooting-context-loss
10821136
[`'uncaughtException'`]: process.md#event-uncaughtexception
10831137
[`TracingChannel`]: #class-tracingchannel
10841138
[`Worker`]: worker_threads.md#class-worker

0 commit comments

Comments
 (0)