-
Notifications
You must be signed in to change notification settings - Fork 30.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
diagnostics_channel: add tracing channel #44943
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
6a950b4
lib: add tracing channel to diagnostics_channel
db68463
diagnostics_channel: throw when callback missing
568b7ea
doc: improve TracingChannel docs
366f973
diagnostics_channel: change traceCallback position to -1
7c2c401
diagnostics_channel: always runStores for tracing channel events
9a1e79b
diagnostics_channel: only runStores where helpful
79b7f56
doc: improve tracing channel docs
51e9418
test: test tracing channel run stores behaviour
c23e772
fixup! test: test tracing channel run stores behaviour
Trott File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const dc = require('diagnostics_channel'); | ||
const { AsyncLocalStorage } = require('async_hooks'); | ||
|
||
let n = 0; | ||
const thisArg = new Date(); | ||
const inputs = [ | ||
{ foo: 'bar' }, | ||
{ baz: 'buz' }, | ||
]; | ||
|
||
const channel = dc.channel('test'); | ||
|
||
// Bind a storage directly to published data | ||
const store1 = new AsyncLocalStorage(); | ||
channel.bindStore(store1); | ||
let store1bound = true; | ||
|
||
// Bind a store with transformation of published data | ||
const store2 = new AsyncLocalStorage(); | ||
channel.bindStore(store2, common.mustCall((data) => { | ||
assert.strictEqual(data, inputs[n]); | ||
return { data }; | ||
}, 4)); | ||
|
||
// Regular subscribers should see publishes from runStores calls | ||
channel.subscribe(common.mustCall((data) => { | ||
if (store1bound) { | ||
assert.deepStrictEqual(data, store1.getStore()); | ||
} | ||
assert.deepStrictEqual({ data }, store2.getStore()); | ||
assert.strictEqual(data, inputs[n]); | ||
}, 4)); | ||
|
||
// Verify stores are empty before run | ||
assert.strictEqual(store1.getStore(), undefined); | ||
assert.strictEqual(store2.getStore(), undefined); | ||
|
||
channel.runStores(inputs[n], common.mustCall(function(a, b) { | ||
// Verify this and argument forwarding | ||
assert.strictEqual(this, thisArg); | ||
assert.strictEqual(a, 1); | ||
assert.strictEqual(b, 2); | ||
|
||
// Verify store 1 state matches input | ||
assert.strictEqual(store1.getStore(), inputs[n]); | ||
|
||
// Verify store 2 state has expected transformation | ||
assert.deepStrictEqual(store2.getStore(), { data: inputs[n] }); | ||
|
||
// Should support nested contexts | ||
n++; | ||
channel.runStores(inputs[n], common.mustCall(function() { | ||
// Verify this and argument forwarding | ||
assert.strictEqual(this, undefined); | ||
|
||
// Verify store 1 state matches input | ||
assert.strictEqual(store1.getStore(), inputs[n]); | ||
|
||
// Verify store 2 state has expected transformation | ||
assert.deepStrictEqual(store2.getStore(), { data: inputs[n] }); | ||
})); | ||
n--; | ||
|
||
// Verify store 1 state matches input | ||
assert.strictEqual(store1.getStore(), inputs[n]); | ||
|
||
// Verify store 2 state has expected transformation | ||
assert.deepStrictEqual(store2.getStore(), { data: inputs[n] }); | ||
}), thisArg, 1, 2); | ||
|
||
// Verify stores are empty after run | ||
assert.strictEqual(store1.getStore(), undefined); | ||
assert.strictEqual(store2.getStore(), undefined); | ||
|
||
// Verify unbinding works | ||
assert.ok(channel.unbindStore(store1)); | ||
store1bound = false; | ||
|
||
// Verify unbinding a store that is not bound returns false | ||
assert.ok(!channel.unbindStore(store1)); | ||
|
||
n++; | ||
channel.runStores(inputs[n], common.mustCall(() => { | ||
// Verify after unbinding store 1 will remain undefined | ||
assert.strictEqual(store1.getStore(), undefined); | ||
|
||
// Verify still bound store 2 receives expected data | ||
assert.deepStrictEqual(store2.getStore(), { data: inputs[n] }); | ||
})); | ||
|
||
// Contain transformer errors and emit on next tick | ||
const fail = new Error('fail'); | ||
channel.bindStore(store1, () => { | ||
throw fail; | ||
}); | ||
|
||
let calledRunStores = false; | ||
process.once('uncaughtException', common.mustCall((err) => { | ||
assert.strictEqual(calledRunStores, true); | ||
assert.strictEqual(err, fail); | ||
})); | ||
|
||
channel.runStores(inputs[n], common.mustCall()); | ||
calledRunStores = true; |
46 changes: 46 additions & 0 deletions
46
test/parallel/test-diagnostics-channel-tracing-channel-async-error.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const dc = require('diagnostics_channel'); | ||
const assert = require('assert'); | ||
|
||
const channel = dc.tracingChannel('test'); | ||
|
||
const expectedError = new Error('test'); | ||
const input = { foo: 'bar' }; | ||
const thisArg = { baz: 'buz' }; | ||
|
||
function check(found) { | ||
assert.deepStrictEqual(found, input); | ||
} | ||
|
||
const handlers = { | ||
start: common.mustCall(check, 2), | ||
end: common.mustCall(check, 2), | ||
asyncStart: common.mustCall(check, 2), | ||
asyncEnd: common.mustCall(check, 2), | ||
error: common.mustCall((found) => { | ||
check(found); | ||
assert.deepStrictEqual(found.error, expectedError); | ||
}, 2) | ||
}; | ||
|
||
channel.subscribe(handlers); | ||
|
||
channel.traceCallback(function(cb, err) { | ||
assert.deepStrictEqual(this, thisArg); | ||
setImmediate(cb, err); | ||
}, 0, input, thisArg, common.mustCall((err, res) => { | ||
assert.strictEqual(err, expectedError); | ||
assert.strictEqual(res, undefined); | ||
}), expectedError); | ||
|
||
channel.tracePromise(function(value) { | ||
assert.deepStrictEqual(this, thisArg); | ||
return Promise.reject(value); | ||
}, input, thisArg, expectedError).then( | ||
common.mustNotCall(), | ||
common.mustCall((value) => { | ||
assert.deepStrictEqual(value, expectedError); | ||
}) | ||
); |
60 changes: 60 additions & 0 deletions
60
test/parallel/test-diagnostics-channel-tracing-channel-async.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const dc = require('diagnostics_channel'); | ||
const assert = require('assert'); | ||
|
||
const channel = dc.tracingChannel('test'); | ||
|
||
const expectedResult = { foo: 'bar' }; | ||
const input = { foo: 'bar' }; | ||
const thisArg = { baz: 'buz' }; | ||
|
||
function check(found) { | ||
assert.deepStrictEqual(found, input); | ||
} | ||
|
||
const handlers = { | ||
start: common.mustCall(check, 2), | ||
end: common.mustCall(check, 2), | ||
asyncStart: common.mustCall((found) => { | ||
check(found); | ||
assert.strictEqual(found.error, undefined); | ||
assert.deepStrictEqual(found.result, expectedResult); | ||
}, 2), | ||
asyncEnd: common.mustCall((found) => { | ||
check(found); | ||
assert.strictEqual(found.error, undefined); | ||
assert.deepStrictEqual(found.result, expectedResult); | ||
}, 2), | ||
error: common.mustNotCall() | ||
}; | ||
|
||
channel.subscribe(handlers); | ||
|
||
channel.traceCallback(function(cb, err, res) { | ||
assert.deepStrictEqual(this, thisArg); | ||
setImmediate(cb, err, res); | ||
}, 0, input, thisArg, common.mustCall((err, res) => { | ||
assert.strictEqual(err, null); | ||
assert.deepStrictEqual(res, expectedResult); | ||
}), null, expectedResult); | ||
|
||
channel.tracePromise(function(value) { | ||
assert.deepStrictEqual(this, thisArg); | ||
return Promise.resolve(value); | ||
}, input, thisArg, expectedResult).then( | ||
common.mustCall((value) => { | ||
assert.deepStrictEqual(value, expectedResult); | ||
}), | ||
common.mustNotCall() | ||
); | ||
|
||
let failed = false; | ||
try { | ||
channel.traceCallback(common.mustNotCall(), 0, input, thisArg, 1, 2, 3); | ||
} catch (err) { | ||
assert.ok(/"callback" argument must be of type function/.test(err.message)); | ||
failed = true; | ||
} | ||
assert.strictEqual(failed, true); |
29 changes: 29 additions & 0 deletions
29
test/parallel/test-diagnostics-channel-tracing-channel-callback-run-stores.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const { AsyncLocalStorage } = require('async_hooks'); | ||
const dc = require('diagnostics_channel'); | ||
const assert = require('assert'); | ||
|
||
const channel = dc.tracingChannel('test'); | ||
const store = new AsyncLocalStorage(); | ||
|
||
const firstContext = { foo: 'bar' }; | ||
const secondContext = { baz: 'buz' }; | ||
|
||
channel.start.bindStore(store, common.mustCall(() => { | ||
return firstContext; | ||
})); | ||
|
||
channel.asyncStart.bindStore(store, common.mustCall(() => { | ||
return secondContext; | ||
})); | ||
|
||
assert.strictEqual(store.getStore(), undefined); | ||
channel.traceCallback(common.mustCall((cb) => { | ||
assert.deepStrictEqual(store.getStore(), firstContext); | ||
setImmediate(cb); | ||
}), 0, {}, null, common.mustCall(() => { | ||
assert.deepStrictEqual(store.getStore(), secondContext); | ||
})); | ||
assert.strictEqual(store.getStore(), undefined); |
24 changes: 24 additions & 0 deletions
24
test/parallel/test-diagnostics-channel-tracing-channel-promise-run-stores.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const { setTimeout } = require('node:timers/promises'); | ||
const { AsyncLocalStorage } = require('async_hooks'); | ||
const dc = require('diagnostics_channel'); | ||
const assert = require('assert'); | ||
|
||
const channel = dc.tracingChannel('test'); | ||
const store = new AsyncLocalStorage(); | ||
|
||
const context = { foo: 'bar' }; | ||
|
||
channel.start.bindStore(store, common.mustCall(() => { | ||
return context; | ||
})); | ||
|
||
assert.strictEqual(store.getStore(), undefined); | ||
channel.tracePromise(common.mustCall(async () => { | ||
assert.deepStrictEqual(store.getStore(), context); | ||
await setTimeout(1); | ||
assert.deepStrictEqual(store.getStore(), context); | ||
})); | ||
assert.strictEqual(store.getStore(), undefined); |
21 changes: 21 additions & 0 deletions
21
test/parallel/test-diagnostics-channel-tracing-channel-run-stores.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const { AsyncLocalStorage } = require('async_hooks'); | ||
const dc = require('diagnostics_channel'); | ||
const assert = require('assert'); | ||
|
||
const channel = dc.tracingChannel('test'); | ||
const store = new AsyncLocalStorage(); | ||
|
||
const context = { foo: 'bar' }; | ||
|
||
channel.start.bindStore(store, common.mustCall(() => { | ||
return context; | ||
})); | ||
|
||
assert.strictEqual(store.getStore(), undefined); | ||
channel.traceSync(common.mustCall(() => { | ||
assert.deepStrictEqual(store.getStore(), context); | ||
})); | ||
assert.strictEqual(store.getStore(), undefined); |
39 changes: 39 additions & 0 deletions
39
test/parallel/test-diagnostics-channel-tracing-channel-sync-error.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const dc = require('diagnostics_channel'); | ||
const assert = require('assert'); | ||
|
||
const channel = dc.tracingChannel('test'); | ||
|
||
const expectedError = new Error('test'); | ||
const input = { foo: 'bar' }; | ||
const thisArg = { baz: 'buz' }; | ||
|
||
function check(found) { | ||
assert.deepStrictEqual(found, input); | ||
} | ||
|
||
const handlers = { | ||
start: common.mustCall(check), | ||
end: common.mustCall(check), | ||
asyncStart: common.mustNotCall(), | ||
asyncEnd: common.mustNotCall(), | ||
error: common.mustCall((found) => { | ||
check(found); | ||
assert.deepStrictEqual(found.error, expectedError); | ||
}) | ||
}; | ||
|
||
channel.subscribe(handlers); | ||
try { | ||
channel.traceSync(function(err) { | ||
assert.deepStrictEqual(this, thisArg); | ||
assert.strictEqual(err, expectedError); | ||
throw err; | ||
}, input, thisArg, expectedError); | ||
|
||
throw new Error('It should not reach this error'); | ||
} catch (error) { | ||
assert.deepStrictEqual(error, expectedError); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we add something similar as in the callback case here:
But asserts stay as is to actually verify the runStores is not used in promise case?
Not meant as blocking comment, perfectly fine to do this in a followup or skip at all.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I'll do that as a follow up. Don't want to tempt fate with CI the day before v20 cut-off. 😂