> ## 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.

> Grafana の ClickHouse データソースプラグインの設定オプション

# Grafana で ClickHouse データソースを設定する

export const ClickHouseSupportedBadge = () => {
  return <div className="ClickHouseSupportedBadge">
            <div className="ClickHouseSupportedIcon">
                <svg width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg">
                    <path d="M1.30762 1.39073C1.30762 1.3103 1.37465 1.22986 1.46849 1.22986H2.64824C2.72868 1.22986 2.80912 1.29689 2.80912 1.39073V14.4886C2.80912 14.5691 2.74209 14.6495 2.64824 14.6495H1.46849C1.38805 14.6495 1.30762 14.5825 1.30762 14.4886V1.39073Z" fill="currentColor" />
                    <path d="M4.2832 1.39073C4.2832 1.3103 4.35023 1.22986 4.44408 1.22986H5.62383C5.70427 1.22986 5.7847 1.29689 5.7847 1.39073V14.4886C5.7847 14.5691 5.71767 14.6495 5.62383 14.6495H4.44408C4.36364 14.6495 4.2832 14.5825 4.2832 14.4886V1.39073Z" fill="currentColor" />
                    <path d="M7.25977 1.39073C7.25977 1.3103 7.3268 1.22986 7.42064 1.22986H8.60039C8.68083 1.22986 8.76127 1.29689 8.76127 1.39073V14.4886C8.76127 14.5691 8.69423 14.6495 8.60039 14.6495H7.42064C7.3402 14.6495 7.25977 14.5825 7.25977 14.4886V1.39073Z" fill="currentColor" />
                    <path d="M10.2354 1.39073C10.2354 1.3103 10.3024 1.22986 10.3962 1.22986H11.576C11.6564 1.22986 11.7369 1.29689 11.7369 1.39073V14.4886C11.7369 14.5691 11.6698 14.6495 11.576 14.6495H10.3962C10.3158 14.6495 10.2354 14.5825 10.2354 14.4886V1.39073Z" fill="currentColor" />
                    <path d="M13.2256 6.6057C13.2256 6.52526 13.2926 6.44482 13.3865 6.44482H14.5662C14.6466 6.44482 14.7271 6.51186 14.7271 6.6057V9.27354C14.7271 9.35398 14.6601 9.43442 14.5662 9.43442H13.3865C13.306 9.43442 13.2256 9.36739 13.2256 9.27354V6.6057Z" fill="currentColor" />
                </svg>
            </div>
            ClickHouse対応
        </div>;
};

export const Image = ({img, alt, size = "lg"}) => {
  const normalizedSize = ["sm", "md", "lg"].includes(size) ? size : "lg";
  return <div className={`ch-image-${normalizedSize}`}>
      <Frame>
        <img src={img} alt={alt} />
      </Frame>
    </div>;
};

<ClickHouseSupportedBadge />

設定を変更する最も簡単な方法は、プラグイン設定ページの Grafana UI を使用することですが、データソースは [YAML ファイルを使ってプロビジョニングする](https://grafana.com/docs/grafana/latest/administration/provisioning/#data-sources)こともできます。

このページでは、ClickHouse プラグインで設定可能なオプションの一覧と、YAML でデータソースをプロビジョニングする方向けの設定スニペットを紹介します。

すべてのオプションを手早く確認したい場合は、設定オプションの完全な一覧を[こちら](#all-yaml-options)で確認できます。

<div id="common-settings">
  ## 共通設定
</div>

設定画面の例：

<Image size="sm" img="https://mintcdn.com/private-7c7dfe99/PWQnWTwcu17exYX2/images/integrations/data-visualization/grafana/config_common.webp?fit=max&auto=format&n=PWQnWTwcu17exYX2&q=85&s=ef8007c65279bb9df23fb52a0c6361d7" alt="セキュアなネイティブ設定の例" border width="601" height="813" data-path="images/integrations/data-visualization/grafana/config_common.webp" />

共通設定の YAML 設定例：

```yaml theme={null}
jsonData:
  host: 127.0.0.1 # (required) server address.
  port: 9000      # (required) server port. For native, defaults to 9440 secure and 9000 insecure. For HTTP, defaults to 8443 secure and 8123 insecure.

  protocol: native # (required) the protocol used for the connection. Can be set to "native" or "http".
  secure: false    # set to true if the connection is secure.

  username: default # the username used for authentication.

  tlsSkipVerify:     <boolean> # skips TLS verification when set to true.
  tlsAuth:           <boolean> # set to true to enable TLS client authentication.
  tlsAuthWithCACert: <boolean> # set to true if CA certificate is provided. Required for verifying self-signed TLS certificates.

secureJsonData:
  password: secureExamplePassword # the password used for authentication.

  tlsCACert:     <string> # TLS CA certificate
  tlsClientCert: <string> # TLS client certificate
  tlsClientKey:  <string> # TLS client key
```

設定をUIから保存すると、`version` プロパティが追加される点に注意してください。これは、その設定がどのバージョンのプラグインで保存されたかを示します。

<div id="http-protocol">
  ### HTTP protocol
</div>

HTTP protocol で接続を選択すると、追加の設定項目が表示されます。

<Image size="md" img="https://mintcdn.com/private-7c7dfe99/PWQnWTwcu17exYX2/images/integrations/data-visualization/grafana/config_http.webp?fit=max&auto=format&n=PWQnWTwcu17exYX2&q=85&s=173a06bd7c26a19b181299cbf4e60f59" alt="追加の HTTP 設定オプション" border width="975" height="442" data-path="images/integrations/data-visualization/grafana/config_http.webp" />

<div id="http-path">
  #### HTTP パス
</div>

HTTP サーバーが別の URL パスで公開されている場合は、ここで設定できます。

```yaml theme={null}
jsonData:
  # 先頭のスラッシュを除く
  path: additional/path/example
```

<div id="custom-http-headers">
  #### カスタム HTTP ヘッダー
</div>

サーバーに送信するリクエストに、カスタムヘッダーを追加できます。

ヘッダーには、平文のものとセキュアなものがあります。
ヘッダー名はすべて平文で保存されますが、セキュアなヘッダーの値はセキュア設定に保存されます (`password` フィールドと同様です) 。

<Warning>
  **HTTP 経由でのセキュアな値**

  セキュアなヘッダーの値は設定内で安全に保存されますが、セキュア接続が無効になっている場合、その値自体は HTTP 経由で送信されます。
</Warning>

平文/セキュアヘッダーの YAML 例:

```yaml theme={null}
jsonData:
  httpHeaders:
  - name: X-Example-Plain-Header
    value: plain text value
    secure: false
  - name: X-Example-Secure-Header
    # "value" は除外されます
    secure: true
secureJsonData:
  secureHttpHeaders.X-Example-Secure-Header: secure header value
```

<div id="additional-settings">
  ## 追加設定
</div>

これらの設定は任意です。

<Image size="sm" img="https://mintcdn.com/private-7c7dfe99/PWQnWTwcu17exYX2/images/integrations/data-visualization/grafana/config_additional.webp?fit=max&auto=format&n=PWQnWTwcu17exYX2&q=85&s=335542c340901788318c16cd8625b5c3" alt="追加設定の例" border width="406" height="452" data-path="images/integrations/data-visualization/grafana/config_additional.webp" />

YAML の例:

```yaml theme={null}
jsonData:
  defaultDatabase: default # default database loaded by the query builder. Defaults to "default".
  defaultTable: <string>   # default table loaded by the query builder.

  dialTimeout: 10    # dial timeout when connecting to the server, in seconds. Defaults to "10".
  queryTimeout: 60   # query timeout when running a query, in seconds. Defaults to 60. This requires permissions on the user, if you get a permission error try setting it to "0" to disable it.
  validateSql: false # when set to true, will validate the SQL in the SQL editor.
```

<div id="opentelemetry">
  ### OpenTelemetry
</div>

OpenTelemetry (OTel) は、このプラグインに密接に統合されています。
OpenTelemetryデータは、[exporter plugin](https://github.com/open-telemetry/opentelemetry-collector-contrib/tree/main/exporter/clickhouseexporter)を使用してClickHouseにエクスポートできます。
最適に活用するには、[ログ](#logs)と[トレース](#traces)の両方でOTelを設定することを推奨します。

また、強力なオブザーバビリティワークフローを実現する機能である[データリンク](/docs/ja/integrations/connectors/data-visualization/grafana/query-builder#data-links)を有効にするには、これらのデフォルト設定を構成する必要があります。

<div id="logs">
  ### ログ
</div>

[ログのクエリ構築](/docs/ja/integrations/connectors/data-visualization/grafana/query-builder#logs)を高速化するために、ログクエリ用のデフォルトのデータベース/テーブルとカラムを設定できます。これにより、クエリビルダーに実行可能なログクエリが事前に読み込まれ、Explore ページでログをすばやく確認できるようになり、オブザーバビリティ用途での閲覧が効率化されます。

OpenTelemetry を使用している場合は、"**Use OTel**" スイッチを有効にし、**デフォルトのログテーブル** を `otel_logs` に設定してください。
これにより、デフォルトのカラムは自動的に上書きされ、選択した OTel スキーマバージョンが使用されます。

ログで OpenTelemetry は必須ではありませんが、logs/trace で単一のデータセットを使用すると、[データリンク](/docs/ja/integrations/connectors/data-visualization/grafana/query-builder#data-links)によって、よりスムーズなオブザーバビリティのワークフローを実現できます。

ログ設定画面の例:

<Image size="sm" img="https://mintcdn.com/private-7c7dfe99/PWQnWTwcu17exYX2/images/integrations/data-visualization/grafana/config_logs.webp?fit=max&auto=format&n=PWQnWTwcu17exYX2&q=85&s=657c4d40f4f9875dc6c33ff8a538ed51" alt="Logs config" border width="460" height="402" data-path="images/integrations/data-visualization/grafana/config_logs.webp" />

ログ設定 YAML の例:

```yaml theme={null}
jsonData:
  logs:
    defaultDatabase: default # default log database.
    defaultTable: otel_logs  # default log table. If you're using OTel, this should be set to "otel_logs".

    otelEnabled: false  # set to true if OTel is enabled.
    otelVersion: latest # the otel collector schema version to be used. Versions are displayed in the UI, but "latest" will use latest available version in the plugin.

    # Default columns to be selected when opening a new log query. Will be ignored if OTel is enabled.
    timeColumn:       <string> # the primary time column for the log.
    levelColumn:   <string> # the log level/severity of the log. Values typically look like "INFO", "error", or "Debug".
    messageColumn: <string> # the log's message/content.
```

<div id="traces">
  ### トレース
</div>

[トレースのクエリビルダー](/docs/ja/integrations/connectors/data-visualization/grafana/query-builder#traces)を高速化するには、トレースクエリ用のデフォルトのデータベース/テーブルとカラムを設定できます。これにより、実行可能なトレース検索クエリがクエリビルダーに事前に読み込まれ、Explore ページでオブザーバビリティデータをよりすばやく参照できるようになります。

OpenTelemetry を使用している場合は、「**Use OTel**」スイッチを有効にし、**デフォルトのトレーステーブル**を `otel_traces` に設定してください。
これにより、選択した OTel スキーマバージョンを使用するよう、デフォルトのカラムが自動的に上書きされます。
OpenTelemetry は必須ではありませんが、トレースにそのスキーマを使用する場合、この機能は最も効果的に動作します。

トレース設定画面の例:

<Image size="sm" img="https://mintcdn.com/private-7c7dfe99/PWQnWTwcu17exYX2/images/integrations/data-visualization/grafana/config_traces.webp?fit=max&auto=format&n=PWQnWTwcu17exYX2&q=85&s=08216481b97764c5ffd49b94716ea0aa" alt="トレース設定" border width="476" height="625" data-path="images/integrations/data-visualization/grafana/config_traces.webp" />

トレース設定 YAML の例:

```yaml theme={null}
jsonData:
  traces:
    defaultDatabase: default  # default trace database.
    defaultTable: otel_traces # default trace table. If you're using OTel, this should be set to "otel_traces".

    otelEnabled: false  # set to true if OTel is enabled.
    otelVersion: latest # the otel collector schema version to be used. Versions are displayed in the UI, but "latest" will use latest available version in the plugin.

    # Default columns to be selected when opening a new trace query. Will be ignored if OTel is enabled.
    traceIdColumn:       <string>    # trace ID column.
    spanIdColumn:        <string>    # span ID column.
    operationNameColumn: <string>    # operation name column.
    parentSpanIdColumn:  <string>    # parent span ID column.
    serviceNameColumn:   <string>    # service name column.
    durationTimeColumn:  <string>    # duration time column.
    durationUnitColumn:  <time unit> # duration time unit. Can be set to "seconds", "milliseconds", "microseconds", or "nanoseconds". For OTel the default is "nanoseconds".
    startTimeColumn:     <string>    # start time column. This is the primary time column for the trace span.
    tagsColumn:          <string>    # tags column. This is expected to be a map type.
    serviceTagsColumn:   <string>    # service tags column. This is expected to be a map type.
```

<div id="column-aliases">
  ### カラムの別名
</div>

カラムに別名を付けると、異なる名前や型でデータをクエリできるようになり、便利です。
別名を使えば、ネストされたスキーマをフラット化して、Grafana で簡単に選択できるようにできます。

次のような場合は、別名付けが役立つ可能性があります。

* スキーマと、そのネストされたプロパティや型の大半を把握している
* データを Map 型に格納している
* JSON を文字列として保存している
* 選択するカラムを変換するために、関数をよく適用する

<div id="table-defined-alias-columns">
  #### テーブルで定義する ALIAS カラム
</div>

ClickHouse にはカラムのエイリアス機能が組み込まれており、Grafana でもそのまま利用できます。
ALIAS カラムはテーブルに直接定義できます。

```sql theme={null}
CREATE TABLE alias_example (
  TimestampNanos DateTime(9),
  TimestampDate ALIAS toDate(TimestampNanos)
)
```

上記の例では、ナノ秒のタイムスタンプを `Date` 型に変換する `TimestampDate` という別名を作成します。
このデータは最初のカラムのようにディスクに保存されず、クエリ実行時に計算されます。
テーブルで定義された別名は `SELECT *` では返されませんが、これはサーバー設定で変更できます。

詳細については、[ALIAS](/docs/ja/reference/statements/create/table#alias) カラム型のドキュメントを参照してください。

<div id="column-alias-tables">
  #### カラム別名テーブル
</div>

デフォルトでは、Grafana は `DESC table` の応答に基づいてカラム候補を提示します。
場合によっては、Grafana に表示されるカラムを完全に上書きしたいこともあります。
これにより、カラム選択時に Grafana 上でスキーマを見えにくくでき、テーブルの複雑さによってはユーザー体験の向上につながります。

テーブル定義内の別名を使う場合と比べた利点は、テーブルを変更しなくても簡単に更新できることです。スキーマによっては、これが数千件のエントリに及ぶことがあり、基になるテーブル定義が煩雑になる場合があります。また、ユーザーに無視してほしいカラムを非表示にすることもできます。

Grafana では、別名テーブルに次のカラム構造が必要です。

```sql theme={null}
CREATE TABLE aliases (
  `alias` String,  -- The name of the alias, as seen in the Grafana column selector
  `select` String, -- The SELECT syntax to use in the SQL generator
  `type` String    -- The type of the resulting column, so the plugin can modify the UI options to match the data type.
)
```

`ALIAS` カラムの動作は、別名テーブル を使って次のように再現できます。

```sql theme={null}
CREATE TABLE example_table (
  TimestampNanos DateTime(9)
);

CREATE TABLE example_table_aliases (`alias` String, `select` String, `type` String);

INSERT INTO example_table_aliases (`alias`, `select`, `type`) VALUES
('TimestampNanos', 'TimestampNanos', 'DateTime(9)'), -- Preserve original column from table (optional)
('TimestampDate', 'toDate(TimestampNanos)', 'Date'); -- Add new column that converts TimestampNanos to a Date
```

次に、このテーブルを Grafana で使えるように設定できます。なお、名前は任意で、別のデータベースで定義することも可能です。

<Image size="md" img="https://mintcdn.com/private-7c7dfe99/PWQnWTwcu17exYX2/images/integrations/data-visualization/grafana/alias_table_config_example.webp?fit=max&auto=format&n=PWQnWTwcu17exYX2&q=85&s=0b8151e07c947f2afba1a4f91b86086b" alt="別名テーブル設定の例" border width="974" height="199" data-path="images/integrations/data-visualization/grafana/alias_table_config_example.webp" />

これで Grafana では、`DESC example_table` の結果ではなく、別名テーブル の結果が表示されます。

<Image size="md" img="https://mintcdn.com/private-7c7dfe99/PWQnWTwcu17exYX2/images/integrations/data-visualization/grafana/alias_table_select_example.webp?fit=max&auto=format&n=PWQnWTwcu17exYX2&q=85&s=e04230ca34881112baab14f67b8ec663" alt="別名テーブル選択の例" border width="508" height="188" data-path="images/integrations/data-visualization/grafana/alias_table_select_example.webp" />

どちらのエイリアス方式も、複雑な型変換や JSON フィールドの抽出に利用できます。

<div id="all-yaml-options">
  ## すべてのYAMLオプション
</div>

以下は、このプラグインで利用できるYAML設定オプションの一覧です。
一部のフィールドには値の例があり、その他はフィールドの型のみが示されています。

YAMLを使用したデータソースのプロビジョニングの詳細については、[Grafanaドキュメント](https://grafana.com/docs/grafana/latest/administration/provisioning/#data-sources)を参照してください。

```yaml theme={null}
datasources:
  - name: Example ClickHouse
    uid: clickhouse-example
    type: grafana-clickhouse-datasource
    jsonData:
      host: 127.0.0.1
      port: 9000
      protocol: native
      secure: false
      username: default
      tlsSkipVerify: <boolean>
      tlsAuth: <boolean>
      tlsAuthWithCACert: <boolean>
      defaultDatabase: default
      defaultTable: <string>
      dialTimeout: 10
      queryTimeout: 60
      validateSql: false
      httpHeaders:
      - name: X-Example-Plain-Header
        value: plain text value
        secure: false
      - name: X-Example-Secure-Header
        secure: true
      logs:
        defaultDatabase: default
        defaultTable: otel_logs
        otelEnabled: false
        otelVersion: latest
        timeColumn: <string>
        levelColumn: <string>
        messageColumn: <string>
      traces:
        defaultDatabase: default
        defaultTable: otel_traces
        otelEnabled: false
        otelVersion: latest
        traceIdColumn: <string>
        spanIdColumn: <string>
        operationNameColumn: <string>
        parentSpanIdColumn: <string>
        serviceNameColumn: <string>
        durationTimeColumn: <string>
        durationUnitColumn: <time unit>
        startTimeColumn: <string>
        tagsColumn: <string>
        serviceTagsColumn: <string>
    secureJsonData:
      tlsCACert:     <string>
      tlsClientCert: <string>
      tlsClientKey:  <string>
      secureHttpHeaders.X-Example-Secure-Header: secure header value
```
