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


稳定性: 1 - 实验性

  • fn <Function> 回调,使用函数来封装追踪
  • position <number> 期望回调的从零开始索引的位置
  • context <Object> 共享对象,用于关联跟踪事件
  • thisArg <any> 用于函数调用的接收对象
  • ...args <any> 可选参数,传递给函数
  • 返回值:<any> 给定函数的返回值

跟踪一个接收回调的函数调用。这将始终在函数执行的同步部分产生 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;
});