tracingChannel.tracePromise(fn[, context[, thisArg[, ...args]]])


稳定性: 1 - 实验性的

¥Stability: 1 - Experimental

  • fn <Function> promise 返回函数来环绕跟踪

    ¥fn <Function> Promise-returning function to wrap a trace around

  • context <Object> 用于关联跟踪事件的共享对象

    ¥context <Object> Shared object to correlate trace events through

  • thisArg <any> 用于函数调用的接收器

    ¥thisArg <any> The receiver to be used for the function call

  • ...args <any> 传递给函数的可选参数

    ¥...args <any> Optional arguments to pass to the function

  • 返回:<Promise> 从给定函数返回的 promise 链接

    ¥Returns: <Promise> Chained from promise returned by the given function

跟踪 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',
});