-
-
Notifications
You must be signed in to change notification settings - Fork 32k
diagnostics_channel: early-exit tracing channel trace methods #51915
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ const { | |
ArrayPrototypePush, | ||
ArrayPrototypeSplice, | ||
SafeFinalizationRegistry, | ||
ObjectDefineProperty, | ||
ObjectGetPrototypeOf, | ||
ObjectSetPrototypeOf, | ||
Promise, | ||
|
@@ -250,35 +251,40 @@ function assertChannel(value, name) { | |
} | ||
} | ||
|
||
function tracingChannelFrom(nameOrChannels, name) { | ||
if (typeof nameOrChannels === 'string') { | ||
return channel(`tracing:${nameOrChannels}:${name}`); | ||
} | ||
|
||
if (typeof nameOrChannels === 'object' && nameOrChannels !== null) { | ||
const channel = nameOrChannels[name]; | ||
assertChannel(channel, `nameOrChannels.${name}`); | ||
return channel; | ||
} | ||
|
||
throw new ERR_INVALID_ARG_TYPE('nameOrChannels', | ||
['string', 'object', 'TracingChannel'], | ||
nameOrChannels); | ||
} | ||
|
||
class TracingChannel { | ||
constructor(nameOrChannels) { | ||
if (typeof nameOrChannels === 'string') { | ||
this.start = channel(`tracing:${nameOrChannels}:start`); | ||
this.end = channel(`tracing:${nameOrChannels}:end`); | ||
this.asyncStart = channel(`tracing:${nameOrChannels}:asyncStart`); | ||
this.asyncEnd = channel(`tracing:${nameOrChannels}:asyncEnd`); | ||
this.error = channel(`tracing:${nameOrChannels}:error`); | ||
} else if (typeof nameOrChannels === 'object') { | ||
const { start, end, asyncStart, asyncEnd, error } = nameOrChannels; | ||
|
||
assertChannel(start, 'nameOrChannels.start'); | ||
assertChannel(end, 'nameOrChannels.end'); | ||
assertChannel(asyncStart, 'nameOrChannels.asyncStart'); | ||
assertChannel(asyncEnd, 'nameOrChannels.asyncEnd'); | ||
assertChannel(error, 'nameOrChannels.error'); | ||
|
||
this.start = start; | ||
this.end = end; | ||
this.asyncStart = asyncStart; | ||
this.asyncEnd = asyncEnd; | ||
this.error = error; | ||
} else { | ||
throw new ERR_INVALID_ARG_TYPE('nameOrChannels', | ||
['string', 'object', 'Channel'], | ||
nameOrChannels); | ||
for (const eventName of traceEvents) { | ||
ObjectDefineProperty(this, eventName, { | ||
__proto__: null, | ||
value: tracingChannelFrom(nameOrChannels, eventName), | ||
}); | ||
} | ||
} | ||
|
||
get hasSubscribers() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like there are no tests for this method. Though it does get code coverage since it's used by other methods. Was that intentional? Seems like an accident since it's a public method. Also it looks like there's no documentation? |
||
return this.start.hasSubscribers || | ||
this.end.hasSubscribers || | ||
this.asyncStart.hasSubscribers || | ||
this.asyncEnd.hasSubscribers || | ||
this.error.hasSubscribers; | ||
} | ||
|
||
subscribe(handlers) { | ||
for (const name of traceEvents) { | ||
if (!handlers[name]) continue; | ||
|
@@ -302,6 +308,10 @@ class TracingChannel { | |
} | ||
|
||
traceSync(fn, context = {}, thisArg, ...args) { | ||
if (!this.hasSubscribers) { | ||
return ReflectApply(fn, thisArg, args); | ||
} | ||
|
||
const { start, end, error } = this; | ||
|
||
return start.runStores(context, () => { | ||
|
@@ -320,6 +330,10 @@ class TracingChannel { | |
} | ||
|
||
tracePromise(fn, context = {}, thisArg, ...args) { | ||
if (!this.hasSubscribers) { | ||
return ReflectApply(fn, thisArg, args); | ||
} | ||
|
||
const { start, end, asyncStart, asyncEnd, error } = this; | ||
|
||
function reject(err) { | ||
|
@@ -358,6 +372,10 @@ class TracingChannel { | |
} | ||
|
||
traceCallback(fn, position = -1, context = {}, thisArg, ...args) { | ||
if (!this.hasSubscribers) { | ||
return ReflectApply(fn, thisArg, args); | ||
} | ||
|
||
const { start, end, asyncStart, asyncEnd, error } = this; | ||
|
||
function wrappedCallback(err, res) { | ||
|
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const dc = require('diagnostics_channel'); | ||
|
||
const channel = dc.tracingChannel('test'); | ||
|
||
const handlers = { | ||
start: common.mustNotCall(), | ||
end: common.mustNotCall(), | ||
asyncStart: common.mustNotCall(), | ||
asyncEnd: common.mustNotCall(), | ||
error: common.mustNotCall() | ||
}; | ||
|
||
// While subscribe occurs _before_ the callback executes, | ||
// no async events should be published. | ||
channel.traceCallback(setImmediate, 0, {}, null, common.mustCall()); | ||
channel.subscribe(handlers); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
'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); | ||
} | ||
|
||
function checkAsync(found) { | ||
check(found); | ||
assert.strictEqual(found.error, undefined); | ||
assert.deepStrictEqual(found.result, expectedResult); | ||
} | ||
|
||
const handlers = { | ||
start: common.mustCall(check), | ||
end: common.mustCall(check), | ||
asyncStart: common.mustCall(checkAsync), | ||
asyncEnd: common.mustCall(checkAsync), | ||
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); | ||
|
||
assert.throws(() => { | ||
channel.traceCallback(common.mustNotCall(), 0, input, thisArg, 1, 2, 3); | ||
}, /"callback" argument must be of type function/); |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,21 @@ | ||||||
'use strict'; | ||||||
|
||||||
const common = require('../common'); | ||||||
const dc = require('diagnostics_channel'); | ||||||
|
||||||
const channel = dc.tracingChannel('test'); | ||||||
|
||||||
const handlers = { | ||||||
start: common.mustNotCall(), | ||||||
end: common.mustNotCall(), | ||||||
asyncStart: common.mustNotCall(), | ||||||
asyncEnd: common.mustNotCall(), | ||||||
error: common.mustNotCall() | ||||||
}; | ||||||
|
||||||
// While subscribe occurs _before_ the promise resolves, | ||||||
// no async events should be published. | ||||||
channel.tracePromise(() => { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
You could even add asserts that this/arguments/return values are correctly forwarded. |
||||||
return new Promise(setImmediate); | ||||||
}, {}); | ||||||
channel.subscribe(handlers); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
'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.mustCall(check), | ||
asyncEnd: common.mustCall(check), | ||
error: common.mustCall((found) => { | ||
check(found); | ||
assert.deepStrictEqual(found.error, expectedError); | ||
}) | ||
}; | ||
|
||
channel.subscribe(handlers); | ||
|
||
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); | ||
}) | ||
); |
Uh oh!
There was an error while loading. Please reload this page.