跳到主要内容
跳到主要内容
编辑此页

在 Grafana 中配置 ClickHouse 数据源

修改配置最简单的方法是在 Grafana UI 的插件配置页面上进行,但数据源也可以使用 YAML 文件进行配置

此页面显示了 ClickHouse 插件中可用的配置选项列表,以及为使用 YAML 配置数据源的用户提供的配置代码片段。

如需快速了解所有选项,请点击此处查看完整的配置选项列表。

常用设置

配置示例屏幕

Example secure native config

常用设置的配置 YAML 示例

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 属性。这显示了保存配置时使用的插件版本。

HTTP 协议

如果您选择通过 HTTP 协议连接,将显示更多设置。

Extra HTTP config options

HTTP 路径

如果您的 HTTP 服务器在不同的 URL 路径下公开,您可以在此处添加。

jsonData:
# excludes first slash
path: additional/path/example

自定义 HTTP 标头

您可以向发送到服务器的请求添加自定义标头。

标头可以是纯文本或安全文本。所有标头键都以纯文本形式存储,而安全标头值则保存在安全配置中(类似于 password 字段)。

通过 HTTP 的安全值

虽然安全标头值安全地存储在配置中,但如果禁用安全连接,该值仍将通过 HTTP 发送。

纯文本/安全标头的 YAML 示例

jsonData:
httpHeaders:
- name: X-Example-Plain-Header
value: plain text value
secure: false
- name: X-Example-Secure-Header
# "value" is excluded
secure: true
secureJsonData:
secureHttpHeaders.X-Example-Secure-Header: secure header value

附加设置

这些附加设置是可选的。

Example additional settings

YAML 示例

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.

OpenTelemetry

OpenTelemetry (OTel) 与插件深度集成。OpenTelemetry 数据可以使用我们的 导出器插件导出到 ClickHouse。为了获得最佳使用效果,建议为日志追踪都配置 OTel。

还需要配置这些默认值以启用数据链接,此功能可实现强大的可观测性工作流程。

日志

为了加速日志的查询构建,您可以设置默认的数据库/表以及日志查询的列。这将预加载查询构建器,使其包含可运行的日志查询,从而加快在探索页面上浏览以进行可观测性的速度。

如果您正在使用 OpenTelemetry,则应启用“使用 OTel”开关,并将默认日志表设置为 otel_logs。这将自动覆盖默认列以使用选定的 OTel 模式版本。

虽然日志不需要 OpenTelemetry,但使用单个日志/追踪数据集有助于通过数据链接实现更流畅的可观测性工作流程。

日志配置示例屏幕

Logs config

日志配置 YAML 示例

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.

追踪

为了加速追踪的查询构建,您可以设置默认的数据库/表以及追踪查询的列。这将预加载查询构建器,使其包含可运行的追踪搜索查询,从而加快在探索页面上浏览以进行可观测性的速度。

如果您正在使用 OpenTelemetry,则应启用“使用 OTel”开关,并将默认追踪表设置为 otel_traces。这将自动覆盖默认列以使用选定的 OTel 模式版本。虽然追踪不需要 OpenTelemetry,但在为追踪使用其模式时,此功能效果最佳。

追踪配置示例屏幕

Traces config

追踪配置 YAML 示例

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.

列别名

列别名是一种使用不同名称和类型查询数据的便捷方式。通过别名,您可以获取嵌套模式并将其展平,以便可以在 Grafana 中轻松选择。

如果您属于以下情况,别名可能与您相关:

  • 您了解您的模式及其大多数嵌套属性/类型
  • 您将数据存储在 Map 类型中
  • 您将 JSON 存储为字符串
  • 您经常应用函数来转换您选择的列

表定义的 ALIAS 列

ClickHouse 内置了列别名功能,并且可以与 Grafana 开箱即用。别名列可以直接在表上定义。

CREATE TABLE alias_example (
TimestampNanos DateTime(9),
TimestampDate ALIAS toDate(TimestampNanos)
)

在上面的示例中,我们创建了一个名为 TimestampDate 的别名,它将纳秒时间戳转换为 Date 类型。此数据不像第一列那样存储在磁盘上,而是在查询时计算的。表定义的别名不会通过 SELECT * 返回,但这可以在服务器设置中配置。

有关更多信息,请阅读 ALIAS 列类型的文档。

列别名表

默认情况下,Grafana 将根据 DESC table 的响应提供列建议。在某些情况下,您可能希望完全覆盖 Grafana 看到的列。这有助于在 Grafana 中选择列时模糊化您的模式,从而根据您表的复杂性改善用户体验。

相比于表定义的别名,这样做的好处是您可以轻松更新它们,而无需更改您的表。在某些模式中,这可能有数千个条目长,这可能会使底层表定义变得混乱。它还允许隐藏您希望用户忽略的列。

Grafana 要求别名表具有以下列结构

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 列的行为

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 中使用。请注意,名称可以是任何名称,甚至可以在单独的数据库中定义

Example alias table config

现在 Grafana 将看到别名表的结果,而不是 DESC example_table 的结果

Example alias table select

两种类型的别名都可以用于执行复杂的类型转换或 JSON 字段提取。

所有 YAML 选项

这些是插件提供的所有 YAML 配置选项。某些字段具有示例值,而其他字段仅显示字段的类型。

有关使用 YAML 配置数据源的更多信息,请参阅 Grafana 文档

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