⛹️‍♂️

OpenTelemetryでNode.jsを使う時の無難な設定

2023/09/26に公開

OpenTelemetryの公式のSDKにはバッチの設定やHTTPの接続など色々な設定項目があります。
この記事ではNode.jsでTraceを送信するときに使う無難な設定を紹介します。

具体例

最初に、このページで使用する具体的な設定を紹介します。
この設定をするとOpenTelemetryのデータをサーバーに送信できます。

const { Resource, processDetector, hostDetector } = require('@opentelemetry/resources');
const { OTLPTraceExporter } = require("@opentelemetry/exporter-trace-otlp-proto");
const { SemanticResourceAttributes } = require("@opentelemetry/semantic-conventions");
const { NodeSDK } = require('@opentelemetry/sdk-node');
const { getNodeAutoInstrumentations } = require('@opentelemetry/auto-instrumentations-node');

const startOtel = () => {
  const resource = Resource.default().merge(new Resource({
    [SemanticResourceAttributes.SERVICE_NAME]: "サービス名",
    [SemanticResourceAttributes.SERVICE_VERSION]: "バージョン名",
    [SemanticResourceAttributes.DEPLOYMENT_ENVIRONMENT]: "環境の名前",
  }));
  const resourceDetectors = [processDetector, hostDetector];

  const exporter = new OTLPTraceExporter({
    url: "https://xxx.example.com/v1/traces",
    headers: {
      "ヘッダー名": "ヘッダーの値"
    }
  });

  const instrumentations = [
    getNodeAutoInstrumentations({
      // fsの自動計装を使用するとNode.jsの起動時に大量のトレースが作られるため、必要がなければ使わないことをおすすめします。
      '@opentelemetry/instrumentation-fs': {
        enabled: false,
      },
    })
  ];

  const sdk = new NodeSDK({
    traceExporter: exporter,
    instrumentations: instrumentations,
    resource: resource,
    resourceDetectors: resourceDetectors
  });

  sdk.start();
}

設定しているのは大まかに次の3箇所です。

  1. サービス名やバージョンなどResourceの設定
  2. OTLPTraceExporterの設定
  3. 自動計装

設定内容の説明

具体例で使った設定について詳しくみていきましょう。

1. サービス名やバージョンなどResourceの設定

OpenTelemetryではResourceにサービス名やバージョンを設定することでデータの発生元の情報を付与することができます。
Resourceに用意されている項目は色々ありますが、サービスを運用する上では最小限「サービス名」と「バージョン」、「環境の名前」は欲しいところです。
また、プロセスIDやホスト名を付与すると、特定のサーバーでだけ起きている問題が把握できたりと便利です。

各項目は具体例のようにすると設定できます。

  const resource = Resource.default().merge(new Resource({
    [SemanticResourceAttributes.SERVICE_NAME]: "サービス名",
    [SemanticResourceAttributes.SERVICE_VERSION]: "バージョン名",
    [SemanticResourceAttributes.DEPLOYMENT_ENVIRONMENT]: "環境の名前",
  }));
  const resourceDetectors = [processDetector, hostDetector];
  • SERVICE_NAME, SERVICE_VERSION, DEPLOYMENT_ENVIRONMENT でサービス名などを設定しています。
  • processDetector を使うと、PIDや実行コマンドなどが自動で判定され設定できます。
  • hostDetector を使うと、ホスト名が自動で判定され設定できます。

2. OTLPTraceExporterの設定

OTLPTraceExporterではデータの送信方法について設定します。

  const exporter = new OTLPTraceExporter({
    url: "https://xxx.example.com/v1/traces",
    headers: {
      "ヘッダー名": "ヘッダーの値"
    }
  });

この例は次のことをしています。

  • url でデータの送信先を指定する。
  • headers で送信時のヘッダーを指定する。認証が必要な時にトークンを渡すために使うことが多いです。

3. 自動計装

次に、計装(instrumentation)を行います。

  const instrumentations = [
    getNodeAutoInstrumentations({
      // fsの自動計装を使用するとNode.jsの起動時に大量のトレースが作られるため、必要がなければ使わないことをおすすめします。
      '@opentelemetry/instrumentation-fs': {
        enabled: false,
      },
    })
  ];

getNodeAutoInstrumentations を使うと、Node.jsに関係するライブラリをまとめて計装してくれます。

※ ただし、instrumentation-fs はNode.jsの起動時に多くのトレースを発生させてしまうので、必要がなければ使わないことをおすすめします。

4. 設定をまとめて開始する

最後に、今までの設定をまとめて、データの収集・送信を開始します。

  const sdk = new NodeSDK({
    traceExporter: exporter,
    instrumentations: instrumentations,
    resource: resource,
    resourceDetectors: resourceDetectors
  });

  sdk.start();

終わりに

以上、OpenTelemetryでNode.jsを使う時の無難な設定を紹介しました。

Vaxila Labs

Discussion