跳到主要内容
跳到主要内容

Prometheus 协议

暴露指标

注意

如果您使用 ClickHouse Cloud,可以通过 Prometheus 集成 将指标暴露给 Prometheus。

ClickHouse 可以将自身指标暴露出来,供 Prometheus 抓取:

<prometheus>
    <port>9363</port>
    <endpoint>/metrics</endpoint>
    <metrics>true</metrics>
    <asynchronous_metrics>true</asynchronous_metrics>
    <events>true</events>
    <errors>true</errors>
    <histograms>true</histograms>
    <dimensional_metrics>true</dimensional_metrics>
</prometheus>

Section `<prometheus.handlers>` can be used to make more extended handlers.
This section is similar to [<http_handlers>](/interfaces/http) but works for prometheus protocols:

```xml
<prometheus>
    <port>9363</port>
    <handlers>
        <my_rule_1>
            <url>/metrics</url>
            <handler>
                <type>expose_metrics</type>
                <metrics>true</metrics>
                <asynchronous_metrics>true</asynchronous_metrics>
                <events>true</events>
                <errors>true</errors>
                <histograms>true</histograms>
                <dimensional_metrics>true</dimensional_metrics>
            </handler>
        </my_rule_1>
    </handlers>
</prometheus>

Settings:

NameDefaultDescription
portnone用于对外提供 metrics 协议服务的端口。
endpoint/metrics供 Prometheus 服务器抓取 metrics 的 HTTP endpoint。以 / 开头。不应与 <handlers> 部分一起使用。
url / headers / methodnone用于为请求查找匹配 handler 的过滤条件。类似于 <http_handlers> 部分中具有相同名称的字段。
metricstruesystem.metrics 表中暴露 metrics 指标。
asynchronous_metricstruesystem.asynchronous_metrics 表中暴露当前 metrics 指标值。
eventstruesystem.events 表中暴露 metrics 指标。
errorstrue暴露自上次服务器重启以来按错误码统计的错误数量。该信息也可以从 system.errors 表中获取。
histogramstruesystem.histogram_metrics 表中暴露直方图类 metrics 指标。
dimensional_metricstruesystem.dimensional_metrics 表中暴露维度化 metrics 指标。

检查(将 127.0.0.1 替换为 ClickHouse 服务器的 IP 地址或主机名):

curl 127.0.0.1:9363/metrics

远程写入协议

ClickHouse 支持 remote-write 协议。 通过该协议接收的数据会写入 TimeSeries 表中 (该表需要预先创建)。

<prometheus>
    <port>9363</port>
    <handlers>
        <my_rule_1>
            <url>/write</url>
            <handler>
                <type>remote_write</type>
                <database>db_name</database>
                <table>time_series_table</table>
            </handler>
        </my_rule_1>
    </handlers>
</prometheus>

Settings:

NameDefaultDescription
portnone用于提供 remote-write 协议服务的监听端口。
url / headers / methodnone用于为请求查找匹配处理器的过滤条件。与 <http_handlers> 部分中同名字段含义相同。
tablenone用于写入通过 remote-write 协议接收到的数据的 TimeSeries 表名。该名称也可以选择性地包含数据库名称。
databasenonetable 设置中未指定数据库名时,table 设置中指定的表所在的数据库名称。

远程读取协议

ClickHouse 支持 remote-read 协议。 数据从 TimeSeries 表中读取,并通过该协议发送。

<prometheus>
    <port>9363</port>
    <handlers>
        <my_rule_1>
            <url>/read</url>
            <handler>
                <type>remote_read</type>
                <database>db_name</database>
                <table>time_series_table</table>
            </handler>
        </my_rule_1>
    </handlers>
</prometheus>

Settings:

NameDefaultDescription
portnone用于提供 remote-read 协议服务的端口。
url / headers / methodnone用于为请求查找匹配处理器的过滤条件。类似于 <http_handlers> 部分中同名字段。
tablenone通过 remote-read 协议发送数据时读取数据的 TimeSeries 表名。该名称中也可以包含数据库名。
databasenone当在 table 设置中未指定数据库名时,用于指定该表所在数据库的名称。

多协议配置

可以在同一位置同时指定多个协议:

<prometheus>
    <port>9363</port>
    <handlers>
        <my_rule_1>
            <url>/metrics</url>
            <handler>
                <type>expose_metrics</type>
                <metrics>true</metrics>
                <asynchronous_metrics>true</asynchronous_metrics>
                <events>true</events>
                <errors>true</errors>
                <histograms>true</histograms>
                <dimensional_metrics>true</dimensional_metrics>
            </handler>
        </my_rule_1>
        <my_rule_2>
            <url>/write</url>
            <handler>
                <type>remote_write</type>
                <table>db_name.time_series_table</table>
            </handler>
        </my_rule_2>
        <my_rule_3>
            <url>/read</url>
            <handler>
                <type>remote_read</type>
                <table>db_name.time_series_table</table>
            </handler>
        </my_rule_3>
    </handlers>
</prometheus>