Skip to content

Commit 614bc7e

Browse files
committed
lib,test: propagate bypass context through TracingChannel trace methods
Signed-off-by: Divyanshu Sharma <divyanshu88999@gmail.com>
1 parent 24a87f4 commit 614bc7e

2 files changed

Lines changed: 116 additions & 3 deletions

File tree

lib/diagnostics_channel.js

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,7 @@ function boundedChannel(nameOrChannels) {
552552
class TracingChannel {
553553
#callWindow;
554554
#continuationWindow;
555+
#bypassIds = null;
555556

556557
constructor(nameOrChannels) {
557558
// Create a BoundedChannel for start/end (call window)
@@ -601,7 +602,23 @@ class TracingChannel {
601602
this.error?.hasSubscribers;
602603
}
603604

605+
#allBypassed() {
606+
const activeKeys = bypassStorage?.getStore();
607+
if (!activeKeys) return false;
608+
const ids = this.#bypassIds;
609+
if (ids === null) return false;
610+
for (const id of ids) {
611+
if (activeKeys.has(id)) return true;
612+
}
613+
return false;
614+
}
615+
604616
subscribe(handlers, options) {
617+
if (options?.bypassId !== undefined) {
618+
if (this.#bypassIds === null) this.#bypassIds = new SafeSet();
619+
this.#bypassIds.add(options.bypassId);
620+
}
621+
605622
// Subscribe to call window (start/end)
606623
if (handlers.start || handlers.end) {
607624
this.#callWindow.subscribe({
@@ -658,7 +675,7 @@ class TracingChannel {
658675
}
659676

660677
traceSync(fn, context = {}, thisArg, ...args) {
661-
if (!this.hasSubscribers) {
678+
if (!this.hasSubscribers || this.#allBypassed()) {
662679
return ReflectApply(fn, thisArg, args);
663680
}
664681

@@ -678,7 +695,7 @@ class TracingChannel {
678695
}
679696

680697
tracePromise(fn, context = {}, thisArg, ...args) {
681-
if (!this.hasSubscribers) {
698+
if (!this.hasSubscribers || this.#allBypassed()) {
682699
const result = ReflectApply(fn, thisArg, args);
683700
if (typeof result?.then !== 'function') {
684701
emitNonThenableWarning(fn);
@@ -733,7 +750,7 @@ class TracingChannel {
733750
}
734751

735752
traceCallback(fn, position = -1, context = {}, thisArg, ...args) {
736-
if (!this.hasSubscribers) {
753+
if (!this.hasSubscribers || this.#allBypassed()) {
737754
return ReflectApply(fn, thisArg, args);
738755
}
739756

test/parallel/test-diagnostics-channel-bypass.js

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,3 +209,99 @@ const { AsyncLocalStorage } = require('async_hooks');
209209
}), receiver, 'a', 'b');
210210
assert.strictEqual(result, 42);
211211
}
212+
213+
// Test 11: TracingChannel traceSync respects bypass
214+
{
215+
const { tracingChannel } = require('node:diagnostics_channel');
216+
const key = Symbol('tracing-sync');
217+
const tc = tracingChannel('test-bypass-tracing-sync');
218+
const startHandler = common.mustNotCall();
219+
const endHandler = common.mustNotCall();
220+
const errorHandler = common.mustNotCall();
221+
222+
tc.subscribe({
223+
start: startHandler,
224+
end: endHandler,
225+
error: errorHandler,
226+
}, { bypassId: key });
227+
228+
bypass(key, common.mustCall(() => {
229+
tc.traceSync(common.mustCall(), {});
230+
}));
231+
232+
tc.unsubscribe({
233+
start: startHandler,
234+
end: endHandler,
235+
error: errorHandler,
236+
});
237+
}
238+
239+
// Test 12: TracingChannel tracePromise respects bypass
240+
{
241+
const { tracingChannel } = require('node:diagnostics_channel');
242+
const key = Symbol('tracing-promise');
243+
const tc = tracingChannel('test-bypass-tracing-promise');
244+
const startHandler = common.mustNotCall();
245+
const endHandler = common.mustNotCall();
246+
const asyncStartHandler = common.mustNotCall();
247+
const asyncEndHandler = common.mustNotCall();
248+
const done = common.mustCall();
249+
250+
tc.subscribe({
251+
start: startHandler,
252+
end: endHandler,
253+
asyncStart: asyncStartHandler,
254+
asyncEnd: asyncEndHandler,
255+
}, { bypassId: key });
256+
257+
bypass(key, common.mustCall(() => {
258+
return tc.tracePromise(common.mustCall(() => Promise.resolve(42)), {});
259+
})).then(common.mustCall(() => {
260+
tc.unsubscribe({
261+
start: startHandler,
262+
end: endHandler,
263+
asyncStart: asyncStartHandler,
264+
asyncEnd: asyncEndHandler,
265+
});
266+
done();
267+
}));
268+
}
269+
270+
// Test 13: TracingChannel traceCallback respects bypass
271+
{
272+
const { tracingChannel } = require('node:diagnostics_channel');
273+
const key = Symbol('tracing-callback');
274+
const tc = tracingChannel('test-bypass-tracing-callback');
275+
const startHandler = common.mustNotCall();
276+
const endHandler = common.mustNotCall();
277+
const asyncStartHandler = common.mustNotCall();
278+
const asyncEndHandler = common.mustNotCall();
279+
const done = common.mustCall();
280+
281+
tc.subscribe({
282+
start: startHandler,
283+
end: endHandler,
284+
asyncStart: asyncStartHandler,
285+
asyncEnd: asyncEndHandler,
286+
}, { bypassId: key });
287+
288+
bypass(key, common.mustCall(() => {
289+
tc.traceCallback(
290+
common.mustCall((cb) => setImmediate(cb, null, 'result')),
291+
-1,
292+
{},
293+
null,
294+
common.mustCall((err, res) => {
295+
assert.strictEqual(err, null);
296+
assert.strictEqual(res, 'result');
297+
tc.unsubscribe({
298+
start: startHandler,
299+
end: endHandler,
300+
asyncStart: asyncStartHandler,
301+
asyncEnd: asyncEndHandler,
302+
});
303+
done();
304+
})
305+
);
306+
}));
307+
}

0 commit comments

Comments
 (0)