tracingChannel.traceCallback(fn[, position[, context[, thisArg[, ...args]]]])
fn<Function> 回调,使用函数来封装追踪position<number> 期望回调的零基参数位置(如果传入undefined,默认为最后一个参数)context<Object> 用于关联跟踪事件的共享对象(如果传入undefined,默认为{})thisArg<any> 用于函数调用的接收对象...args<any> 要传递给函数的参数(必须包括回调函数)- 返回值:<any> 给定函数的返回值
跟踪接收回调的函数调用。回调通常遵循将错误作为第一个参数的惯例。这将始终在函数执行的同步部分产生 start 事件 和 end 事件,并在回调执行时产生 asyncStart 事件 和 asyncEnd 事件。如果给定的函数抛出异常或传递给回调的第一个参数被设置,也可能产生 error 事件。这将使用 start 通道上的 channel.runStores(context, ...) 运行给定函数,从而确保所有事件的绑定存储都设置为匹配此跟踪上下文。
【Trace a callback-receiving function call. The callback is expected to follow
the error as first arg convention typically used. This will always produce a
start event and end event around the synchronous portion of the
function execution, and will produce a asyncStart event and
asyncEnd event around the callback execution. It may also produce an
error event if the given function throws or the first argument passed to
the callback is set. 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.traceCallback((arg1, callback) => {
// Do something
callback(null, 'result');
}, 1, {
some: 'thing',
}, thisArg, arg1, callback);const diagnostics_channel = require('node:diagnostics_channel');
const channels = diagnostics_channel.tracingChannel('my-channel');
channels.traceCallback((arg1, callback) => {
// Do something
callback(null, 'result');
}, 1, {
some: 'thing',
}, thisArg, arg1, callback);回调也将使用 channel.runStores(context, ...) 运行,这在某些情况下可以启用上下文丢失恢复。
【The callback will also be run with channel.runStores(context, ...) which
enables context loss recovery in some cases.】
import diagnostics_channel from 'node:diagnostics_channel';
import { AsyncLocalStorage } from 'node:async_hooks';
const channels = diagnostics_channel.tracingChannel('my-channel');
const myStore = new AsyncLocalStorage();
// The start channel sets the initial store data to something
// and stores that store data value on the trace context object
channels.start.bindStore(myStore, (data) => {
const span = new Span(data);
data.span = span;
return span;
});
// Then asyncStart can restore from that data it stored previously
channels.asyncStart.bindStore(myStore, (data) => {
return data.span;
});const diagnostics_channel = require('node:diagnostics_channel');
const { AsyncLocalStorage } = require('node:async_hooks');
const channels = diagnostics_channel.tracingChannel('my-channel');
const myStore = new AsyncLocalStorage();
// The start channel sets the initial store data to something
// and stores that store data value on the trace context object
channels.start.bindStore(myStore, (data) => {
const span = new Span(data);
data.span = span;
return span;
});
// Then asyncStart can restore from that data it stored previously
channels.asyncStart.bindStore(myStore, (data) => {
return data.span;
});