Node.js v21.7.2 ドキュメント
- Node.js v21.7.2
- ► 目次
-
► インデックス
- アサーションテスト
- 非同期コンテキスト追跡
- Async Hooks
- Buffer
- C++ アドオン
- Node-API を使用した C/C++ アドオン
- C++ Embedder API
- 子プロセス
- クラスタ
- コマンドラインオプション
- Console
- Corepack
- Crypto
- デバッガ
- 非推奨API
- Diagnostics Channel
- DNS
- Domain
- エラー
- イベント
- ファイルシステム
- グローバルオブジェクト
- HTTP
- HTTP/2
- HTTPS
- Inspector
- 国際化
- モジュール: CommonJS モジュール
- モジュール: ECMAScript モジュール
- モジュール: `node:module` API
- モジュール: パッケージ
- Net
- OS
- Path
- パフォーマンスフック
- パーミッション
- Process
- Punycode
- クエリ文字列
- Readline
- REPL
- レポート
- 単一実行可能アプリケーション
- Stream
- 文字列デコーダ
- テストランナー
- タイマー
- TLS/SSL
- トレースイベント
- TTY
- UDP/データグラム
- URL
- ユーティリティ
- V8
- VM
- WASI
- Web Crypto API
- Web Streams API
- ワーカースレッド
- Zlib
- ► その他のバージョン
- ► オプション
Diagnostics Channel#
ソースコード: 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)
#
diagnostics_channel.subscribe(name, 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)
#
diagnostics_channel.unsubscribe(name, 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> チャネルからunbindするストア。- 戻り値: <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を返す関数の呼び出しをトレースします。これにより、関数の同期部分の周囲に常に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');
}, {
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チャネル#
TracingChannelは、単一のトレース可能なアクションの実行ライフサイクル内の特定のポイントを表す複数のdiagnostics_channelsの集合です。動作は、start
、end
、asyncStart
、asyncEnd
、error
からなる5つのdiagnostics_channelsに分割されます。単一のトレース可能なアクションは、すべてのイベント間で同じイベントオブジェクトを共有します。これは、weakmapを通じて相関を管理するのに役立ちます。
これらのイベントオブジェクトは、タスクが「完了」すると、result
またはerror
値で拡張されます。同期タスクの場合、result
は戻り値になり、error
は関数からスローされたものになります。コールバックベースの非同期関数では、result
はコールバックの2番目の引数になり、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)
#
- 名前:
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
イベントがトリガーされます。コールバックまたはプロミス拒否を通じて非同期的にエラーを受け取ると、それもイベントのerror
フィールドに割り当てられ、error
イベントがトリガーされます。
単一のトレース可能な関数呼び出しで複数回エラーが発生する可能性があるため、このイベントを使用する際には考慮する必要があります。たとえば、内部で別の非同期タスクがトリガーされ失敗し、その後関数の同期部分がエラーをスローすると、同期エラーと非同期エラーの2つのerror
イベントが発行されます。
組み込みチャネル#
diagnostics_channel API は現在安定版と見なされていますが、現在利用可能な組み込みチャネルは安定版ではありません。各チャネルは個別に安定版として宣言する必要があります。
HTTP#
http.client.request.start
request
<http.ClientRequest>
クライアントがリクエストを開始したときに発行されます。
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.finish
request
<http.IncomingMessage>response
<http.ServerResponse>socket
<net.Socket>server
<http.Server>
サーバーがレスポンスを送信したときに発行されます。
NET#
net.client.socket
socket
<net.Socket>
新しいTCPまたはパイプクライアントソケットが作成されたときに発行されます。
net.server.socket
socket
<net.Socket>
新しいTCPまたはパイプ接続が受信されたときに発行されます。
UDP#
udp.socket
socket
<dgram.Socket>
新しいUDPソケットが作成されたときに発行されます。
プロセス#
子プロセス
process
<ChildProcess>
新しいプロセスが作成されたときに発行されます。
ワーカスレッド#
worker_threads
worker
Worker
新しいスレッドが作成されたときに発行されます。