0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Functionsにカスタムメトリックを送信する

Posted at

ライブラリ

@opentelemetry/apiを使って、カスタムメトリックを送信する
AzureMonitorとの統合には@azure/monitor-opentelemetryuseAzureMonitorを使う

※似た方法として、Application Insights SDKがMSから提供されていますが、去年の夏ごろに廃止していく方針を発表しています。
ここでは、方針に則ってOpenTelemetry Distroで実装していきます。
image.png
Application Insights を使用して Node.js サービスとアプリを監視する

Opentelemetryのカスタムメトリックについて

アプリケーションやシステムの状態を数値として測定し監視するため、大きく以下3種類が用意されています。

メトリクス種類 データの方向性 主な用途 更新方法
Counter 増加のみ イベント数、リクエスト数などの累積値を追跡 明示的に add メソッドを呼び出す
Histogram 分布を記録 応答時間やデータサイズの分布測定 明示的に record メソッドを呼び出す
Observable Metrics 増減可能 メモリ使用量、CPU使用率などの動的な値を監視 コールバック関数内で値を観測

ここでは、最も汎用的なヒストグラムでの送信を考えます。

ヒストグラム

送信方法は以下。
やり方はシンプルでcreateHistogramでヒストグラムを作成して、recordで送信。
引数に2つ目にJSONを追加して、ディメンション分割することも可能。

useAzureMonitor({
  azureMonitorExporterOptions: {
    connectionString: APPINSIGHTS_CONNECTION_STRING,
  },
  enableLiveMetrics: true,
});

export const opentelemetry = {
  async sendHistogram(args: { metricName: string; customMetric: CustomMetric[]; logger: Logger }) {
    const { metricName, customMetric, logger } = args;

    logger.info(`CustomMetric send start. metricName: ${metricName} count: ${customMetric.length}`);

    // Get the meter for the "metricName" namespace
    const meter = metrics.getMeter(metricName);

    // Create a histogram metric
    const histogram = meter.createHistogram(metricName);

    customMetric.map((cm) => histogram.record(cm.value, { [cm.dimensionName]: cm.dimensionValue }));

    logger.info(`CustomMetric send end. metricName: ${metricName}`);
  },
};

実際に送信してみる

テストデータとして、以下のようなMetric1,Metric2のデータを用意した。

  const metricName = 'testMetric';
  const customMetric: CustomMetric[] = [
    {
      dimensionName: 'Metric',
      dimensionValue: 'Metric1',
      value: 30,
    },
    {
      dimensionName: 'Metric',
      dimensionValue: 'Metric2',
      value: 60,
    },
  ];

10分ほどデータを流して、ApplicationInsightsで確認する。
testMetricというメトリック名で、ディメンション別にグラフ表示できていることを確認できた。
image.png

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?