Skip to content

Commit f7f593e

Browse files
committed
doc: address Flarna review feedback on bypass documentation
Signed-off-by: Divyanshu Sharma <divyanshu88999@gmail.com>
1 parent 149f073 commit f7f593e

1 file changed

Lines changed: 23 additions & 98 deletions

File tree

doc/api/diagnostics_channel.md

Lines changed: 23 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,6 @@ if (channel.hasSubscribers) {
7272

7373
// Unsubscribe from the channel
7474
diagnostics_channel.unsubscribe('my-channel', onMessage);
75-
76-
// Use bypass() to skip opted-in subscribers during internal calls
77-
const kMyTool = Symbol('my-tool');
78-
diagnostics_channel.channel('my-channel').subscribe(onMessage, { bypassId: kMyTool });
79-
diagnostics_channel.bypass(kMyTool, () => {
80-
// onMessage will NOT be called for publishes inside here
81-
});
8275
```
8376

8477
```cjs
@@ -104,13 +97,6 @@ if (channel.hasSubscribers) {
10497

10598
// Unsubscribe from the channel
10699
diagnostics_channel.unsubscribe('my-channel', onMessage);
107-
108-
// Use bypass() to skip opted-in subscribers during internal calls
109-
const kMyTool = Symbol('my-tool');
110-
diagnostics_channel.channel('my-channel').subscribe(onMessage, { bypassId: kMyTool });
111-
diagnostics_channel.bypass(kMyTool, () => {
112-
// onMessage will NOT be called for publishes inside here
113-
});
114100
```
115101

116102
#### `diagnostics_channel.hasSubscribers(name)`
@@ -251,21 +237,20 @@ diagnostics_channel.unsubscribe('my-channel', onMessage);
251237
added: REPLACEME
252238
-->
253239

254-
* `key` {symbol|Object} The bypass identity token. Must match the
255-
`bypassId` used when subscribing.
240+
> Stability: 1.1 - Active development
241+
242+
* `key` {symbol|Object} The bypass identity token. Subscribers and
243+
stores registered with the same `bypassId` value will be skipped
244+
while this function executes.
256245
* `fn` {Function} The function to run with bypass active.
257246
* `thisArg` {any} The receiver to use for the function call.
258247
* `...args` {any} Optional arguments to pass to the function.
259248
* Returns: {any} The return value of `fn`.
260249

261-
Runs `fn` with the given `key` active in the current async context.
262-
Any channel subscribers or bound stores that were registered with
263-
`{ bypassId: key }` will be skipped for the duration of `fn` and
264-
any async continuations (Promises, timers, microtasks) within it.
265-
266-
This is designed for observability tooling (APM agents, tracers)
267-
that need to prevent recursive instrumentation when their own
268-
internal code calls into an instrumented library.
250+
Calls `fn` and skips any channel subscribers or bound stores that
251+
were registered with a matching `bypassId` key. The skip behavior
252+
also applies to any async continuations (Promises, timers,
253+
microtasks) within `fn`.
269254

270255
```mjs
271256
import diagnostics_channel from 'node:diagnostics_channel';
@@ -277,7 +262,7 @@ const { channel, bypass } = diagnostics_channel;
277262
const kMyTracer = Symbol('my-tracer');
278263

279264
// Subscribe to HTTP requests, but opt into bypass
280-
channel('http.client.request').subscribe((message) => {
265+
channel('http.client.request.start').subscribe((message) => {
281266
console.log('HTTP request:', message.url);
282267
}, { bypassId: kMyTracer });
283268

@@ -304,7 +289,7 @@ const { channel, bypass } = diagnostics_channel;
304289
const kMyTracer = Symbol('my-tracer');
305290

306291
// Subscribe to HTTP requests, but opt into bypass
307-
channel('http.client.request').subscribe((message) => {
292+
channel('http.client.request.start').subscribe((message) => {
308293
console.log('HTTP request:', message.url);
309294
}, { bypassId: kMyTracer });
310295

@@ -331,14 +316,14 @@ The bypass context propagates across async boundaries:
331316
// bypass() works across Promise boundaries
332317
await bypass(kMyTracer, async () => {
333318
await someAsyncOperation(); // still bypassed
334-
channel('http.client.request').publish({}); // subscriber skipped
319+
channel('http.client.request.start').publish({}); // subscriber skipped
335320
});
336321

337322
// And across timers
338323
bypass(kMyTracer, () => {
339324
setImmediate(() => {
340325
// Still bypassed here
341-
channel('http.client.request').publish({});
326+
channel('http.client.request.start').publish({});
342327
});
343328
});
344329
```
@@ -347,11 +332,11 @@ bypass(kMyTracer, () => {
347332
(async () => {
348333
await bypass(kMyTracer, async () => {
349334
await someAsyncOperation();
350-
channel('http.client.request').publish({});
335+
channel('http.client.request.start').publish({});
351336
});
352337
bypass(kMyTracer, () => {
353338
setImmediate(() => {
354-
channel('http.client.request').publish({});
339+
channel('http.client.request.start').publish({});
355340
});
356341
});
357342
})();
@@ -364,12 +349,12 @@ interfering with each other:
364349
const kToolA = Symbol('tool-a');
365350
const kToolB = Symbol('tool-b');
366351

367-
channel('http.client.request').subscribe(handlerA, { bypassId: kToolA });
368-
channel('http.client.request').subscribe(handlerB, { bypassId: kToolB });
352+
channel('http.client.request.start').subscribe(handlerA, { bypassId: kToolA });
353+
channel('http.client.request.start').subscribe(handlerB, { bypassId: kToolB });
369354

370355
// Only handlerA is skipped, handlerB still fires
371356
bypass(kToolA, () => {
372-
channel('http.client.request').publish({});
357+
channel('http.client.request.start').publish({});
373358
});
374359
```
375360

@@ -381,12 +366,12 @@ const { channel, bypass } = diagnostics_channel;
381366
const kToolA = Symbol('tool-a');
382367
const kToolB = Symbol('tool-b');
383368

384-
channel('http.client.request').subscribe(handlerA, { bypassId: kToolA });
385-
channel('http.client.request').subscribe(handlerB, { bypassId: kToolB });
369+
channel('http.client.request.start').subscribe(handlerA, { bypassId: kToolA });
370+
channel('http.client.request.start').subscribe(handlerB, { bypassId: kToolB });
386371

387372
// Only handlerA is skipped, handlerB still fires
388373
bypass(kToolA, () => {
389-
channel('http.client.request').publish({});
374+
channel('http.client.request.start').publish({});
390375
});
391376
```
392377

@@ -627,33 +612,7 @@ channel.subscribe((message, name) => {
627612
});
628613
```
629614

630-
To opt this subscriber into bypass behavior:
631-
632-
```mjs
633-
import diagnostics_channel from 'node:diagnostics_channel';
634-
635-
const { channel, bypass } = diagnostics_channel;
636-
const kMyTool = Symbol('my-tool');
637-
const ch = channel('http.client.request');
638-
639-
// This handler will be skipped when bypass(kMyTool, fn) is active
640-
ch.subscribe((message) => {
641-
// handle message
642-
}, { bypassId: kMyTool });
643-
```
644-
645-
```cjs
646-
const diagnostics_channel = require('node:diagnostics_channel');
647-
648-
const { channel, bypass } = diagnostics_channel;
649-
const kMyTool = Symbol('my-tool');
650-
const ch = channel('http.client.request');
651-
652-
// This handler will be skipped when bypass(kMyTool, fn) is active
653-
ch.subscribe((message) => {
654-
// handle message
655-
}, { bypassId: kMyTool });
656-
```
615+
See [`diagnostics_channel.bypass()`][] for usage with `bypassId`.
657616

658617
#### `channel.unsubscribe(onMessage)`
659618

@@ -767,37 +726,7 @@ channel.bindStore(store, (data) => {
767726
});
768727
```
769728

770-
To opt this bound store into bypass behavior:
771-
772-
```mjs
773-
import diagnostics_channel from 'node:diagnostics_channel';
774-
import { AsyncLocalStorage } from 'node:async_hooks';
775-
776-
const { channel, bypass } = diagnostics_channel;
777-
const kMyTool = Symbol('my-tool');
778-
const ch = channel('http.client.request');
779-
const store = new AsyncLocalStorage();
780-
781-
// This store will NOT be entered when bypass(kMyTool, fn) is active
782-
ch.bindStore(store, (data) => ({ requestId: data.id }), {
783-
bypassId: kMyTool,
784-
});
785-
```
786-
787-
```cjs
788-
const diagnostics_channel = require('node:diagnostics_channel');
789-
const { AsyncLocalStorage } = require('node:async_hooks');
790-
791-
const { channel, bypass } = diagnostics_channel;
792-
const kMyTool = Symbol('my-tool');
793-
const ch = channel('http.client.request');
794-
const store = new AsyncLocalStorage();
795-
796-
// This store will NOT be entered when bypass(kMyTool, fn) is active
797-
ch.bindStore(store, (data) => ({ requestId: data.id }), {
798-
bypassId: kMyTool,
799-
});
800-
```
729+
See [`diagnostics_channel.bypass()`][] for usage with `bypassId`.
801730

802731
#### `channel.unbindStore(store)`
803732

@@ -996,10 +925,6 @@ dynamically.
996925
added:
997926
- v19.9.0
998927
- v18.19.0
999-
changes:
1000-
- version: REPLACEME
1001-
pr-url: https://github.com/nodejs/node/pull/63651
1002-
description: Added `options.bypassId` parameter.
1003928
-->
1004929

1005930
* `subscribers` {Object} Set of [TracingChannel Channels][] subscribers

0 commit comments

Comments
 (0)