tracingChannel.traceCallback(fn[, position[, context[, thisArg[, ...args]]]])
¥Stability: 1 - Experimental
-
fn
<Function> 使用函数来封装跟踪的回调¥
fn
<Function> callback using function to wrap a trace around -
position
<number> 预期回调的零索引参数位置¥
position
<number> Zero-indexed argument position of expected callback -
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 -
返回:<any> 给定函数的返回值
¥Returns: <any> The return value of the given function
跟踪回调接收函数调用。这将始终围绕函数执行的同步部分生成 start
事件 和 end
事件,并将围绕回调执行生成 asyncStart
事件 和 asyncEnd
事件。如果给定的函数抛出错误或返回的 promise 被拒绝,它也可能产生一个 error
事件。这将在 start
通道上使用 channel.runStores(context, ...)
运行给定函数,确保所有事件都应设置任何绑定存储以匹配此跟踪上下文。
¥Trace a callback-receiving function call. 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 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.
默认情况下,position
将为 -1 以指示应将最终参数用作回调。
¥The position
will be -1 by default to indicate the final argument should
be used as the callback.
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');
}, {
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;
});