On this page

diagnostics_channel 诊断通道

History
Source Code: lib/diagnostics_channel.js

稳定性:2 - 稳定

node:diagnostics_channel 模块提供了一个 API,用于创建命名通道, 以报告用于诊断目的的任意消息数据。

可通过以下方式访问:

模块编写者如果想要报告诊断消息, 应当创建一个或多个顶层通道用于报告消息。 通道也可以在运行时获取,但由于这样做会带来额外的开销, 因此不鼓励这样做。为方便起见,可以导出通道, 但只要知道名称,就可以在任何地方获取它。

如果您打算让模块生成诊断数据供他人 使用,建议您在文档中包含所用命名通道 以及消息数据结构的相关说明。通道名称 通常应包含模块名称,以避免与来自其他 模块的数据发生冲突。

以下是公共 API 的简单概览。

import diagnostics_channel from 'node:diagnostics_channel';

// 获取可复用的通道对象
const channel = diagnostics_channel.channel('my-channel');

function onMessage(message, name) {
  // 接收到的数据
}

// 订阅该通道
diagnostics_channel.subscribe('my-channel', onMessage);

// 检查通道是否有活动的订阅者
if (channel.hasSubscribers) {
  // 向通道发布数据
  channel.publish({
    some: 'data',
  });
}

// 取消订阅该通道
diagnostics_channel.unsubscribe('my-channel', onMessage);
M

diagnostics_channel.hasSubscribers

History
diagnostics_channel.hasSubscribers(name): void
Attributes
通道名称

检查命名通道是否存在活动的订阅者。如果要发送的消息 准备起来开销较大,这会很有帮助。

此 API 是可选的,但在从对性能非常敏感的代码中 发布消息时很有帮助。

import diagnostics_channel from 'node:diagnostics_channel';

if (diagnostics_channel.hasSubscribers('my-channel')) {
  // 存在订阅者,准备并发布消息
}
M

diagnostics_channel

History
diagnostics_channel(name): void
Attributes
通道名称

这是希望向命名通道发布消息时的主要入口点。 它产生的通道对象经过优化,可尽可能降低发布时的 开销。

import diagnostics_channel from 'node:diagnostics_channel';

const channel = diagnostics_channel.channel('my-channel');
M

diagnostics_channel.subscribe

History
diagnostics_channel.subscribe(name, onMessage): void
Attributes
通道名称
onMessage:<Function>
用于接收通道消息的处理程序
message:<any>
消息数据
通道的名称

注册一个消息处理程序以订阅该通道。每当有消息发布到该通道时, 该消息处理程序都会同步运行。消息处理程序中抛出的任何 错误都将触发 'uncaughtException'。

import diagnostics_channel from 'node:diagnostics_channel';

diagnostics_channel.subscribe('my-channel', (message, name) => {
  // 接收到的数据
});
M

diagnostics_channel.unsubscribe

History
diagnostics_channel.unsubscribe(name, onMessage): void
Attributes
通道名称
onMessage:<Function>
要移除的先前订阅的处理程序

移除先前使用 diagnostics_channel.subscribe(name, onMessage) 注册到该通道的消息处理程序。

import diagnostics_channel from 'node:diagnostics_channel';

function onMessage(message, name) {
  // 接收到的数据
}

diagnostics_channel.subscribe('my-channel', onMessage);

diagnostics_channel.unsubscribe('my-channel', onMessage);
M

diagnostics_channel.tracingChannel

History
diagnostics_channel.tracingChannel(nameOrChannels): void

稳定性:2 - 稳定

Attributes
nameOrChannels:<string> | <TracingChannel>
通道名称或 包含所有  TracingChannel Channels 的对象

为给定的 TracingChannel Channels 创建一个 TracingChannel 包装器。如果给定的是名称,则相应的追踪 通道将以 tracing:${name}:${eventType} 的形式创建,其中 eventType 对应于 TracingChannel Channels 的类型。

import diagnostics_channel from 'node:diagnostics_channel';

const channelsByName = diagnostics_channel.tracingChannel('my-channel');

// 或者...

const channelsByCollection = diagnostics_channel.tracingChannel({
  start: diagnostics_channel.channel('tracing:my-channel:start'),
  end: diagnostics_channel.channel('tracing:my-channel:end'),
  asyncStart: diagnostics_channel.channel('tracing:my-channel:asyncStart'),
  asyncEnd: diagnostics_channel.channel('tracing:my-channel:asyncEnd'),
  error: diagnostics_channel.channel('tracing:my-channel:error'),
});
M

diagnostics_channel.boundedChannel

History
diagnostics_channel.boundedChannel(nameOrChannels): void

稳定性:1 - 实验性

Attributes
nameOrChannels:<string> | <BoundedChannel>
通道名称或 包含所有  BoundedChannel Channels 的对象

为给定的通道创建一个 BoundedChannel 包装器。如果给定的是名称, 则相应的通道将以 tracing:${name}:${eventType} 的形式创建,其中 eventType 为 start 或 end。

BoundedChannel 是 TracingChannel 的简化版本,仅 追踪同步操作。它只有 start 和 end 事件,没有 asyncStart、asyncEnd 或 error 事件,因此适合追踪 不涉及异步延续或错误处理的操作。

import { boundedChannel, channel } from 'node:diagnostics_channel';

const wc = boundedChannel('my-operation');

// 或者...

const wc2 = boundedChannel({
  start: channel('tracing:my-operation:start'),
  end: channel('tracing:my-operation:end'),
});

类:Channel

History

Channel 类表示数据管道中的单个命名通道。 它用于跟踪订阅者,并在有订阅者时发布消息。 它作为独立对象存在,以避免在发布时查找通道, 从而实现极快的发布速度,并允许大量使用, 而开销极小。通道通过 diagnostics_channel.channel(name) 创建,不支持通过 new Channel(name) 直接 构造通道。

P

channel.hasSubscribers

History
  • 返回:<boolean> 是否存在活动的订阅者

检查该通道是否存在活动的订阅者。当要发送的消息 准备起来开销较大时,这很有帮助。

此 API 是可选的,但在从对性能非常敏感的代码中 发布消息时很有帮助。

import diagnostics_channel from 'node:diagnostics_channel';

const channel = diagnostics_channel.channel('my-channel');

if (channel.hasSubscribers) {
  // 存在订阅者,准备并发布消息
}
M

channel.publish

History
channel.publish(message): void
Attributes
message:<any>
要发送给通道订阅者的消息

向该通道的所有订阅者发布消息。这将同步触发 消息处理程序,使其在同一上下文中执行。

import diagnostics_channel from 'node:diagnostics_channel';

const channel = diagnostics_channel.channel('my-channel');

channel.publish({
  some: 'message',
});
channel.subscribe(onMessage): void
Attributes
onMessage:<Function>
用于接收通道消息的处理程序
message:<any>
消息数据
通道的名称

注册一个消息处理程序以订阅该通道。每当有消息发布到该通道时, 该消息处理程序都会同步运行。消息处理程序中抛出的任何 错误都将触发 'uncaughtException'。

import diagnostics_channel from 'node:diagnostics_channel';

const channel = diagnostics_channel.channel('my-channel');

channel.subscribe((message, name) => {
  // 接收到的数据
});
channel.unsubscribe(onMessage): void
Attributes
onMessage:<Function>
要移除的先前订阅的处理程序

移除先前使用 channel.subscribe(onMessage) 注册到 该通道的消息处理程序。

import diagnostics_channel from 'node:diagnostics_channel';

const channel = diagnostics_channel.channel('my-channel');

function onMessage(message, name) {
  // 接收到的数据
}

channel.subscribe(onMessage);

channel.unsubscribe(onMessage);
M

channel.bindStore

History
channel.bindStore(store, transform?): void

稳定性:1 - 实验性

Attributes
要将上下文数据绑定到的存储
transform:<Function>
在设置存储上下文之前转换上下文数据

当调用 channel.runStores(context, ...) 时,给定的上下文数据 将应用到绑定到该通道的任何存储。如果存储已经被绑定, 则先前的 transform 函数将被新函数替换。 可以省略 transform 函数,直接将给定的上下文数据 设置为上下文。

import diagnostics_channel from 'node:diagnostics_channel';
import { AsyncLocalStorage } from 'node:async_hooks';

const store = new AsyncLocalStorage();

const channel = diagnostics_channel.channel('my-channel');

channel.bindStore(store, (data) => {
  return { data };
});
M

channel.unbindStore

History
channel.unbindStore(store): void

稳定性:1 - 实验性

Attributes
要从通道解绑的存储。

移除先前使用 channel.bindStore(store) 注册到 该通道的消息处理程序。

import diagnostics_channel from 'node:diagnostics_channel';
import { AsyncLocalStorage } from 'node:async_hooks';

const store = new AsyncLocalStorage();

const channel = diagnostics_channel.channel('my-channel');

channel.bindStore(store);
channel.unbindStore(store);
M

channel.runStores

History
channel.runStores(context, fn, thisArg?, ...args?): void

稳定性:1 - 实验性

Attributes
context:<any>
要发送给订阅者并绑定到存储的消息
要在进入的存储上下文中运行的处理程序
thisArg:<any>
函数调用时使用的接收者。
...args:<any>
要传递给函数的可选参数。

在给定函数执行期间,将给定的数据应用到绑定到该通道的 任何 AsyncLocalStorage 实例,然后在数据应用到存储的 作用域内向通道发布消息。

如果向 channel.bindStore(store) 提供了转换函数,则会在消息数据 成为存储上下文值之前应用该函数来转换数据。 在需要上下文链接的情况下,可以从转换函数 内部访问先前的存储上下文。

应用到存储的上下文应可在从给定函数执行期间开始的 任何异步代码中访问,但在某些情况下可能 会发生 上下文丢失。

import diagnostics_channel from 'node:diagnostics_channel';
import { AsyncLocalStorage } from 'node:async_hooks';

const store = new AsyncLocalStorage();

const channel = diagnostics_channel.channel('my-channel');

channel.bindStore(store, (message) => {
  const parent = store.getStore();
  return new Span(message, parent);
});
channel.runStores({ some: 'message' }, () => {
  store.getStore(); // Span({ some: 'message' })
});
M

channel.withStoreScope

History
channel.withStoreScope(data): void

稳定性:1 - 实验性

Attributes
data:<any>
要绑定到存储的消息

创建一个可释放的作用域,将给定的数据绑定到该通道的 所有 AsyncLocalStorage 实例,并将其发布给订阅者。 该作用域在释放时会自动恢复先前的存储上下文。

此方法支持使用 JavaScript 的显式资源管理 (结合 Symbol.dispose 的 using 语法)来管理存储上下文, 而无需闭包包装。

import { channel } from 'node:diagnostics_channel';
import { AsyncLocalStorage } from 'node:async_hooks';

const store = new AsyncLocalStorage();
const ch = channel('my-channel');

ch.bindStore(store, (message) => {
  return { ...message, timestamp: Date.now() };
});

{
  using scope = ch.withStoreScope({ request: 'data' });
  // 进入存储,数据已发布
  console.log(store.getStore()); // { request: 'data', timestamp: ... }
}
// 作用域退出时自动恢复存储

类:RunStoresScope

History

稳定性:1 - 实验性

RunStoresScope 类表示由 channel.withStoreScope(data) 创建的可释放作用域。它管理存储上下文的生命周期, 并确保在作用域退出时正确恢复这些上下文。

必须使用 using 语法使用该作用域,以确保正确释放。

类:TracingChannel

History

稳定性:2 - 稳定

TracingChannel 类是 TracingChannel Channels 的集合, 共同表示一个可追踪的操作。它用于规范化和简化 为追踪应用流程而生成事件的过程。 diagnostics_channel.tracingChannel() 用于构造 TracingChannel。与 Channel 一样,建议在文件顶层 创建并复用单个 TracingChannel,而不是动态地 创建它们。

M

tracingChannel.subscribe

History
tracingChannel.subscribe(subscribers): void
Attributes
subscribers:<Object>
TracingChannel Channels 订阅者的集合
start event 的订阅者
end event 的订阅者
asyncStart:<Function>
asyncStart event 的订阅者
asyncEnd:<Function>
asyncEnd event 的订阅者
error event 的订阅者

用于将一组函数订阅到相应通道的辅助方法。 这与在每个通道上分别调用 channel.subscribe(onMessage) 相同。

import diagnostics_channel from 'node:diagnostics_channel';

const channels = diagnostics_channel.tracingChannel('my-channel');

channels.subscribe({
  start(message) {
    // 处理 start 消息
  },
  end(message) {
    // 处理 end 消息
  },
  asyncStart(message) {
    // 处理 asyncStart 消息
  },
  asyncEnd(message) {
    // 处理 asyncEnd 消息
  },
  error(message) {
    // 处理 error 消息
  },
});
M

tracingChannel.unsubscribe

History
tracingChannel.unsubscribe(subscribers): void
Attributes
subscribers:<Object>
TracingChannel Channels 订阅者的集合
start event 的订阅者
end event 的订阅者
asyncStart:<Function>
asyncStart event 的订阅者
asyncEnd:<Function>
asyncEnd event 的订阅者
error event 的订阅者

用于从相应通道取消订阅一组函数的辅助方法。 这与在每个通道上分别调用 channel.unsubscribe(onMessage) 相同。

import diagnostics_channel from 'node:diagnostics_channel';

const channels = diagnostics_channel.tracingChannel('my-channel');

channels.unsubscribe({
  start(message) {
    // 处理 start 消息
  },
  end(message) {
    // 处理 end 消息
  },
  asyncStart(message) {
    // 处理 asyncStart 消息
  },
  asyncEnd(message) {
    // 处理 asyncEnd 消息
  },
  error(message) {
    // 处理 error 消息
  },
});
M

tracingChannel.traceSync

History
tracingChannel.traceSync(fn, context?, thisArg?, ...args?): void
Attributes
要包裹追踪的函数
context:<Object>
用于关联事件的共享对象
thisArg:<any>
函数调用时使用的接收者
...args:<any>
要传递给函数的可选参数

追踪同步函数调用。这将始终在执行前后产生 start event 和 end event,如果给定函数抛出错误,还可能产生 error event。 这将在 start 通道上使用 channel.runStores(context, ...) 运行给定函数,从而确保所有 事件的绑定存储都与这一追踪上下文相匹配。

为确保只形成正确的追踪图,只有在开始追踪之前 已有订阅者时才会发布事件。追踪开始后添加的订阅 不会收到该追踪的后续事件, 只有之后的追踪才对他们可见。

import diagnostics_channel from 'node:diagnostics_channel';

const channels = diagnostics_channel.tracingChannel('my-channel');

channels.traceSync(() => {
  // 执行某些操作
}, {
  some: 'thing',
});
tracingChannel.tracePromise(fn, context?, thisArg?, ...args?): void
Attributes
要包裹追踪的函数
context:<Object>
用于关联追踪事件的共享对象
thisArg:<any>
函数调用时使用的接收者
...args:<any>
要传递给函数的可选参数

追踪返回 <Promise> 或 thenable object 的异步函数 调用。这将始终在函数执行的同步部分前后产生 start event 和 end event,并在返回的 promise 被兑现或拒绝时 产生 asyncStart event 和 asyncEnd event。 如果给定函数抛出错误或返回的 promise 被拒绝, 还可能产生 error event。这将在 start 通道上 使用 channel.runStores(context, ...) 运行给定函数, 从而确保所有事件的绑定存储都与这一追踪上下文 相匹配。

如果 fn 返回的值不是 Promise 或 thenable 对象, 则将附带警告返回该值,并且不会产生 asyncStart 或 asyncEnd 事件。

为确保只形成正确的追踪图,只有在开始追踪之前 已有订阅者时才会发布事件。追踪开始后添加的订阅 不会收到该追踪的后续事件, 只有之后的追踪才对他们可见。

import diagnostics_channel from 'node:diagnostics_channel';

const channels = diagnostics_channel.tracingChannel('my-channel');

channels.tracePromise(async () => {
  // 执行某些操作
}, {
  some: 'thing',
});
M

tracingChannel.traceCallback

History
tracingChannel.traceCallback(fn, position?, context?, thisArg?, ...args?): void
Attributes
要包裹追踪的接收回调的函数
position:<number>
预期回调的从零开始的参数位置 (如果传入  undefined ,则默认为最后一个参数)
context:<Object>
用于关联追踪事件的共享对象(如果传入  undefined ,则默认为 {} )
thisArg:<any>
函数调用时使用的接收者
...args:<any>
要传递给函数的参数(必须包含回调)

追踪接收回调的函数调用。回调应遵循通常使用的 “错误作为第一个参数”约定。这将在函数执行的同步部分 前后始终产生 start event 和 end event, 并在回调执行前后产生 asyncStart event 和 asyncEnd event。如果给定函数抛出异常,或传给 回调的第一个参数被设置,还可能产生 error event。 这将在 start 通道上使用 channel.runStores(context, ...) 运行给定函数,以确保 所有事件的绑定存储都与这一追踪上下文相匹配。

为确保只形成正确的追踪图,只有在开始追踪之前 已有订阅者时才会发布事件。追踪开始后添加的订阅 不会收到该追踪的后续事件, 只有之后的追踪才对他们可见。

import diagnostics_channel from 'node:diagnostics_channel';

const channels = diagnostics_channel.tracingChannel('my-channel');

channels.traceCallback((arg1, callback) => {
  // 执行某些操作
  callback(null, 'result');
}, 1, {
  some: 'thing',
}, thisArg, arg1, callback);

回调也将使用 channel.runStores(context, ...) 运行, 这在某些情况下可以实现上下文丢失的恢复。

import diagnostics_channel from 'node:diagnostics_channel';
import { AsyncLocalStorage } from 'node:async_hooks';

const channels = diagnostics_channel.tracingChannel('my-channel');
const myStore = new AsyncLocalStorage();

// start 通道将初始存储数据设置为某个值,
// 并将该存储数据值保存在追踪上下文对象上
channels.start.bindStore(myStore, (data) => {
  const span = new Span(data);
  data.span = span;
  return span;
});

// 然后 asyncStart 可以从之前存储的数据中恢复
channels.asyncStart.bindStore(myStore, (data) => {
  return data.span;
});
P

tracingChannel.hasSubscribers

History
  • 返回:<boolean> 如果任一单独通道有订阅者,则为 true, 否则为 false。

这是 TracingChannel 实例上提供的辅助方法,用于检查 TracingChannel Channels 中是否有任何通道存在订阅者。如果其中 任何一个至少有一个订阅者,则返回 true,否则返回 false。

import diagnostics_channel from 'node:diagnostics_channel';

const channels = diagnostics_channel.tracingChannel('my-channel');

if (channels.hasSubscribers) {
  // 执行某些操作
}

BoundedChannel 通道

History

稳定性:1 - 实验性

BoundedChannel 类是 TracingChannel 的简化版本, 仅追踪同步操作。它只包含两个通道(start 和 end), 而非五个,省略了 asyncStart、asyncEnd 和 error 事件。 这使其适合追踪不涉及异步延续 或错误处理的操作。

与 TracingChannel 一样,建议在文件顶层创建并 复用单个 BoundedChannel,而不是动态地 创建它们。

P

boundedChannel.hasSubscribers

History
  • 返回:<boolean> 如果任一单独通道有订阅者,则为 true, 否则为 false。

检查 start 或 end 通道是否有订阅者。

import { boundedChannel } from 'node:diagnostics_channel';

const wc = boundedChannel('my-operation');

if (wc.hasSubscribers) {
  // 有订阅者,执行被追踪的操作
}
M

boundedChannel.subscribe

History
boundedChannel.subscribe(handlers): void
Attributes
handlers:<Object>
通道订阅者的集合
start 事件的订阅者
end 事件的订阅者

订阅有界通道的事件。这等同于在每个通道上分别调用 channel.subscribe(onMessage)。

import { boundedChannel } from 'node:diagnostics_channel';

const wc = boundedChannel('my-operation');

wc.subscribe({
  start(message) {
    // 处理 start
  },
  end(message) {
    // 处理 end
  },
});
M

boundedChannel.unsubscribe

History
boundedChannel.unsubscribe(handlers): void
Attributes
handlers:<Object>
通道订阅者的集合
start 事件的订阅者
end 事件的订阅者

取消订阅有界通道的事件。这等同于在每个通道上分别调用 channel.unsubscribe(onMessage)。

import { boundedChannel } from 'node:diagnostics_channel';

const wc = boundedChannel('my-operation');

const handlers = {
  start(message) {},
  end(message) {},
};

wc.subscribe(handlers);
wc.unsubscribe(handlers);
M

boundedChannel.run

History
boundedChannel.run(context, fn, thisArg?, ...args?): void
Attributes
context:<Object>
用于关联事件的共享对象
要包裹追踪的函数
thisArg:<any>
函数调用时使用的接收者
...args:<any>
要传递给函数的可选参数

追踪同步函数调用。这将在执行前后产生 start 事件和 end 事件。这将在 start 通道上使用 channel.runStores(context, ...) 运行给定函数,以确保所有 事件的绑定存储都与这一追踪上下文相匹配。

import { boundedChannel } from 'node:diagnostics_channel';

const wc = boundedChannel('my-operation');

const result = wc.run({ operationId: '123' }, () => {
  // 执行操作
  return 42;
});
M

boundedChannel.withScope

History
boundedChannel.withScope(context?): void
Attributes
context:<Object>
用于关联事件的共享对象

使用 JavaScript 的显式资源管理(using 语法)创建一个 用于追踪同步操作的可释放作用域。该作用域会自动发布 start 和 end 事件、进入绑定的存储,并在释放时处理清理。

import { boundedChannel } from 'node:diagnostics_channel';

const wc = boundedChannel('my-operation');

const context = { operationId: '123' };
{
  using scope = wc.withScope(context);
  // 进入存储,start 事件已发布

  // 执行工作并在上下文上设置结果
  context.result = 42;
}
// end 事件已发布,存储自动恢复

类:BoundedChannelScope

History

稳定性:1 - 实验性

BoundedChannelScope 类表示由 boundedChannel.withScope(context) 创建的可释放作用域。它管理被追踪操作的生命周期, 自动发布事件并管理存储上下文。

必须使用 using 语法使用该作用域,以确保正确释放。

import { boundedChannel } from 'node:diagnostics_channel';

const wc = boundedChannel('my-operation');

const context = {};
{
  using scope = wc.withScope(context);
  // start 事件已发布,存储已进入
  context.result = performOperation();
  // 代码块结束时自动发布 end 事件
}

BoundedChannel 由两个诊断通道组成,表示 使用 using 语法创建的作用域的生命周期:

  • tracing:${name}:start - 在执行 using 语句时发布(作用域创建)
  • tracing:${name}:end - 在退出代码块时发布(作用域释放)

当将 using 语法与 [boundedChannel.withScope([context])][] 一起使用时,start 事件会在语句执行时立即发布,而 end 事件 会在代码块末尾释放时自动发布。所有 事件共享同一个上下文对象,在作用域执行期间 可以使用 result 等额外属性对其进行扩展。

TracingChannel 是若干诊断通道的集合,表示单个可追踪 动作执行生命周期中的特定节点。其行为被拆分为五个 诊断通道,即 start、end、asyncStart、 asyncEnd 和 error。单个可追踪动作将在所有事件 之间共享同一个事件对象,这有助于通过 weakmap 管理关联。

当任务“完成”时,这些事件对象会使用 result 或 error 值进行扩展。对于同步任务, result 将是返回值,而 error 将是函数抛出的任何内容。 对于基于回调的异步函数,result 将是回调的第二个参数, 而 error 要么是在 end 事件中可见的抛出错误, 要么是 asyncStart 或 asyncEnd 事件中的第一个 回调参数。

为确保只形成正确的追踪图,只有在开始追踪之前已有 订阅者时才应发布事件。追踪开始后添加的订阅不应 接收该追踪的后续事件, 只能看到未来的追踪。

追踪通道应遵循以下命名模式:

  • tracing:module.class.method:start 或 tracing:module.function:start
  • tracing:module.class.method:end 或 tracing:module.function:end
  • tracing:module.class.method:asyncStart 或 tracing:module.function:asyncStart
  • tracing:module.class.method:asyncEnd 或 tracing:module.function:asyncEnd
  • tracing:module.class.method:error 或 tracing:module.function:error
start(event): void
  • 名称:tracing:${name}:start

start 事件表示函数被调用的时刻。此时, 事件数据可能包含函数参数,或函数刚开始执行时 可用的其他任何内容。

end(event): void
  • 名称:tracing:${name}:end

end 事件表示函数调用返回值的时刻。 对于异步函数,这是指返回的 promise,而不是函数自身 在内部执行 return 语句。此时,如果被追踪的函数是 同步的,则 result 字段将设置为函数的返回值。 或者,error 字段可能存在, 用于表示抛出的任何错误。

建议专门监听 error 事件来跟踪错误, 因为可追踪动作可能产生多个错误。例如, 一个失败的异步任务可能在内部启动,随后任务的同步 部分抛出错误。

asyncStart(event): void
  • 名称:tracing:${name}:asyncStart

asyncStart 事件表示可追踪函数的回调或延续被 执行到。此时,可能可以获取回调参数,或表示动作 “结果”的其他任何内容。

对于基于回调的函数,回调的第一个参数如果不是 undefined 或 null,将被赋给 error 字段,第二个参数 将被赋给 result 字段。

对于 promise,resolve 路径的参数将赋给 result, reject 路径的参数将赋给 error。

建议专门监听 error 事件来跟踪错误, 因为可追踪动作可能产生多个错误。例如, 一个失败的异步任务可能在内部启动,随后任务的同步 部分抛出错误。

asyncEnd(event): void
  • 名称:tracing:${name}:asyncEnd

asyncEnd 事件表示异步函数的回调返回。 asyncStart 事件之后事件数据不太可能变化, 但查看回调完成的时刻可能很有用。

error(event): void
  • 名称:tracing:${name}:error

error 事件表示可追踪函数同步或异步产生的 任何错误。如果错误是在被追踪函数的同步部分抛出的, 该错误将被赋给事件的 error 字段并触发 error 事件。如果错误是通过回调或 promise 拒绝异步 接收到的,它也将被赋给事件的 error 字段并触发 error 事件。

单个可追踪函数调用可能会多次产生错误,因此在 使用此事件时应考虑到这一点。例如,如果内部触发 了另一个失败的异步任务,随后函数的同步部分又 抛出错误,则会发出两个 error 事件,一个对应 同步错误,另一个对应异步错误。

稳定性:1 - 实验性

Attributes
args:<any>
[]

调用 console.log() 时触发。接收传递给 console.log() 的参数数组。

Attributes
args:<any>
[]

调用 console.info() 时触发。接收传递给 console.info() 的参数数组。

Attributes
args:<any>
[]

调用 console.debug() 时触发。接收传递给 console.debug() 的参数数组。

Attributes
args:<any>
[]

调用 console.warn() 时触发。接收传递给 console.warn() 的参数数组。

Attributes
args:<any>
[]

调用 console.error() 时触发。接收传递给 console.error() 的参数数组。

Crypto

History

稳定性:1 - 实验性

Attributes
operation:<string>
由提供方定义的操作类型。
reason:<string>
由提供方定义的描述,说明操作为何 未获批准。
blocked:<boolean>
指示回调函数是否阻止了该操作。
count:<number>
此消息所表示的匹配的待处理 指示调用次数。
dropped:<number>
在传递此消息之前丢弃的额外 指示调用次数。

当 Node.js 使用的 OpenSSL FIPS 提供方在相应的提供方检查 被放宽后检测到未获 FIPS 批准的操作时触发。当提供方 配置为向后兼容时,可能出现此类操作。operation 和 reason 值来自提供方,应视为不透明字符串, 而不是稳定的枚举。

使用 --enable-fips-indicator-events 启动 Node.js 以启用此通道。 若不使用该选项,订阅不会安装 OpenSSL 回调,也不会 发布任何消息。

订阅此通道仅用于观察,绝不会改变操作的 结果。Node.js 会保留在初始化其加密支持之前 安装的任何原生指示回调的结果。当使用 --force-fips=strict 时,无论是否启用指示事件或 通道是否有订阅者,Node.js 都会拒绝回调所指示的 未批准操作。

消息在主线程上异步发布,因为 OpenSSL 指示可能 来自 Worker 或其他线程。只有主线程上的订阅会 接收消息。相对于发起操作的传递顺序未定义, 也无法将消息与特定调用或 Worker 关联 起来。

一个加密操作可能多次调用 OpenSSL 指示。匹配的 待处理调用会被合并并反映在 count 中,但这不 一定代表加密操作的次数。最多排队 256 条不同的 消息。额外的调用会记录在第一个排队消息的 dropped 中。排队的消息不会保持事件循环活跃, 因此此通道提供的是尽力而为的诊断,而非权威的 审计日志。

此通道观察 Node.js 使用的默认 OpenSSL 库上下文。 它不会观察使用另一个 OSSL_LIB_CTX 或另一份 libcrypto 的原生插件或其他代码。它适用于 OpenSSL 3.4 及更高版本,不适用于 BoringSSL。配置为直接拒绝检查的 提供方(包括严格的 OpenSSL FIPS 提供方)可以在不发出 指示的情况下拒绝操作。是否收到消息并不能证明 Node.js 或某个加密操作已通过 FIPS 验证。

import diagnosticsChannel from 'node:diagnostics_channel';

diagnosticsChannel.subscribe('crypto.fips.indicator', (message) => {
  console.error('Non-approved cryptographic operation', message);
});

稳定性:1 - 实验性

Attributes

客户端创建请求对象时触发。 与 http.client.request.start 不同,此事件在请求发送之前触发。

Attributes

客户端开始请求时触发。

Attributes

客户端请求期间发生错误时触发。

Attributes

客户端收到响应时触发。

Attributes

服务器收到请求时触发。

Attributes

服务器创建响应时触发。 此事件在响应发送之前触发。

Attributes

服务器发送响应时触发。

稳定性:1 - 实验性

Attributes

在客户端创建流时触发。

Attributes

在客户端启动流时触发。

Attributes

在客户端处理流期间发生错误时触发。

Attributes

在客户端收到流时触发。

Attributes

正在发送客户端流主体数据块时触发。

Attributes

客户端流主体完全发送后触发。

Attributes

在客户端关闭流时触发。关闭流时使用的 HTTP/2 错误码 可通过 stream.rstCode 属性获取。

Attributes

在服务器创建流时触发。

Attributes

在服务器启动流时触发。

Attributes

在服务器处理流期间发生错误时触发。

Attributes

在服务器发送流时触发。

Attributes

在服务器关闭流时触发。关闭流时使用的 HTTP/2 错误码 可通过 stream.rstCode 属性获取。

稳定性:1 - 实验性

Attributes
event:<Object>
包含以下属性
id:
传给 require() 的参数。模块名称。
parentFilename:
尝试 require(id) 的模块名称。

执行 require() 时触发。参见 start event。

Attributes
event:<Object>
包含以下属性
id:
传给 require() 的参数。模块名称。
parentFilename:
尝试 require(id) 的模块名称。

require() 调用返回时触发。参见 end event。

Attributes
event:<Object>
包含以下属性
id:
传给 require() 的参数。模块名称。
parentFilename:
尝试 require(id) 的模块名称。
error:<Error>

require() 抛出错误时触发。参见 error event。

Attributes
event:<Object>
包含以下属性
id:
传给 import() 的参数。模块名称。
parentURL:
尝试 import(id) 的模块的 URL 对象。

调用 import() 时触发。参见 asyncStart event。

Attributes
event:<Object>
包含以下属性
id:
传给 import() 的参数。模块名称。
parentURL:
尝试 import(id) 的模块的 URL 对象。

import() 完成时触发。参见 asyncEnd event。

Attributes
event:<Object>
包含以下属性
id:
传给 import() 的参数。模块名称。
parentURL:
尝试 import(id) 的模块的 URL 对象。
error:<Error>

import() 抛出错误时触发。参见 error event。

稳定性:1 - 实验性

Attributes

创建新的 TCP 或管道客户端套接字连接时触发。

Attributes

收到新的 TCP 或管道连接时触发。

Attributes
options:<Object>

调用 net.Server.listen() 时触发,在端口或管道实际设置之前。

Attributes

net.Server.listen() 完成时触发,此时服务器已准备好接受连接。

Attributes
error:<Error>

net.Server.listen() 返回错误时触发。

稳定性:1 - 实验性

Attributes

创建新的 UDP 套接字时触发。

Process

History

稳定性:1 - 实验性

Attributes

创建新进程时触发。

tracing:child_process.spawn:start

Attributes
options:<Object>

调用 child_process.spawn() 时触发,在进程实际 生成之前。

tracing:child_process.spawn:end

Attributes

child_process.spawn() 成功完成且 进程已创建时触发。

tracing:child_process.spawn:error

Attributes
error:<Error>

child_process.spawn() 遇到错误时触发。

Attributes
execPath:<string>
[]
[]

调用 process.execve() 时触发。

测试运行器

History

详情参见 Test instrumentation and OpenTelemetry。

Web 锁

History

稳定性:1 - 实验性

每次调用 locks.request() 时都会触发这些通道。参见 worker_threads.locks 了解 Web 锁的详情。

Attributes
所请求锁资源的名称。
锁模式: 'exclusive' 或 'shared' 。

发起锁请求时触发,在授予锁之前。

Attributes
所请求锁资源的名称。
锁模式: 'exclusive' 或 'shared' 。

成功授予锁且回调即将运行时触发。

Attributes
所请求锁资源的名称。
锁模式: 'exclusive' 或 'shared' 。

当 ifAvailable 为 true 且锁并非立即可用时触发, 并且请求回调以 null 而非 Lock 对象调用。

Attributes
所请求锁资源的名称。
锁模式: 'exclusive' 或 'shared' 。
steal:<boolean>
请求是否使用 steal 语义。
ifAvailable:<boolean>
请求是否使用 ifAvailable 语义。
回调抛出的错误(如有)。

锁请求完成时触发,无论回调是成功、 抛出错误,还是锁被抢占。

工作线程

History

稳定性:1 - 实验性

Attributes
worker:<Worker>

创建新线程时触发。

SQLite

History

稳定性:1 - 实验性

Attributes
已代入绑定参数值的展开后 SQL。 如果展开失败,则改用带有未替换占位符的 原始 SQL。
database:<Database>
执行该语句的  Database 实例。
duration:<number>
SQLite 内部估算的语句运行时间, 单位为纳秒。这仅反映 C 层执行时间,不包括 JavaScript 绑定开销,例如参数编组或结果行 的构造。

在 SQL 语句针对 Database 实例执行完毕后触发。 这是一个 性能分析 事件:每条语句完成时 触发一次,并报告 SQLite 内部分析器估算的耗时。 它不是分布式追踪跨度。没有对应的 start 事件, 没有异步上下文传播,也没有父跨度关联。如果需要 兼容 OpenTelemetry 的跨度或异步上下文传播,请在 JavaScript 层用 TracingChannel 包装 SQLite 调用。

没有订阅者时,发布是零开销的。

对于在迭代中途被放弃并在之后终结的语句,不会触发 任何事件,无论是通过 statement.close() 显式终结, 还是语句被垃圾回收。订阅者不得关闭数据库或该语句, 因为在事件传递期间两者仍在使用中;参见 database.close() 和 statement.close()。