tracingChannel.traceSync(fn[, context[, thisArg[, ...args]]])
fn<Function> 用于封装跟踪的函数context<Object> 共享对象,用于关联事件thisArg<any> 用于函数调用的接收对象...args<any> 可选参数,传递给函数- 返回值:<any> 给定函数的返回值
跟踪一个同步函数调用。这将始终在执行期间产生 start 事件 和 end 事件,如果给定函数抛出错误,可能会产生 error 事件。它将使用 start 通道上的 channel.runStores(context, ...) 运行给定函数,这确保所有事件的绑定存储都应设置为与此跟踪上下文匹配。
【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.】
import diagnostics_channel from 'node:diagnostics_channel';
const channels = diagnostics_channel.tracingChannel('my-channel');
channels.traceSync(() => {
// Do something
}, {
some: 'thing',
});const diagnostics_channel = require('node:diagnostics_channel');
const channels = diagnostics_channel.tracingChannel('my-channel');
channels.traceSync(() => {
// Do something
}, {
some: 'thing',
});