-
Notifications
You must be signed in to change notification settings - Fork 30.1k
/
diagnostics_channel.md
1293 lines (953 loc) Β· 35.7 KB
/
diagnostics_channel.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Diagnostics Channel
<!-- YAML
added:
- v15.1.0
- v14.17.0
changes:
- version:
- v19.2.0
- v18.13.0
pr-url: https://github.com/nodejs/node/pull/45290
description: diagnostics_channel is now Stable.
-->
<!--introduced_in=v15.1.0-->
> Stability: 2 - Stable
<!-- source_link=lib/diagnostics_channel.js -->
The `node:diagnostics_channel` module provides an API to create named channels
to report arbitrary message data for diagnostics purposes.
It can be accessed using:
```mjs
import diagnostics_channel from 'node:diagnostics_channel';
```
```cjs
const diagnostics_channel = require('node:diagnostics_channel');
```
It is intended that a module writer wanting to report diagnostics messages
will create one or many top-level channels to report messages through.
Channels may also be acquired at runtime but it is not encouraged
due to the additional overhead of doing so. Channels may be exported for
convenience, but as long as the name is known it can be acquired anywhere.
If you intend for your module to produce diagnostics data for others to
consume it is recommended that you include documentation of what named
channels are used along with the shape of the message data. Channel names
should generally include the module name to avoid collisions with data from
other modules.
## Public API
### Overview
Following is a simple overview of the public API.
```mjs
import diagnostics_channel from 'node:diagnostics_channel';
// Get a reusable channel object
const channel = diagnostics_channel.channel('my-channel');
function onMessage(message, name) {
// Received data
}
// Subscribe to the channel
diagnostics_channel.subscribe('my-channel', onMessage);
// Check if the channel has an active subscriber
if (channel.hasSubscribers) {
// Publish data to the channel
channel.publish({
some: 'data',
});
}
// Unsubscribe from the channel
diagnostics_channel.unsubscribe('my-channel', onMessage);
```
```cjs
const diagnostics_channel = require('node:diagnostics_channel');
// Get a reusable channel object
const channel = diagnostics_channel.channel('my-channel');
function onMessage(message, name) {
// Received data
}
// Subscribe to the channel
diagnostics_channel.subscribe('my-channel', onMessage);
// Check if the channel has an active subscriber
if (channel.hasSubscribers) {
// Publish data to the channel
channel.publish({
some: 'data',
});
}
// Unsubscribe from the channel
diagnostics_channel.unsubscribe('my-channel', onMessage);
```
#### `diagnostics_channel.hasSubscribers(name)`
<!-- YAML
added:
- v15.1.0
- v14.17.0
-->
* `name` {string|symbol} The channel name
* Returns: {boolean} If there are active subscribers
Check if there are active subscribers to the named channel. This is helpful if
the message you want to send might be expensive to prepare.
This API is optional but helpful when trying to publish messages from very
performance-sensitive code.
```mjs
import diagnostics_channel from 'node:diagnostics_channel';
if (diagnostics_channel.hasSubscribers('my-channel')) {
// There are subscribers, prepare and publish message
}
```
```cjs
const diagnostics_channel = require('node:diagnostics_channel');
if (diagnostics_channel.hasSubscribers('my-channel')) {
// There are subscribers, prepare and publish message
}
```
#### `diagnostics_channel.channel(name)`
<!-- YAML
added:
- v15.1.0
- v14.17.0
-->
* `name` {string|symbol} The channel name
* Returns: {Channel} The named channel object
This is the primary entry-point for anyone wanting to publish to a named
channel. It produces a channel object which is optimized to reduce overhead at
publish time as much as possible.
```mjs
import diagnostics_channel from 'node:diagnostics_channel';
const channel = diagnostics_channel.channel('my-channel');
```
```cjs
const diagnostics_channel = require('node:diagnostics_channel');
const channel = diagnostics_channel.channel('my-channel');
```
#### `diagnostics_channel.subscribe(name, onMessage)`
<!-- YAML
added:
- v18.7.0
- v16.17.0
-->
* `name` {string|symbol} The channel name
* `onMessage` {Function} The handler to receive channel messages
* `message` {any} The message data
* `name` {string|symbol} The name of the channel
Register a message handler to subscribe to this channel. This message handler
will be run synchronously whenever a message is published to the channel. Any
errors thrown in the message handler will trigger an [`'uncaughtException'`][].
```mjs
import diagnostics_channel from 'node:diagnostics_channel';
diagnostics_channel.subscribe('my-channel', (message, name) => {
// Received data
});
```
```cjs
const diagnostics_channel = require('node:diagnostics_channel');
diagnostics_channel.subscribe('my-channel', (message, name) => {
// Received data
});
```
#### `diagnostics_channel.unsubscribe(name, onMessage)`
<!-- YAML
added:
- v18.7.0
- v16.17.0
-->
* `name` {string|symbol} The channel name
* `onMessage` {Function} The previous subscribed handler to remove
* Returns: {boolean} `true` if the handler was found, `false` otherwise.
Remove a message handler previously registered to this channel with
[`diagnostics_channel.subscribe(name, onMessage)`][].
```mjs
import diagnostics_channel from 'node:diagnostics_channel';
function onMessage(message, name) {
// Received data
}
diagnostics_channel.subscribe('my-channel', onMessage);
diagnostics_channel.unsubscribe('my-channel', onMessage);
```
```cjs
const diagnostics_channel = require('node:diagnostics_channel');
function onMessage(message, name) {
// Received data
}
diagnostics_channel.subscribe('my-channel', onMessage);
diagnostics_channel.unsubscribe('my-channel', onMessage);
```
#### `diagnostics_channel.tracingChannel(nameOrChannels)`
<!-- YAML
added:
- v19.9.0
- v18.19.0
-->
> Stability: 1 - Experimental
* `nameOrChannels` {string|TracingChannel} Channel name or
object containing all the [TracingChannel Channels][]
* Returns: {TracingChannel} Collection of channels to trace with
Creates a [`TracingChannel`][] wrapper for the given
[TracingChannel Channels][]. If a name is given, the corresponding tracing
channels will be created in the form of `tracing:${name}:${eventType}` where
`eventType` corresponds to the types of [TracingChannel Channels][].
```mjs
import diagnostics_channel from 'node:diagnostics_channel';
const channelsByName = diagnostics_channel.tracingChannel('my-channel');
// or...
const channelsByCollection = diagnostics_channel.tracingChannel({
start: diagnostics_channel.channel('tracing:my-channel:start'),
end: diagnostics_channel.channel('tracing:my-channel:end'),
asyncStart: diagnostics_channel.channel('tracing:my-channel:asyncStart'),
asyncEnd: diagnostics_channel.channel('tracing:my-channel:asyncEnd'),
error: diagnostics_channel.channel('tracing:my-channel:error'),
});
```
```cjs
const diagnostics_channel = require('node:diagnostics_channel');
const channelsByName = diagnostics_channel.tracingChannel('my-channel');
// or...
const channelsByCollection = diagnostics_channel.tracingChannel({
start: diagnostics_channel.channel('tracing:my-channel:start'),
end: diagnostics_channel.channel('tracing:my-channel:end'),
asyncStart: diagnostics_channel.channel('tracing:my-channel:asyncStart'),
asyncEnd: diagnostics_channel.channel('tracing:my-channel:asyncEnd'),
error: diagnostics_channel.channel('tracing:my-channel:error'),
});
```
### Class: `Channel`
<!-- YAML
added:
- v15.1.0
- v14.17.0
-->
The class `Channel` represents an individual named channel within the data
pipeline. It is used to track subscribers and to publish messages when there
are subscribers present. It exists as a separate object to avoid channel
lookups at publish time, enabling very fast publish speeds and allowing
for heavy use while incurring very minimal cost. Channels are created with
[`diagnostics_channel.channel(name)`][], constructing a channel directly
with `new Channel(name)` is not supported.
#### `channel.hasSubscribers`
<!-- YAML
added:
- v15.1.0
- v14.17.0
-->
* Returns: {boolean} If there are active subscribers
Check if there are active subscribers to this channel. This is helpful if
the message you want to send might be expensive to prepare.
This API is optional but helpful when trying to publish messages from very
performance-sensitive code.
```mjs
import diagnostics_channel from 'node:diagnostics_channel';
const channel = diagnostics_channel.channel('my-channel');
if (channel.hasSubscribers) {
// There are subscribers, prepare and publish message
}
```
```cjs
const diagnostics_channel = require('node:diagnostics_channel');
const channel = diagnostics_channel.channel('my-channel');
if (channel.hasSubscribers) {
// There are subscribers, prepare and publish message
}
```
#### `channel.publish(message)`
<!-- YAML
added:
- v15.1.0
- v14.17.0
-->
* `message` {any} The message to send to the channel subscribers
Publish a message to any subscribers to the channel. This will trigger
message handlers synchronously so they will execute within the same context.
```mjs
import diagnostics_channel from 'node:diagnostics_channel';
const channel = diagnostics_channel.channel('my-channel');
channel.publish({
some: 'message',
});
```
```cjs
const diagnostics_channel = require('node:diagnostics_channel');
const channel = diagnostics_channel.channel('my-channel');
channel.publish({
some: 'message',
});
```
#### `channel.subscribe(onMessage)`
<!-- YAML
added:
- v15.1.0
- v14.17.0
deprecated:
- v18.7.0
- v16.17.0
-->
> Stability: 0 - Deprecated: Use [`diagnostics_channel.subscribe(name, onMessage)`][]
* `onMessage` {Function} The handler to receive channel messages
* `message` {any} The message data
* `name` {string|symbol} The name of the channel
Register a message handler to subscribe to this channel. This message handler
will be run synchronously whenever a message is published to the channel. Any
errors thrown in the message handler will trigger an [`'uncaughtException'`][].
```mjs
import diagnostics_channel from 'node:diagnostics_channel';
const channel = diagnostics_channel.channel('my-channel');
channel.subscribe((message, name) => {
// Received data
});
```
```cjs
const diagnostics_channel = require('node:diagnostics_channel');
const channel = diagnostics_channel.channel('my-channel');
channel.subscribe((message, name) => {
// Received data
});
```
#### `channel.unsubscribe(onMessage)`
<!-- YAML
added:
- v15.1.0
- v14.17.0
deprecated:
- v18.7.0
- v16.17.0
changes:
- version:
- v17.1.0
- v16.14.0
- v14.19.0
pr-url: https://github.com/nodejs/node/pull/40433
description: Added return value. Added to channels without subscribers.
-->
> Stability: 0 - Deprecated: Use [`diagnostics_channel.unsubscribe(name, onMessage)`][]
* `onMessage` {Function} The previous subscribed handler to remove
* Returns: {boolean} `true` if the handler was found, `false` otherwise.
Remove a message handler previously registered to this channel with
[`channel.subscribe(onMessage)`][].
```mjs
import diagnostics_channel from 'node:diagnostics_channel';
const channel = diagnostics_channel.channel('my-channel');
function onMessage(message, name) {
// Received data
}
channel.subscribe(onMessage);
channel.unsubscribe(onMessage);
```
```cjs
const diagnostics_channel = require('node:diagnostics_channel');
const channel = diagnostics_channel.channel('my-channel');
function onMessage(message, name) {
// Received data
}
channel.subscribe(onMessage);
channel.unsubscribe(onMessage);
```
#### `channel.bindStore(store[, transform])`
<!-- YAML
added:
- v19.9.0
- v18.19.0
-->
> Stability: 1 - Experimental
* `store` {AsyncLocalStorage} The store to which to bind the context data
* `transform` {Function} Transform context data before setting the store context
When [`channel.runStores(context, ...)`][] is called, the given context data
will be applied to any store bound to the channel. If the store has already been
bound the previous `transform` function will be replaced with the new one.
The `transform` function may be omitted to set the given context data as the
context directly.
```mjs
import diagnostics_channel from 'node:diagnostics_channel';
import { AsyncLocalStorage } from 'node:async_hooks';
const store = new AsyncLocalStorage();
const channel = diagnostics_channel.channel('my-channel');
channel.bindStore(store, (data) => {
return { data };
});
```
```cjs
const diagnostics_channel = require('node:diagnostics_channel');
const { AsyncLocalStorage } = require('node:async_hooks');
const store = new AsyncLocalStorage();
const channel = diagnostics_channel.channel('my-channel');
channel.bindStore(store, (data) => {
return { data };
});
```
#### `channel.unbindStore(store)`
<!-- YAML
added:
- v19.9.0
- v18.19.0
-->
> Stability: 1 - Experimental
* `store` {AsyncLocalStorage} The store to unbind from the channel.
* Returns: {boolean} `true` if the store was found, `false` otherwise.
Remove a message handler previously registered to this channel with
[`channel.bindStore(store)`][].
```mjs
import diagnostics_channel from 'node:diagnostics_channel';
import { AsyncLocalStorage } from 'node:async_hooks';
const store = new AsyncLocalStorage();
const channel = diagnostics_channel.channel('my-channel');
channel.bindStore(store);
channel.unbindStore(store);
```
```cjs
const diagnostics_channel = require('node:diagnostics_channel');
const { AsyncLocalStorage } = require('node:async_hooks');
const store = new AsyncLocalStorage();
const channel = diagnostics_channel.channel('my-channel');
channel.bindStore(store);
channel.unbindStore(store);
```
#### `channel.runStores(context, fn[, thisArg[, ...args]])`
<!-- YAML
added:
- v19.9.0
- v18.19.0
-->
> Stability: 1 - Experimental
* `context` {any} Message to send to subscribers and bind to stores
* `fn` {Function} Handler to run within the entered storage context
* `thisArg` {any} The receiver to be used for the function call.
* `...args` {any} Optional arguments to pass to the function.
Applies the given data to any AsyncLocalStorage instances bound to the channel
for the duration of the given function, then publishes to the channel within
the scope of that data is applied to the stores.
If a transform function was given to [`channel.bindStore(store)`][] it will be
applied to transform the message data before it becomes the context value for
the store. The prior storage context is accessible from within the transform
function in cases where context linking is required.
The context applied to the store should be accessible in any async code which
continues from execution which began during the given function, however
there are some situations in which [context loss][] may occur.
```mjs
import diagnostics_channel from 'node:diagnostics_channel';
import { AsyncLocalStorage } from 'node:async_hooks';
const store = new AsyncLocalStorage();
const channel = diagnostics_channel.channel('my-channel');
channel.bindStore(store, (message) => {
const parent = store.getStore();
return new Span(message, parent);
});
channel.runStores({ some: 'message' }, () => {
store.getStore(); // Span({ some: 'message' })
});
```
```cjs
const diagnostics_channel = require('node:diagnostics_channel');
const { AsyncLocalStorage } = require('node:async_hooks');
const store = new AsyncLocalStorage();
const channel = diagnostics_channel.channel('my-channel');
channel.bindStore(store, (message) => {
const parent = store.getStore();
return new Span(message, parent);
});
channel.runStores({ some: 'message' }, () => {
store.getStore(); // Span({ some: 'message' })
});
```
### Class: `TracingChannel`
<!-- YAML
added:
- v19.9.0
- v18.19.0
-->
> Stability: 1 - Experimental
The class `TracingChannel` is a collection of [TracingChannel Channels][] which
together express a single traceable action. It is used to formalize and
simplify the process of producing events for tracing application flow.
[`diagnostics_channel.tracingChannel()`][] is used to construct a
`TracingChannel`. As with `Channel` it is recommended to create and reuse a
single `TracingChannel` at the top-level of the file rather than creating them
dynamically.
#### `tracingChannel.subscribe(subscribers)`
<!-- YAML
added:
- v19.9.0
- v18.19.0
-->
> Stability: 1 - Experimental
* `subscribers` {Object} Set of [TracingChannel Channels][] subscribers
* `start` {Function} The [`start` event][] subscriber
* `end` {Function} The [`end` event][] subscriber
* `asyncStart` {Function} The [`asyncStart` event][] subscriber
* `asyncEnd` {Function} The [`asyncEnd` event][] subscriber
* `error` {Function} The [`error` event][] subscriber
Helper to subscribe a collection of functions to the corresponding channels.
This is the same as calling [`channel.subscribe(onMessage)`][] on each channel
individually.
```mjs
import diagnostics_channel from 'node:diagnostics_channel';
const channels = diagnostics_channel.tracingChannel('my-channel');
channels.subscribe({
start(message) {
// Handle start message
},
end(message) {
// Handle end message
},
asyncStart(message) {
// Handle asyncStart message
},
asyncEnd(message) {
// Handle asyncEnd message
},
error(message) {
// Handle error message
},
});
```
```cjs
const diagnostics_channel = require('node:diagnostics_channel');
const channels = diagnostics_channel.tracingChannel('my-channel');
channels.subscribe({
start(message) {
// Handle start message
},
end(message) {
// Handle end message
},
asyncStart(message) {
// Handle asyncStart message
},
asyncEnd(message) {
// Handle asyncEnd message
},
error(message) {
// Handle error message
},
});
```
#### `tracingChannel.unsubscribe(subscribers)`
<!-- YAML
added:
- v19.9.0
- v18.19.0
-->
> Stability: 1 - Experimental
* `subscribers` {Object} Set of [TracingChannel Channels][] subscribers
* `start` {Function} The [`start` event][] subscriber
* `end` {Function} The [`end` event][] subscriber
* `asyncStart` {Function} The [`asyncStart` event][] subscriber
* `asyncEnd` {Function} The [`asyncEnd` event][] subscriber
* `error` {Function} The [`error` event][] subscriber
* Returns: {boolean} `true` if all handlers were successfully unsubscribed,
and `false` otherwise.
Helper to unsubscribe a collection of functions from the corresponding channels.
This is the same as calling [`channel.unsubscribe(onMessage)`][] on each channel
individually.
```mjs
import diagnostics_channel from 'node:diagnostics_channel';
const channels = diagnostics_channel.tracingChannel('my-channel');
channels.unsubscribe({
start(message) {
// Handle start message
},
end(message) {
// Handle end message
},
asyncStart(message) {
// Handle asyncStart message
},
asyncEnd(message) {
// Handle asyncEnd message
},
error(message) {
// Handle error message
},
});
```
```cjs
const diagnostics_channel = require('node:diagnostics_channel');
const channels = diagnostics_channel.tracingChannel('my-channel');
channels.unsubscribe({
start(message) {
// Handle start message
},
end(message) {
// Handle end message
},
asyncStart(message) {
// Handle asyncStart message
},
asyncEnd(message) {
// Handle asyncEnd message
},
error(message) {
// Handle error message
},
});
```
#### `tracingChannel.traceSync(fn[, context[, thisArg[, ...args]]])`
<!-- YAML
added:
- v19.9.0
- v18.19.0
-->
> Stability: 1 - Experimental
* `fn` {Function} Function to wrap a trace around
* `context` {Object} Shared object to correlate events through
* `thisArg` {any} The receiver to be used for the function call
* `...args` {any} Optional arguments to pass to the function
* Returns: {any} The return value of the given function
Trace a synchronous function call. This will always produce a [`start` event][]
and [`end` event][] around the execution and may produce an [`error` event][]
if the given function throws an error. This will run the given function using
[`channel.runStores(context, ...)`][] on the `start` channel which ensures all
events should have any bound stores set to match this trace context.
To ensure only correct trace graphs are formed, events will only be published
if subscribers are present prior to starting the trace. Subscriptions which are
added after the trace begins will not receive future events from that trace,
only future traces will be seen.
```mjs
import diagnostics_channel from 'node:diagnostics_channel';
const channels = diagnostics_channel.tracingChannel('my-channel');
channels.traceSync(() => {
// Do something
}, {
some: 'thing',
});
```
```cjs
const diagnostics_channel = require('node:diagnostics_channel');
const channels = diagnostics_channel.tracingChannel('my-channel');
channels.traceSync(() => {
// Do something
}, {
some: 'thing',
});
```
#### `tracingChannel.tracePromise(fn[, context[, thisArg[, ...args]]])`
<!-- YAML
added:
- v19.9.0
- v18.19.0
-->
> Stability: 1 - Experimental
* `fn` {Function} Promise-returning function to wrap a trace around
* `context` {Object} Shared object to correlate trace events through
* `thisArg` {any} The receiver to be used for the function call
* `...args` {any} Optional arguments to pass to the function
* Returns: {Promise} Chained from promise returned by the given function
Trace a promise-returning function call. This will always produce a
[`start` event][] and [`end` event][] around the synchronous portion of the
function execution, and will produce an [`asyncStart` event][] and
[`asyncEnd` event][] when a promise continuation is reached. It may also
produce an [`error` event][] if the given function throws an error or the
returned promise rejects. This will run the given function using
[`channel.runStores(context, ...)`][] on the `start` channel which ensures all
events should have any bound stores set to match this trace context.
To ensure only correct trace graphs are formed, events will only be published
if subscribers are present prior to starting the trace. Subscriptions which are
added after the trace begins will not receive future events from that trace,
only future traces will be seen.
```mjs
import diagnostics_channel from 'node:diagnostics_channel';
const channels = diagnostics_channel.tracingChannel('my-channel');
channels.tracePromise(async () => {
// Do something
}, {
some: 'thing',
});
```
```cjs
const diagnostics_channel = require('node:diagnostics_channel');
const channels = diagnostics_channel.tracingChannel('my-channel');
channels.tracePromise(async () => {
// Do something
}, {
some: 'thing',
});
```
#### `tracingChannel.traceCallback(fn, position, context, thisArg, ...args)`
<!-- YAML
added:
- v19.9.0
- v18.19.0
-->
> Stability: 1 - Experimental
* `fn` {Function} callback using function to wrap a trace around
* `position` {number} Zero-indexed argument position of expected callback
(defaults to last argument if `undefined` is passed)
* `context` {Object} Shared object to correlate trace events through (defaults
to `{}` if `undefined` is passed)
* `thisArg` {any} The receiver to be used for the function call
* `...args` {any} arguments to pass to the function (must include the callback)
* Returns: {any} The return value of the given function
Trace a callback-receiving function call. The callback is expected to follow
the error as first arg convention typically used. This will always produce a
[`start` event][] and [`end` event][] around the synchronous portion of the
function execution, and will produce a [`asyncStart` event][] and
[`asyncEnd` event][] around the callback execution. It may also produce an
[`error` event][] if the given function throws or the first argument passed to
the callback is set. This will run the given function using
[`channel.runStores(context, ...)`][] on the `start` channel which ensures all
events should have any bound stores set to match this trace context.
To ensure only correct trace graphs are formed, events will only be published
if subscribers are present prior to starting the trace. Subscriptions which are
added after the trace begins will not receive future events from that trace,
only future traces will be seen.
```mjs
import diagnostics_channel from 'node:diagnostics_channel';
const channels = diagnostics_channel.tracingChannel('my-channel');
channels.traceCallback((arg1, callback) => {
// Do something
callback(null, 'result');
}, 1, {
some: 'thing',
}, thisArg, arg1, callback);
```
```cjs
const diagnostics_channel = require('node:diagnostics_channel');
const channels = diagnostics_channel.tracingChannel('my-channel');
channels.traceCallback((arg1, callback) => {
// Do something
callback(null, 'result');
}, {
some: 'thing',
}, thisArg, arg1, callback);
```
The callback will also be run with [`channel.runStores(context, ...)`][] which
enables context loss recovery in some cases.
```mjs
import diagnostics_channel from 'node:diagnostics_channel';
import { AsyncLocalStorage } from 'node:async_hooks';
const channels = diagnostics_channel.tracingChannel('my-channel');
const myStore = new AsyncLocalStorage();
// The start channel sets the initial store data to something
// and stores that store data value on the trace context object
channels.start.bindStore(myStore, (data) => {
const span = new Span(data);
data.span = span;
return span;
});
// Then asyncStart can restore from that data it stored previously
channels.asyncStart.bindStore(myStore, (data) => {
return data.span;
});
```
```cjs
const diagnostics_channel = require('node:diagnostics_channel');
const { AsyncLocalStorage } = require('node:async_hooks');
const channels = diagnostics_channel.tracingChannel('my-channel');
const myStore = new AsyncLocalStorage();
// The start channel sets the initial store data to something
// and stores that store data value on the trace context object
channels.start.bindStore(myStore, (data) => {
const span = new Span(data);
data.span = span;
return span;
});
// Then asyncStart can restore from that data it stored previously
channels.asyncStart.bindStore(myStore, (data) => {
return data.span;
});
```
#### `tracingChannel.hasSubscribers`
<!-- YAML
added:
- v22.0.0
- v20.13.0
-->
> Stability: 1 - Experimental
* Returns: {boolean} `true` if any of the individual channels has a subscriber,
`false` if not.
This is a helper method available on a [`TracingChannel`][] instance to check if
any of the [TracingChannel Channels][] have subscribers. A `true` is returned if
any of them have at least one subscriber, a `false` is returned otherwise.
```mjs
import diagnostics_channel from 'node:diagnostics_channel';
const channels = diagnostics_channel.tracingChannel('my-channel');