Node.js
ClickStack は、テレメトリデータ(ログ、メトリクス、 トレース、例外)を収集するために OpenTelemetry の標準を使用します。トレースは自動インストルメンテーションによって生成されるため、 トレースから価値を得るために手動でインストルメンテーションを行う必要はありません。
このガイドでは次の内容を統合します:
- ログ
- メトリクス
- トレース
- 例外
はじめに
HyperDX OpenTelemetry インストルメンテーションパッケージのインストール
以下のコマンドを使用して、ClickStack OpenTelemetry パッケージ をインストールします。
- NPM
- Yarn
SDK の初期化
SDK を初期化するには、アプリケーションのエントリポイントの先頭で init 関数を呼び出す必要があります。
- require
- import
これにより、Node.js アプリケーションからトレース、メトリクス、ログが自動的に収集されます。
ログ収集のセットアップ
デフォルトでは、console.* ログが収集されます。winston や pino などのロガーを使用している場合は、ログを ClickStack に送信するためのトランスポートをロガーに追加する必要があります。別の種類のロガーを使用している場合は、
お問い合わせいただくか、該当する場合は Kubernetes などのプラットフォームインテグレーションを確認してください。
- Winston
- Pino
- console.log
ロガーとして winston を使用している場合は、次のトランスポートをロガーに追加する必要があります。
ロガーとして pino を使用している場合は、次のトランスポートをロガーに追加し、ログをトレースと相関付けるために mixin を指定する必要があります。
デフォルトで console.* メソッドのログ収集がサポートされています。追加の設定は不要です。
これを無効にするには、環境変数 HDX_NODE_CONSOLE_CAPTURE を 0 に設定するか、init 関数に consoleCapture: false を渡してください。
エラー収集のセットアップ
ClickStack SDK を使用すると、アプリケーション内で捕捉されていない例外やエラーを、スタックトレースおよびコードコンテキストとともに自動的に収集できます。
これを有効にするには、アプリケーションのエラー処理ミドルウェアの末尾に次のコードを追加するか、recordException 関数を使用して例外を手動で記録します。
- Express
- Koa
- Manual
トラブルシューティング
SDK で問題が発生している場合は、OTEL_LOG_LEVEL 環境変数を debug に設定することで詳細なログ出力を有効化できます。
高度な計装設定
コンソールログのキャプチャ
デフォルトでは、ClickStack SDK はコンソールログをキャプチャします。これを無効にするには、
HDX_NODE_CONSOLE_CAPTURE 環境変数を 0 に設定してください。
Attach user information or metadata
To easily tag all events related to a given attribute or identifier (ex. user id
or email), you can call the setTraceAttributes function which will tag every
log/span associated with the current trace after the call with the declared
attributes. It's recommended to call this function as early as possible within a
given request/trace (ex. as early in an Express middleware stack as possible).
This is a convenient way to ensure all logs/spans are automatically tagged with the right identifiers to be searched on later, instead of needing to manually tag and propagate identifiers yourself.
userId, userEmail, userName, and teamName will populate the sessions UI
with the corresponding values, but can be omitted. Any other additional values
can be specified and used to search for events.
Make sure to enable beta mode by setting HDX_NODE_BETA_MODE environment
variable to 1 or by passing betaMode: true to the init function to
enable trace attributes.
Google Cloud Run
If you're running your application on Google Cloud Run, Cloud Trace automatically injects sampling headers into incoming requests, currently restricting traces to be sampled at 0.1 requests per second for each instance.
The @hyperdx/node-opentelemetry package overwrites the sample rate to 1.0 by
default.
To change this behavior, or to configure other OpenTelemetry installations, you
can manually configure the environment variables
OTEL_TRACES_SAMPLER=parentbased_always_on and OTEL_TRACES_SAMPLER_ARG=1 to
achieve the same result.
To learn more, and to force tracing of specific requests, please refer to the Google Cloud Run documentation.
Auto-instrumented libraries
The following libraries will be automatically instrumented (traced) by the SDK:
dnsexpressgraphqlhapihttpioredisknexkoamongodbmongoosemysqlmysql2netpgpinorediswinston
Alternative installation
Run the Application with ClickStack OpenTelemetry CLI
Alternatively, you can auto-instrument your application without any code changes by using the opentelemetry-instrument CLI or using the
Node.js --require flag. The CLI installation exposes a wider range of auto-instrumented libraries and frameworks.
- Using NPX
- Custom Entry Point (ex. Nodemon, ts-node, etc.)
- Code Import
The OTEL_SERVICE_NAME environment variable is used to identify your service in the HyperDX app, it can be any name you want.
Enabling exception capturing
To enable uncaught exception capturing, you'll need to set the HDX_NODE_EXPERIMENTAL_EXCEPTION_CAPTURE environment variable to 1.
その後、Express や Koa で発生した例外を自動的に収集したり、例外を手動で捕捉したりするには、上記の Setup Error Collection セクションの手順に従ってください。
自動インストルメントされるライブラリ
上記のインストール方法により、次のライブラリが自動的にインストルメント(トレース)されます: