tracingChannel.tracePromise(fn[, context[, thisArg[, ...args]]])
fn<Function> 返回 Promise 的函数,用于在其周围封装跟踪context<Object> 共享对象,用于关联跟踪事件thisArg<any> 用于函数调用的接收对象...args<any> 可选参数,传递给函数- 返回:<Promise> 链式来自给定函数返回的 Promise
跟踪返回 Promise 的函数调用。这将在函数执行的同步部分始终产生 start 事件 和 end 事件,并且在到达 Promise 延续时会产生 asyncStart 事件 和 asyncEnd 事件。如果给定的函数抛出错误或返回的 Promise 被拒绝,它也可能产生 error 事件。这将使用 start 通道上的 channel.runStores(context, ...) 运行给定函数,这确保所有事件的任何绑定存储都设置为匹配此跟踪上下文。
【Trace a promise-returning function call. This will always produce a
start event and end event around the synchronous portion of the
function execution, and will produce an asyncStart event and
asyncEnd event when a promise continuation is reached. It may also
produce an error event if the given function throws an error or the
returned promise rejects. 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.tracePromise(async () => {
// Do something
}, {
some: 'thing',
});const diagnostics_channel = require('node:diagnostics_channel');
const channels = diagnostics_channel.tracingChannel('my-channel');
channels.tracePromise(async () => {
// Do something
}, {
some: 'thing',
});