> ## Documentation Index
> Fetch the complete documentation index at: https://clickhouse.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# telemetrygen で合成 OpenTelemetry データを生成する

> telemetrygen を使用して、多様な合成ログ、トレース、メトリクスを完全マネージド型の ClickHouse オブザーバビリティに送信します

> [tab] 完全マネージド型
>
> [ベータ版](https://clickhouse.com/docs/reference/settings/beta-and-experimental-features.md#beta-features)
>
> [`telemetrygen`](https://github.com/open-telemetry/opentelemetry-collector-contrib/tree/main/cmd/telemetrygen) は、OpenTelemetry Collector Contrib に含まれるデータジェネレーターです。合成の OTLP ログ、トレース、メトリクスを出力でき、フラグを使ってデータの形状を調整できます。たとえば、複数のサービス、ログの重大度、スパンのステータスや子スパン、さまざまなメトリクスの型などを指定できます。このツールを使って完全マネージド型の ClickHouse オブザーバビリティにテストデータを送信し、検索および Chart Explorer で結果を確認しましょう。
>
> **1. 前提条件**
>
> 既存の完全マネージド型 ClickHouse オブザーバビリティサービスに加え、そのマネージドテレメトリーエンドポイントへのネットワークアクセスが必要です。詳しくは[完全マネージド型の Getting Started ガイド](https://clickhouse.com/docs/ja/clickstack/observability/getting-started.md)を参照してください。
>
> **2. telemetrygen をインストール**
>
> Docker をインストールして起動した状態で、`telemetrygen` を公式のコンテナイメージから実行するためのラッパーを定義します。
>
> ```shell theme={null}
> telemetrygen() {
>   docker run --rm \
>     ghcr.io/open-telemetry/opentelemetry-collector-contrib/telemetrygen:latest "$@"
> }
> ```
>
> または、Go を使用してバイナリをインストールします。
>
> ```shell theme={null}
> go install github.com/open-telemetry/opentelemetry-collector-contrib/cmd/telemetrygen@latest
> ```
>
> **3. 環境変数を設定する**
>
> ClickHouse オブザーバビリティのサービスを開き、**チーム設定 → Managed Telemetry** に移動します。**OTLP HTTP エンドポイント**と **APIキー** をコピーします。
>
> ![チーム設定の Managed Telemetry 画面。OTLP HTTP エンドポイントと APIキーがマスクされた状態で表示されている](https://mintcdn.com/private-7c7dfe99/Zk1feNn6fCQBpold/images/clickstack/getting-started/observability-managed-telemetry.png?fit=max\&auto=format\&n=Zk1feNn6fCQBpold\&q=85\&s=e63a2ac2e6ab996d3fc360ebe0f487a5)
>
> エンドポイントには、`https://` プレフィックスを取り除いたホストとポートを指定します。例えば、`https://YOUR_OTEL_ENDPOINT:4318` であれば `YOUR_OTEL_ENDPOINT:4318` とします。`/v1/logs` のようなシグナルのパスは付加しないでください。
>
> ```shell theme={null}
> export OTEL_ENDPOINT='YOUR_OTEL_ENDPOINT:4318'
> printf "API key: "
> read -r -s OTEL_API_KEY
> printf "\n"
> export OTEL_API_KEY
> ```
>
> 小さな `tg` ヘルパーを定義して、各コマンドでは変わる部分 (サービス、重大度、ステータス、属性) だけを指定すれば済むようにします。
>
> ```shell theme={null}
> tg() { local signal=$1; shift; telemetrygen "$signal" \
>   --otlp-endpoint "${OTEL_ENDPOINT}" --otlp-http \
>   --otlp-header "authorization=\"Bearer ${OTEL_API_KEY}\"" \
>   --rate 5 --duration 30s "$@"; }
> ```
>
> ヘルパーは、証明書検証を有効にした TLS 経由の OTLP HTTP を使用します。`--otlp-insecure` と `--otlp-insecure-skip-verify` は指定しないでください。各コマンドは 30 秒間実行され、マネージドテレメトリーの APIキーを使用して認証します。
>
> **4. ログを生成**
>
> 3つのサービスから、情報、警告、エラーの各ログを送信します。
>
> ```shell theme={null}
> tg logs --service frontend --severity-text Info  --severity-number 9  --body "GET /api/products 200" \
>   --otlp-attributes 'deployment.environment="production"' \
>   --telemetry-attributes 'http.method="GET"' --telemetry-attributes 'http.status_code="200"'
> tg logs --service checkout --severity-text Warn  --severity-number 13 --body "retrying payment authorization" \
>   --otlp-attributes 'deployment.environment="production"' \
>   --telemetry-attributes 'http.method="POST"'
> tg logs --service payment  --severity-text Error --severity-number 17 --body "payment gateway timeout" \
>   --otlp-attributes 'deployment.environment="production"' \
>   --telemetry-attributes 'http.status_code="500"'
> ```
>
> 特に便利なログ用フラグは次のとおりです。
>
> * `--service` は `service.name` を設定し、イベントをどのサービスのものか識別できるようにします。
> * `--severity-text` と `--severity-number` はレベルを設定します (`severity-number` の範囲は 1～24 です) 。
> * `--body` はログメッセージを設定します。
> * `--otlp-attributes` はリソースレベルの属性を設定します (`key="value"`、`key=true`、または `key=<integer>`) 。
> * `--telemetry-attributes` はレコードごとの属性を設定します。
>
> **5. トレースを生成**
>
> 複数のサービスから、成功ステータスとエラーステータスを含む複数スパンのトレースを送信します。各コマンドはそれぞれ独立したトレースを生成するため、これらの例ではサービス間の呼び出しは発生しません。
>
> ```shell theme={null}
> # Healthy services: the bulk of the traffic, all spans Ok
> for svc in frontend checkout cart; do
>   tg traces --service "$svc" --child-spans 3 --span-duration 80ms --status-code Ok \
>     --otlp-attributes 'deployment.environment="production"' \
>     --telemetry-attributes "http.route=\"/$svc\""
> done
>
> # A service with slower spans and errors
> tg traces --service payment --child-spans 3 --span-duration 450ms --span-links 1 --status-code Error \
>   --otlp-attributes 'deployment.environment="production"' \
>   --telemetry-attributes 'http.route="/charge"'
> ```
>
> 特に便利なトレース用フラグは次のとおりです。
>
> * `--child-spans` は、トレースごとに指定した数の子スパンを生成し、各トレースに実際の階層構造を持たせます。
> * `--span-duration` は、各スパンの所要時間を設定します (例: `120ms`、`2s`) 。
> * `--status-code` には `Unset`、`Error`、`Ok` (または `0`、`1`、`2`) のいずれかを指定します。エラー関連のビューを検証するには `Error` を指定します。
> * `--span-links` は、スパン間にリンクを追加します。
> * `--workers` は、複数のジェネレーターを並列に実行し、より多様なデータをより大量に生成します。
>
> **6. メトリクスを生成**
>
> Sum、Gauge、ヒストグラムの各メトリクスを送信します。これらは合成された値であり、実際のシステムの計測値ではありません。各コマンドは 30 秒後に自動的に停止します。
>
> ```shell theme={null}
> tg metrics --service frontend --metric-type Sum       --otlp-metric-name http.server.requests --aggregation-temporality cumulative
> tg metrics --service frontend --metric-type Gauge     --otlp-metric-name system.memory.usage
> tg metrics --service payment  --metric-type Histogram --otlp-metric-name http.server.duration
> ```
>
> `--metric-type` には `Gauge`、`Sum`、`Histogram`、`ExponentialHistogram` のいずれかを指定します。`--otlp-metric-name` では系列の名前を指定し、UI でその系列を見つけられるようにします。`--aggregation-temporality` には `delta` または `cumulative` を指定します。
>
> **7. ClickHouse オブザーバビリティで確認する**
>
> ClickHouse オブザーバビリティサービスを開きます。`Search` ビューで時間範囲を `Last 15 minutes` に設定し、ログソースを `Logs` と `Traces` で切り替えます。`ServiceName` でフィルタすると `frontend`、`checkout`、`cart`、`payment` の各サービスを確認でき、`SeverityText` でフィルタすると警告およびエラーのログ行を絞り込めます。`payment` のトレースを開くと、子スパンとエラーステータスを確認できます。`Chart Explorer` を開いて `Metrics` を選択し、上記で設定したメトリクス名のいずれか (例：`http.server.requests`) をチャートに表示して、メトリクスがインジェストされていることを確認します。

> [tab] セルフマネージド
>
> [`telemetrygen`](https://github.com/open-telemetry/opentelemetry-collector-contrib/tree/main/cmd/telemetrygen) は、OpenTelemetry Collector Contrib のデータジェネレーターです。合成した OTLP のログ、トレース、メトリクスを送信でき、フラグを使って生成するデータを細かく調整できます。たとえば、複数のサービス、ログの重大度、スパンのステータスと子スパン、さまざまなメトリクス型などを指定できます。ClickStack OpenTelemetryコレクターがデータを正しく受信しているか、また多様で現実的なイベントがオブザーバビリティ UI に表示されるかを確認する際に使用してください。
>
> このガイドでは、collector がすでに稼働しており、`4317` (gRPC) および `4318` (HTTP) で OTLP エンドポイントが公開されていることを前提としています。
>
> **1. 前提条件**
>
> このガイドでは、[ClickHouse オブザーバビリティ — セルフマネージド版の Getting Started ガイド](https://clickhouse.com/docs/ja/clickstack/self-configured/getting-started/managed.md)を完了していること、および `telemetrygen` を実行するマシンから OTLP gRPC (`4317`) と HTTP (`4318`) のエンドポイントに接続可能な状態で OpenTelemetry collector が稼働していることを前提としています。`OTLP_AUTH_TOKEN` で [collector を保護](https://clickhouse.com/docs/ja/clickstack/self-configured/ingesting-data/collector.md#securing-the-collector-self-configured)している場合は、その値をすぐ使えるように控えておいてください。
>
> **2. telemetrygen をインストール**
>
> `telemetrygen` は Docker イメージから実行します (インストールは不要です) 。以下のコマンドを読みやすくするため、小さなラッパーを定義します。`--add-host` を指定すると、コンテナからホスト上で待ち受けている collector にアクセスできるようになります。
>
> ```shell theme={null}
> telemetrygen() {
>   docker run --rm --add-host=host.docker.internal:host-gateway \
>     ghcr.io/open-telemetry/opentelemetry-collector-contrib/telemetrygen:latest "$@"
> }
> export OTEL_ENDPOINT=host.docker.internal:4317
> ```
>
> または、Go でバイナリをインストールし、代わりに `localhost` に接続します：
>
> ```shell theme={null}
> go install github.com/open-telemetry/opentelemetry-collector-contrib/cmd/telemetrygen@latest
> export OTEL_ENDPOINT=localhost:4317
> ```
>
> **3. 環境変数を設定する**
>
> collector が保護されている場合は、認証トークンをエクスポートします。
>
> ```shell theme={null}
> export OTLP_AUTH_TOKEN=<your_otlp_auth_token>
> ```
>
> > [!NOTE]
> >
> > **保護されていない collector**
> >
> > ClickStack OpenTelemetryコレクターは、デフォルトでは認証なしで動作します。[collector の保護](https://clickhouse.com/docs/ja/clickstack/self-configured/ingesting-data/collector.md#securing-the-collector-self-configured) の手順に従って `OTLP_AUTH_TOKEN` を設定していない場合は、以下のヘルパーから `--otlp-header` の行を削除してください。
>
> 各コマンドでは変化する部分 (サービス、重大度、ステータス、属性) だけを指定すれば済むように、小さな `tg` ヘルパーを定義します。
>
> ```shell theme={null}
> tg() { local signal=$1; shift; telemetrygen "$signal" \
>   --otlp-endpoint ${OTEL_ENDPOINT} --otlp-insecure \
>   --otlp-header "authorization=\"${OTLP_AUTH_TOKEN}\"" \
>   --rate 5 --duration 30s "$@"; }
> ```
>
> **4. ログを生成**
>
> ログは単一で均質なストリームとしてではなく、複数のサービスにまたがる現実的な重大度の組み合わせで送信します。大半を情報レベルとし、警告とエラーをそれぞれ1件ずつ含めます。
>
> ```shell theme={null}
> tg logs --service frontend --severity-text Info  --severity-number 9  --body "GET /api/products 200" \
>   --otlp-attributes 'deployment.environment="production"' \
>   --telemetry-attributes 'http.method="GET"' --telemetry-attributes 'http.status_code="200"'
> tg logs --service checkout --severity-text Warn  --severity-number 13 --body "retrying payment authorization" \
>   --otlp-attributes 'deployment.environment="production"' \
>   --telemetry-attributes 'http.method="POST"'
> tg logs --service payment  --severity-text Error --severity-number 17 --body "payment gateway timeout" \
>   --otlp-attributes 'deployment.environment="production"' \
>   --telemetry-attributes 'http.status_code="500"'
> ```
>
> 特に役立つログ用フラグは次のとおりです。
>
> * `--service` は `service.name` を設定し、イベントがどのサービスから発生したかを識別できるようにします。
> * `--severity-text` と `--severity-number` はレベルを設定します (`severity-number` の範囲は 1～24 です) 。
> * `--body` はログメッセージを設定します。
> * `--otlp-attributes` はリソースレベルの属性を設定します (`key="value"`、`key=true`、または `key=<integer>`) 。
> * `--telemetry-attributes` はレコード単位の属性を設定します。
>
> **5. トレースを生成**
>
> 複数の正常なサービスと、障害が発生している 1 つの依存先から、複数のスパンで構成されるトレースを送信します。これにより、サービスマップは大部分が正常で 1 つのサービスだけがエラーを返すという現実的な状態になり、エラー関連のビューにもデータが投入されます。
>
> ```shell theme={null}
> # Healthy services: the bulk of the traffic, all spans Ok
> for svc in frontend checkout cart; do
>   tg traces --service "$svc" --child-spans 3 --span-duration 80ms --status-code Ok \
>     --otlp-attributes 'deployment.environment="production"' \
>     --telemetry-attributes "http.route=\"/$svc\""
> done
>
> # One slow dependency returning errors
> tg traces --service payment --child-spans 3 --span-duration 450ms --span-links 1 --status-code Error \
>   --otlp-attributes 'deployment.environment="production"' \
>   --telemetry-attributes 'http.route="/charge"'
> ```
>
> 特に有用なトレース関連のフラグは次のとおりです。
>
> * `--child-spans` は、トレースごとに指定した数の子スパンを生成し、各トレースに実際の階層構造を持たせます。
> * `--span-duration` は、各スパンの所要時間を設定します (例: `120ms`、`2s`) 。
> * `--status-code` には `Unset`、`Error`、`Ok` (または `0`、`1`、`2`) のいずれかを指定します。エラー関連のビューを試すには `Error` を使用します。
> * `--span-links` は、スパン間にリンクを追加します。
> * `--workers` は、複数のジェネレーターを並列に実行し、より大量で多様なデータを生成します。
>
> **6. メトリクスを生成**
>
> 一般的な 3 種類のメトリクス型を送信し、ダッシュボードでカウンター、Gauge、ディストリビューション (ヒストグラム) を利用できるようにします。一部の生成ツールとは異なり、`telemetrygen` はメトリクスでも `--duration` の指定に従うため、手動で停止する必要はありません。
>
> ```shell theme={null}
> tg metrics --service frontend --metric-type Sum       --otlp-metric-name http.server.requests --aggregation-temporality cumulative
> tg metrics --service frontend --metric-type Gauge     --otlp-metric-name system.memory.usage
> tg metrics --service payment  --metric-type Histogram --otlp-metric-name http.server.duration
> ```
>
> `--metric-type` には `Gauge`、`Sum`、`Histogram`、`ExponentialHistogram` のいずれかを指定できます。`--otlp-metric-name` は系列の名前を指定するオプションで、UI 上でその系列を見つけやすくするために使用します。`--aggregation-temporality` には `delta` または `cumulative` を指定します。
>
> **7. オブザーバビリティUIで確認する**
>
> ClickHouse Cloud コンソールからオブザーバビリティ UI を開きます。`Search` ビューで時間範囲を `Last 15 minutes` に設定し、ログソースを `Logs` と `Traces` で切り替えます。`ServiceName` でフィルタすると `frontend`、`checkout`、`cart`、`payment` の各サービスを確認でき、`SeverityText` でフィルタすると警告およびエラーのログ行を絞り込めます。`payment` のトレースを開き、子スパンとエラーステータスを確認します。`Chart Explorer` を開いて `Metrics` を選択し、上記で設定したメトリクス名のいずれか (例：`http.server.requests`) をチャートに表示して、メトリクスのインジェストが行われていることを確認します。
