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


  • fn <Function> 用于封装跟踪的函数
  • context <Object> 用于关联跟踪事件的共享对象
  • thisArg <any> 用于函数调用的接收器
  • ...args <any> 传递给函数的可选参数
  • 返回:<any> 给定函数的返回值,或者如果跟踪通道有活跃的订阅者,则对返回值调用 .then(...) 的结果。如果返回值不是 Promise 或可 then 的对象,则将其原样返回并发出警告。

跟踪一个返回 <Promise>可调用 then 的对象 的异步函数调用。这将始终在函数执行的同步部分产生 start 事件end 事件,并且在返回的 promise 被解决或拒绝时产生 asyncStart 事件asyncEnd 事件。如果给定的函数抛出错误或返回的 promise 被拒绝,它还可能产生 error 事件。这将使用 start 通道通过 channel.runStores(context, ...) 运行给定的函数,这确保所有事件的绑定存储都设置为与此追踪上下文匹配。

🌐 Trace an asynchronous function call which returns a <Promise> or thenable object. 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 the returned promise is resolved or rejected. It may also produce an error event if the given function throws an error or the returned promise is rejected. 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.

如果 fn 返回的值不是 Promise 或可 thenable 对象,那么它将被返回并伴随警告,并且不会产生 asyncStartasyncEnd 事件。

🌐 If the value returned by fn is not a Promise or thenable, then it will be returned with a warning, and no asyncStart or asyncEnd events will be produced.

为了确保只生成正确的跟踪图,事件只有在跟踪开始前存在订阅者时才会被发布。在跟踪开始后添加的订阅将不会接收到该跟踪的未来事件,只能看到未来的跟踪。

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