tracingChannel.traceCallback(fn[, position[, context[, thisArg[, ...args]]]])


  • fn <Function> 使用函数来封装跟踪的回调

    ¥fn <Function> callback using function to wrap a trace around

  • position <number> 预期回调的零索引参数位置(如果传递 undefined,则默认为最后一个参数)

    ¥position <number> Zero-indexed argument position of expected callback (defaults to last argument if undefined is passed)

  • context <Object> 用于关联跟踪事件的共享对象(如果传递 undefined,则默认为 {}

    ¥context <Object> Shared object to correlate trace events through (defaults to {} if undefined is passed)

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

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

  • ...args <any> 要传递给函数的参数(必须包括回调)

    ¥...args <any> arguments to pass to the function (must include the callback)

  • 返回:<any> 给定函数的返回值

    ¥Returns: <any> The return value of the given function

跟踪回调接收函数调用。回调预计会遵循通常使用的第一个参数约定的错误。这将始终围绕函数执行的同步部分生成 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;
});