AI翻訳通知

このドキュメントはAIによって翻訳されました。正確な情報については英語版をご参照ください。

テレメトリ

実験的

NocoBase のテレメトリ (Telemetry) モジュールは、OpenTelemetry をベースにラップされています。この記事では、テレメトリモジュールを使用してトレース (Trace) やメトリクス (Metric) データを収集し、NocoBase システムのオブザーバビリティ (Observability) を向上させる方法について説明します。

インストルメンテーション

メトリクス

const meter = app.telemetry.metric.getMeter();
const counter = meter.createCounter('event_counter', {});
counter.add(1);

参考:

トレース

const tracer = app.telemetry.trace.getTracer();
tracer.startActiveSpan();
tracer.startSpan();

参考:

ライブラリ

import { Plugin } from '@nocobase/server';
import { getNodeAutoInstrumentations } from '@opentelemetry/auto-instrumentations-node';

class InstrumentationPlugin extends Plugin {
  afterAdd() {
    this.app.on('beforeLoad', (app) => {
      app.telemetry.addInstrumentation(getNodeAutoInstrumentations());
    });
  }
}
注意

NocoBase におけるテレメトリモジュールの初期化位置は app.beforeLoad です。そのため、すべてのインストルメンテーションライブラリが NocoBase に適しているわけではありません。
例えば、instrumentation-koaKoa のインスタンス化の前にインポートする必要があります。NocoBase の ApplicationKoa をベースにしていますが、テレメトリモジュールは Application のインスタンス化の後に初期化されるため、適用することができません。

参考:

収集

メトリクス

import { Plugin } from '@nocobase/server';
import {
  PeriodicExportingMetricReader,
  ConsoleMetricExporter,
} from '@opentelemetry/sdk-metrics';

class MetricReaderPlugin extends Plugin {
  afterAdd() {
    this.app.on('beforeLoad', (app) => {
      app.telemetry.metric.registerReader(
        'console',
        () =>
          new PeriodicExportingMetricReader({
            exporter: new ConsoleMetricExporter(),
          }),
      );
    });
  }
}

トレース

import { Plugin } from '@nocobase/server';
import {
  BatchSpanProcessor,
  ConsoleSpanExporter,
} from '@opentelemetry/sdk-trace-base';

class TraceSpanProcessorPlugin extends Plugin {
  afterAdd() {
    this.app.on('beforeLoad', (app) => {
      app.telemetry.trace.registerProcessor(
        'console',
        () => new BatchSpanProcessor(new ConsoleSpanExporter()),
      );
    });
  }
}

参考: