Node.js v25.0.0 ドキュメンテーション
- Node.js v25.0.0
-
目次
- Diagnostics Channel
- パブリック API
- 概要
- クラス:
Channel - クラス:
TracingChanneltracingChannel.subscribe(subscribers)tracingChannel.unsubscribe(subscribers)tracingChannel.traceSync(fn[, context[, thisArg[, ...args]]])tracingChannel.tracePromise(fn[, context[, thisArg[, ...args]]])tracingChannel.traceCallback(fn[, position[, context[, thisArg[, ...args]]]])tracingChannel.hasSubscribers
- TracingChannel のチャネル
- 組み込みチャネル
- Console
- HTTP
- HTTP/2
- イベント:
'http2.client.stream.created' - イベント:
'http2.client.stream.start' - イベント:
'http2.client.stream.error' - イベント:
'http2.client.stream.finish' - イベント:
'http2.client.stream.close' - イベント:
'http2.server.stream.created' - イベント:
'http2.server.stream.start' - イベント:
'http2.server.stream.error' - イベント:
'http2.server.stream.finish' - イベント:
'http2.server.stream.close'
- イベント:
- モジュール
- NET
- UDP
- Process
- ワーカースレッド
- パブリック API
- Diagnostics Channel
-
索引
- アサーションテスト
- 非同期コンテキストの追跡
- Async hooks
- Buffer
- C++アドオン
- Node-API を使用した C/C++ アドオン
- C++ embedder API
- 子プロセス
- Cluster
- コマンドラインオプション
- Console
- Crypto
- Debugger
- 非推奨のAPI
- Diagnostics Channel
- DNS
- Domain
- 環境変数
- エラー
- Events
- ファイルシステム
- Globals
- HTTP
- HTTP/2
- HTTPS
- Inspector
- 国際化
- モジュール: CommonJS モジュール
- モジュール: ECMAScript モジュール
- モジュール:
node:moduleAPI - モジュール: パッケージ
- モジュール: TypeScript
- Net
- OS
- Path
- Performance hooks
- パーミッション
- Process
- Punycode
- クエリストリング
- Readline
- REPL
- レポート
- 単一実行可能ファイルアプリケーション
- SQLite
- Stream
- String decoder
- テストランナー
- タイマー
- TLS/SSL
- トレースイベント
- TTY
- UDP/datagram
- URL
- ユーティリティ
- V8
- VM
- WASI
- Web Crypto API
- Web Streams API
- ワーカースレッド
- Zlib
- 他のバージョン
- オプション
診断チャネル#
ソースコード: lib/diagnostics_channel.js
node:diagnostics_channel モジュールは、診断目的で任意のメッセージデータを報告するための名前付きチャネルを作成する API を提供します。
以下のようにアクセスできます。
import diagnostics_channel from 'node:diagnostics_channel';const diagnostics_channel = require('node:diagnostics_channel');
診断メッセージを報告したいモジュール作成者は、メッセージを報告するためのトップレベルのチャネルを1つまたは複数作成することを意図しています。チャネルは実行時に取得することもできますが、そうすることによる追加のオーバーヘッドのため推奨されません。チャネルは便宜上エクスポートすることもできますが、名前がわかっていればどこでも取得できます。
モジュールが他の人が利用するための診断データを作成する場合、使用される名前付きチャネルとメッセージデータの形式についてのドキュメントを含めることが推奨されます。チャネル名は、他のモジュールからのデータとの衝突を避けるために、通常モジュール名を含めるべきです。
パブリック API#
概要#
以下にパブリック API の簡単な概要を示します。
import diagnostics_channel from 'node:diagnostics_channel';
// Get a reusable channel object
const channel = diagnostics_channel.channel('my-channel');
function onMessage(message, name) {
// Received data
}
// Subscribe to the channel
diagnostics_channel.subscribe('my-channel', onMessage);
// Check if the channel has an active subscriber
if (channel.hasSubscribers) {
// Publish data to the channel
channel.publish({
some: 'data',
});
}
// Unsubscribe from the channel
diagnostics_channel.unsubscribe('my-channel', onMessage);const diagnostics_channel = require('node:diagnostics_channel');
// Get a reusable channel object
const channel = diagnostics_channel.channel('my-channel');
function onMessage(message, name) {
// Received data
}
// Subscribe to the channel
diagnostics_channel.subscribe('my-channel', onMessage);
// Check if the channel has an active subscriber
if (channel.hasSubscribers) {
// Publish data to the channel
channel.publish({
some: 'data',
});
}
// Unsubscribe from the channel
diagnostics_channel.unsubscribe('my-channel', onMessage);
diagnostics_channel.hasSubscribers(name)#
指定された名前のチャネルにアクティブなサブスクライバがいるかどうかを確認します。これは、送信したいメッセージの準備にコストがかかる場合に役立ちます。
この API は任意ですが、パフォーマンスに非常に敏感なコードからメッセージを公開しようとする場合に役立ちます。
import diagnostics_channel from 'node:diagnostics_channel';
if (diagnostics_channel.hasSubscribers('my-channel')) {
// There are subscribers, prepare and publish message
}const diagnostics_channel = require('node:diagnostics_channel');
if (diagnostics_channel.hasSubscribers('my-channel')) {
// There are subscribers, prepare and publish message
}
diagnostics_channel.channel(name)#
これは、名前付きチャネルに公開したい人にとっての主要なエントリーポイントです。公開時のオーバーヘッドをできるだけ減らすように最適化されたチャネルオブジェクトを生成します。
import diagnostics_channel from 'node:diagnostics_channel';
const channel = diagnostics_channel.channel('my-channel');const diagnostics_channel = require('node:diagnostics_channel');
const channel = diagnostics_channel.channel('my-channel');
diagnostics_channel.subscribe(name, onMessage)#
name<string> | <symbol> チャネル名onMessage<Function> チャネルメッセージを受け取るハンドラ
このチャネルをサブスクライブするためのメッセージハンドラを登録します。このメッセージハンドラは、チャネルにメッセージが公開されるたびに同期的に実行されます。メッセージハンドラ内でスローされたエラーは、'uncaughtException' をトリガーします。
import diagnostics_channel from 'node:diagnostics_channel';
diagnostics_channel.subscribe('my-channel', (message, name) => {
// Received data
});const diagnostics_channel = require('node:diagnostics_channel');
diagnostics_channel.subscribe('my-channel', (message, name) => {
// Received data
});
diagnostics_channel.unsubscribe(name, onMessage)#
name<string> | <symbol> チャネル名onMessage<Function> 削除する、以前にサブスクライブしたハンドラ- 戻り値: <boolean> ハンドラが見つかった場合は
true、そうでない場合はfalse。
diagnostics_channel.subscribe(name, onMessage) で以前にこのチャネルに登録されたメッセージハンドラを削除します。
import diagnostics_channel from 'node:diagnostics_channel';
function onMessage(message, name) {
// Received data
}
diagnostics_channel.subscribe('my-channel', onMessage);
diagnostics_channel.unsubscribe('my-channel', onMessage);const diagnostics_channel = require('node:diagnostics_channel');
function onMessage(message, name) {
// Received data
}
diagnostics_channel.subscribe('my-channel', onMessage);
diagnostics_channel.unsubscribe('my-channel', onMessage);
diagnostics_channel.tracingChannel(nameOrChannels)#
nameOrChannels<string> | <TracingChannel> チャネル名、またはすべての TracingChannel のチャネル を含むオブジェクト- 戻り値: <TracingChannel> トレースに使用するチャネルのコレクション
指定された TracingChannel のチャネル のための TracingChannel ラッパーを作成します。名前が指定された場合、対応するトレースチャネルは tracing:${name}:${eventType} の形式で作成されます。ここで eventType は TracingChannel のチャネル のタイプに対応します。
import diagnostics_channel from 'node:diagnostics_channel';
const channelsByName = diagnostics_channel.tracingChannel('my-channel');
// or...
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'),
});const diagnostics_channel = require('node:diagnostics_channel');
const channelsByName = diagnostics_channel.tracingChannel('my-channel');
// or...
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'),
});
クラス: Channel#
クラス Channel はデータパイプライン内の個別の名前付きチャネルを表します。サブスクライバを追跡し、サブスクライバが存在する場合にメッセージを公開するために使用されます。公開時のチャネル検索を避けるために別個のオブジェクトとして存在し、非常に高速な公開速度を可能にし、非常に最小限のコストで多用できます。チャネルは diagnostics_channel.channel(name) で作成され、new Channel(name) で直接チャネルを構築することはサポートされていません。
channel.hasSubscribers#
- 戻り値: <boolean> アクティブなサブスクライバがいる場合は true
このチャネルにアクティブなサブスクライバがいるかどうかを確認します。これは、送信したいメッセージの準備にコストがかかる場合に役立ちます。
この API は任意ですが、パフォーマンスに非常に敏感なコードからメッセージを公開しようとする場合に役立ちます。
import diagnostics_channel from 'node:diagnostics_channel';
const channel = diagnostics_channel.channel('my-channel');
if (channel.hasSubscribers) {
// There are subscribers, prepare and publish message
}const diagnostics_channel = require('node:diagnostics_channel');
const channel = diagnostics_channel.channel('my-channel');
if (channel.hasSubscribers) {
// There are subscribers, prepare and publish message
}
channel.publish(message)#
message<any> チャネルのサブスクライバに送信するメッセージ
チャネルのサブスクライバにメッセージを公開します。これにより、メッセージハンドラが同期的にトリガーされるため、同じコンテキスト内で実行されます。
import diagnostics_channel from 'node:diagnostics_channel';
const channel = diagnostics_channel.channel('my-channel');
channel.publish({
some: 'message',
});const diagnostics_channel = require('node:diagnostics_channel');
const channel = diagnostics_channel.channel('my-channel');
channel.publish({
some: 'message',
});
channel.subscribe(onMessage)#
onMessage<Function> チャネルメッセージを受け取るハンドラ
このチャネルをサブスクライブするためのメッセージハンドラを登録します。このメッセージハンドラは、チャネルにメッセージが公開されるたびに同期的に実行されます。メッセージハンドラ内でスローされたエラーは、'uncaughtException' をトリガーします。
import diagnostics_channel from 'node:diagnostics_channel';
const channel = diagnostics_channel.channel('my-channel');
channel.subscribe((message, name) => {
// Received data
});const diagnostics_channel = require('node:diagnostics_channel');
const channel = diagnostics_channel.channel('my-channel');
channel.subscribe((message, name) => {
// Received data
});
channel.unsubscribe(onMessage)#
onMessage<Function> 削除する、以前にサブスクライブしたハンドラ- 戻り値: <boolean> ハンドラが見つかった場合は
true、そうでない場合はfalse。
channel.subscribe(onMessage) で以前にこのチャネルに登録されたメッセージハンドラを削除します。
import diagnostics_channel from 'node:diagnostics_channel';
const channel = diagnostics_channel.channel('my-channel');
function onMessage(message, name) {
// Received data
}
channel.subscribe(onMessage);
channel.unsubscribe(onMessage);const diagnostics_channel = require('node:diagnostics_channel');
const channel = diagnostics_channel.channel('my-channel');
function onMessage(message, name) {
// Received data
}
channel.subscribe(onMessage);
channel.unsubscribe(onMessage);
channel.bindStore(store[, transform])#
store<AsyncLocalStorage> コンテキストデータをバインドするストア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 };
});const diagnostics_channel = require('node:diagnostics_channel');
const { AsyncLocalStorage } = require('node:async_hooks');
const store = new AsyncLocalStorage();
const channel = diagnostics_channel.channel('my-channel');
channel.bindStore(store, (data) => {
return { data };
});
channel.unbindStore(store)#
store<AsyncLocalStorage> チャネルからアンバインドするストア。- 戻り値: <boolean> ストアが見つかった場合は
true、そうでない場合はfalse。
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);const diagnostics_channel = require('node:diagnostics_channel');
const { AsyncLocalStorage } = require('node:async_hooks');
const store = new AsyncLocalStorage();
const channel = diagnostics_channel.channel('my-channel');
channel.bindStore(store);
channel.unbindStore(store);
channel.runStores(context, fn[, thisArg[, ...args]])#
context<any> サブスクライバに送信し、ストアにバインドするメッセージfn<Function> 入力されたストレージコンテキスト内で実行するハンドラ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' })
});const diagnostics_channel = require('node:diagnostics_channel');
const { AsyncLocalStorage } = require('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' })
});
クラス: TracingChannel#
クラス TracingChannel は、単一のトレース可能なアクションを表現する TracingChannel のチャネル のコレクションです。これは、アプリケーションフローをトレースするためのイベントを生成するプロセスを形式化し、簡素化するために使用されます。diagnostics_channel.tracingChannel() は TracingChannel を構築するために使用されます。Channel と同様に、動的に作成するのではなく、ファイルのトップレベルで単一の TracingChannel を作成して再利用することが推奨されます。
tracingChannel.subscribe(subscribers)#
subscribers<Object> TracingChannel のチャネル サブスクライバのセットstart<Function>startイベント のサブスクライバend<Function>endイベント のサブスクライバasyncStart<Function>asyncStartイベント のサブスクライバasyncEnd<Function>asyncEndイベント のサブスクライバerror<Function>errorイベント のサブスクライバ
対応するチャネルに関数のコレクションをサブスクライブするためのヘルパーです。これは、各チャネルで個別に channel.subscribe(onMessage) を呼び出すのと同じです。
import diagnostics_channel from 'node:diagnostics_channel';
const channels = diagnostics_channel.tracingChannel('my-channel');
channels.subscribe({
start(message) {
// Handle start message
},
end(message) {
// Handle end message
},
asyncStart(message) {
// Handle asyncStart message
},
asyncEnd(message) {
// Handle asyncEnd message
},
error(message) {
// Handle error message
},
});const diagnostics_channel = require('node:diagnostics_channel');
const channels = diagnostics_channel.tracingChannel('my-channel');
channels.subscribe({
start(message) {
// Handle start message
},
end(message) {
// Handle end message
},
asyncStart(message) {
// Handle asyncStart message
},
asyncEnd(message) {
// Handle asyncEnd message
},
error(message) {
// Handle error message
},
});
tracingChannel.unsubscribe(subscribers)#
subscribers<Object> TracingChannel のチャネル サブスクライバのセットstart<Function>startイベント のサブスクライバend<Function>endイベント のサブスクライバasyncStart<Function>asyncStartイベント のサブスクライバasyncEnd<Function>asyncEndイベント のサブスクライバerror<Function>errorイベント のサブスクライバ
- 戻り値: <boolean> すべてのハンドラが正常にアンサブスクライブされた場合は
true、そうでない場合はfalse。
対応するチャネルから関数のコレクションをアンサブスクライブするためのヘルパーです。これは、各チャネルで個別に channel.unsubscribe(onMessage) を呼び出すのと同じです。
import diagnostics_channel from 'node:diagnostics_channel';
const channels = diagnostics_channel.tracingChannel('my-channel');
channels.unsubscribe({
start(message) {
// Handle start message
},
end(message) {
// Handle end message
},
asyncStart(message) {
// Handle asyncStart message
},
asyncEnd(message) {
// Handle asyncEnd message
},
error(message) {
// Handle error message
},
});const diagnostics_channel = require('node:diagnostics_channel');
const channels = diagnostics_channel.tracingChannel('my-channel');
channels.unsubscribe({
start(message) {
// Handle start message
},
end(message) {
// Handle end message
},
asyncStart(message) {
// Handle asyncStart message
},
asyncEnd(message) {
// Handle asyncEnd message
},
error(message) {
// Handle error message
},
});
tracingChannel.traceSync(fn[, context[, thisArg[, ...args]]])#
fn<Function> トレースでラップする関数context<Object> イベントを相関させるための共有オブジェクトthisArg<any> 関数呼び出しに使用されるレシーバ...args<any> 関数に渡すオプションの引数- 戻り値: <any> 指定された関数の戻り値
同期関数呼び出しをトレースします。これは常に実行の前後に start イベント と end イベント を生成し、指定された関数がエラーをスローした場合は error イベント を生成することがあります。これは、start チャネルで channel.runStores(context, ...) を使用して指定された関数を実行します。これにより、すべてのイベントでバインドされたストアがこのトレースコンテキストに一致するように設定されます。
正しいトレースグラフのみが形成されるようにするため、イベントはトレースを開始する前にサブスクライバが存在する場合にのみ公開されます。トレースが開始された後に追加されたサブスクリプションは、そのトレースからの将来のイベントを受け取らず、将来のトレースのみが表示されます。
import diagnostics_channel from 'node:diagnostics_channel';
const channels = diagnostics_channel.tracingChannel('my-channel');
channels.traceSync(() => {
// Do something
}, {
some: 'thing',
});const diagnostics_channel = require('node:diagnostics_channel');
const channels = diagnostics_channel.tracingChannel('my-channel');
channels.traceSync(() => {
// Do something
}, {
some: 'thing',
});
tracingChannel.tracePromise(fn[, context[, thisArg[, ...args]]])#
fn<Function> トレースでラップする Promise を返す関数context<Object> トレースイベントを相関させるための共有オブジェクトthisArg<any> 関数呼び出しに使用されるレシーバ...args<any> 関数に渡すオプションの引数- 戻り値: <Promise> 指定された関数が返す Promise からチェーンされた Promise
Promise を返す関数呼び出しをトレースします。これは常に、関数の同期部分の実行前後に start イベント と end イベント を生成し、Promise の継続に達したときに asyncStart イベント と asyncEnd イベント を生成します。また、指定された関数がエラーをスローしたり、返された Promise が拒否されたりした場合は、error イベント を生成することもあります。これは、start チャネルで channel.runStores(context, ...) を使用して指定された関数を実行します。これにより、すべてのイベントでバインドされたストアがこのトレースコンテキストに一致するように設定されます。
正しいトレースグラフのみが形成されるようにするため、イベントはトレースを開始する前にサブスクライバが存在する場合にのみ公開されます。トレースが開始された後に追加されたサブスクリプションは、そのトレースからの将来のイベントを受け取らず、将来のトレースのみが表示されます。
import diagnostics_channel from 'node:diagnostics_channel';
const channels = diagnostics_channel.tracingChannel('my-channel');
channels.tracePromise(async () => {
// Do something
}, {
some: 'thing',
});const diagnostics_channel = require('node:diagnostics_channel');
const channels = diagnostics_channel.tracingChannel('my-channel');
channels.tracePromise(async () => {
// Do something
}, {
some: 'thing',
});
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, ...) を使用して指定された関数を実行します。これにより、すべてのイベントでバインドされたストアがこのトレースコンテキストに一致するように設定されます。
正しいトレースグラフのみが形成されるようにするため、イベントはトレースを開始する前にサブスクライバが存在する場合にのみ公開されます。トレースが開始された後に追加されたサブスクリプションは、そのトレースからの将来のイベントを受け取らず、将来のトレースのみが表示されます。
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, ...) で実行されるため、一部のケースでコンテキスト損失の回復が可能になります。
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;
});
tracingChannel.hasSubscribers#
- 戻り値: <boolean> 個々のチャネルのいずれかにサブスクライバがいる場合は
true、いない場合はfalse。
これは TracingChannel インスタンスで利用できるヘルパーメソッドで、TracingChannel のチャネル のいずれかにサブスクライバがいるかどうかを確認します。いずれかに少なくとも1つのサブスクライバがいる場合は true が返され、そうでない場合は false が返されます。
import diagnostics_channel from 'node:diagnostics_channel';
const channels = diagnostics_channel.tracingChannel('my-channel');
if (channels.hasSubscribers) {
// Do something
}const diagnostics_channel = require('node:diagnostics_channel');
const channels = diagnostics_channel.tracingChannel('my-channel');
if (channels.hasSubscribers) {
// Do something
}
TracingChannel のチャネル#
TracingChannel は、単一のトレース可能なアクションの実行ライフサイクルにおける特定の時点を表す複数の diagnostics_channel のコレクションです。動作は start、end、asyncStart、asyncEnd、error の5つの diagnostics_channel に分割されます。単一のトレース可能なアクションは、すべてのイベント間で同じイベントオブジェクトを共有します。これは、weakmap を通じて相関関係を管理するのに役立ちます。
これらのイベントオブジェクトは、タスクが「完了」したときに result または error の値で拡張されます。同期タスクの場合、result は関数の戻り値になり、error は関数からスローされたものになります。コールバックベースの非同期関数では、result はコールバックの2番目の引数になり、error は end イベントで見えるスローされたエラーか、asyncStart または asyncEnd イベントのいずれかの最初のコールバック引数になります。
正しいトレースグラフのみが形成されるようにするため、イベントはトレースを開始する前にサブスクライバが存在する場合にのみ公開されるべきです。トレースが開始された後に追加されたサブスクリプションは、そのトレースからの将来のイベントを受け取るべきではなく、将来のトレースのみが表示されます。
トレースチャネルは、以下の命名パターンに従うべきです。
tracing:module.class.method:startまたはtracing:module.function:starttracing:module.class.method:endまたはtracing:module.function:endtracing:module.class.method:asyncStartまたはtracing:module.function:asyncStarttracing:module.class.method:asyncEndまたはtracing:module.function:asyncEndtracing:module.class.method:errorまたはtracing:module.function:error
start(event)#
- 名前:
tracing:${name}:start
start イベントは、関数が呼び出された時点を表します。この時点で、イベントデータには関数の引数や、関数の実行のまさに開始時点で利用可能なものが含まれる場合があります。
end(event)#
- 名前:
tracing:${name}:end
end イベントは、関数呼び出しが値を返した時点を表します。非同期関数の場合、これは関数自体が内部で return 文を実行したときではなく、返された Promise が返されたときです。この時点で、トレースされた関数が同期的であった場合、result フィールドは関数の戻り値に設定されます。あるいは、error フィールドが存在して、スローされたエラーを表すことがあります。
トレース可能なアクションが複数のエラーを生成する可能性があるため、エラーを追跡するには特に error イベントをリッスンすることが推奨されます。例えば、失敗した非同期タスクが内部で開始された後、タスクの同期部分がエラーをスローする場合があります。
asyncStart(event)#
- 名前:
tracing:${name}:asyncStart
asyncStart イベントは、トレース可能な関数のコールバックまたは継続に到達した時点を表します。この時点では、コールバック引数や、アクションの「結果」を表すものが利用可能になる場合があります。
コールバックベースの関数の場合、コールバックの最初の引数は undefined または null でない場合 error フィールドに割り当てられ、2番目の引数は result フィールドに割り当てられます。
Promise の場合、resolve パスへの引数は result に割り当てられ、reject パスへの引数は error に割り当てられます。
トレース可能なアクションが複数のエラーを生成する可能性があるため、エラーを追跡するには特に error イベントをリッスンすることが推奨されます。例えば、失敗した非同期タスクが内部で開始された後、タスクの同期部分がエラーをスローする場合があります。
asyncEnd(event)#
- 名前:
tracing:${name}:asyncEnd
asyncEnd イベントは、非同期関数のコールバックが返った時点を表します。asyncStart イベントの後にイベントデータが変更される可能性は低いですが、コールバックが完了した時点を見るのに役立つ場合があります。
error(event)#
- 名前:
tracing:${name}:error
error イベントは、トレース可能な関数によって同期的または非同期的に生成された任意のエラーを表します。トレースされた関数の同期部分でエラーがスローされた場合、エラーはイベントの error フィールドに割り当てられ、error イベントがトリガーされます。コールバックまたは Promise の拒否を介して非同期的にエラーが受信された場合も、イベントの error フィールドに割り当てられ、error イベントがトリガーされます。
単一のトレース可能な関数呼び出しが複数回エラーを生成する可能性があるため、このイベントを利用する際にはこの点を考慮する必要があります。例えば、内部でトリガーされた別の非同期タスクが失敗し、その後関数の同期部分がエラーをスローした場合、同期エラーと非同期エラーのために2つの error イベントが発行されます。
組み込みチャネル#
コンソール#
イベント: 'console.info'#
args<any[]>
console.info() が呼び出されたときに発行されます。console.info() に渡された引数の配列を受け取ります。
イベント: 'console.debug'#
args<any[]>
console.debug() が呼び出されたときに発行されます。console.debug() に渡された引数の配列を受け取ります。
HTTP#
イベント: 'http.client.request.created'#
request<http.ClientRequest>
クライアントがリクエストオブジェクトを作成したときに発行されます。http.client.request.start とは異なり、このイベントはリクエストが送信される前に発行されます。
イベント: 'http.client.request.error'#
request<http.ClientRequest>error<Error>
クライアントリクエスト中にエラーが発生したときに発行されます。
イベント: 'http.client.response.finish'#
request<http.ClientRequest>response<http.IncomingMessage>
クライアントがレスポンスを受信したときに発行されます。
イベント: 'http.server.request.start'#
request<http.IncomingMessage>response<http.ServerResponse>socket<net.Socket>server<http.Server>
サーバーがリクエストを受信したときに発行されます。
イベント: 'http.server.response.created'#
request<http.IncomingMessage>response<http.ServerResponse>
サーバーがレスポンスを作成したときに発行されます。このイベントはレスポンスが送信される前に発行されます。
イベント: 'http.server.response.finish'#
request<http.IncomingMessage>response<http.ServerResponse>socket<net.Socket>server<http.Server>
サーバーがレスポンスを送信したときに発行されます。
HTTP/2#
イベント: 'http2.client.stream.created'#
stream<ClientHttp2Stream>headers<HTTP/2 ヘッダーオブジェクト>
クライアント上でストリームが作成されたときに発行されます。
イベント: 'http2.client.stream.start'#
stream<ClientHttp2Stream>headers<HTTP/2 ヘッダーオブジェクト>
クライアント上でストリームが開始されたときに発行されます。
イベント: 'http2.client.stream.error'#
stream<ClientHttp2Stream>error<Error>
クライアント上でのストリーム処理中にエラーが発生したときに発行されます。
イベント: 'http2.client.stream.finish'#
stream<ClientHttp2Stream>headers<HTTP/2 ヘッダーオブジェクト>flags<number>
クライアント上でストリームが受信されたときに発行されます。
イベント: 'http2.client.stream.close'#
stream<ClientHttp2Stream>
クライアント上でストリームが閉じられたときに発行されます。ストリームを閉じる際に使用された HTTP/2 エラーコードは、stream.rstCode プロパティを使用して取得できます。
イベント: 'http2.server.stream.created'#
stream<ServerHttp2Stream>headers<HTTP/2 ヘッダーオブジェクト>
サーバー上でストリームが作成されたときに発行されます。
イベント: 'http2.server.stream.start'#
stream<ServerHttp2Stream>headers<HTTP/2 ヘッダーオブジェクト>
サーバー上でストリームが開始されたときに発行されます。
イベント: 'http2.server.stream.error'#
stream<ServerHttp2Stream>error<Error>
サーバー上でのストリーム処理中にエラーが発生したときに発行されます。
イベント: 'http2.server.stream.finish'#
stream<ServerHttp2Stream>headers<HTTP/2 ヘッダーオブジェクト>flags<number>
サーバー上でストリームが送信されたときに発行されます。
イベント: 'http2.server.stream.close'#
stream<ServerHttp2Stream>
サーバー上でストリームが閉じられたときに発行されます。ストリームを閉じる際に使用された HTTP/2 エラーコードは、stream.rstCode プロパティを使用して取得できます。
モジュール#
イベント: 'module.require.start'#
event<Object> 以下のプロパティを含むidrequire()に渡された引数。モジュール名。parentFilenamerequire(id) を試みたモジュールの名前。
require() が実行されたときに発行されます。start イベント を参照してください。
イベント: 'module.require.end'#
event<Object> 以下のプロパティを含むidrequire()に渡された引数。モジュール名。parentFilenamerequire(id) を試みたモジュールの名前。
require() 呼び出しが返ったときに発行されます。end イベント を参照してください。
イベント: 'module.require.error'#
event<Object> 以下のプロパティを含むidrequire()に渡された引数。モジュール名。parentFilenamerequire(id) を試みたモジュールの名前。
error<Error>
require() がエラーをスローしたときに発行されます。error イベント を参照してください。
イベント: 'module.import.asyncStart'#
event<Object> 以下のプロパティを含むidimport()に渡された引数。モジュール名。parentURLimport(id) を試みたモジュールの URL オブジェクト。
import() が呼び出されたときに発行されます。asyncStart イベント を参照してください。
イベント: 'module.import.asyncEnd'#
event<Object> 以下のプロパティを含むidimport()に渡された引数。モジュール名。parentURLimport(id) を試みたモジュールの URL オブジェクト。
import() が完了したときに発行されます。asyncEnd イベント を参照してください。
イベント: 'module.import.error'#
event<Object> 以下のプロパティを含むidimport()に渡された引数。モジュール名。parentURLimport(id) を試みたモジュールの URL オブジェクト。
error<Error>
import() がエラーをスローしたときに発行されます。error イベント を参照してください。
NET#
イベント: 'net.client.socket'#
socket<net.Socket> | <tls.TLSSocket>
新しい TCP またはパイプのクライアントソケット接続が作成されたときに発行されます。
イベント: 'tracing:net.server.listen:asyncStart'#
server<net.Server>options<Object>
net.Server.listen() が呼び出され、ポートまたはパイプが実際にセットアップされる前に発行されます。
イベント: 'tracing:net.server.listen:asyncEnd'#
server<net.Server>
net.Server.listen() が完了し、サーバーが接続を受け入れる準備ができたときに発行されます。
イベント: 'tracing:net.server.listen:error'#
server<net.Server>error<Error>
net.Server.listen() がエラーを返しているときに発行されます。