Skip to content

Prometheus

GreptimeDB 可以作为 Prometheus 的长期存储。使用 GreptimeDB 作为 Prometheus 的后端存储可以获得无缝体验。由于 Prometheus 支持在配置远程写和读的过程中设置基本的认证信息,你只需要把配置的用户名和密码添加到配置 YAML 文件中就可以了。

请按照 Prometheus configuration (prometheus.yml) 中的设置进行配置:

yaml
remote_write:
- url: http://localhost:4000/v1/prometheus/write?db=public&physical_table=greptime_physical_table
#  basic_auth:
#    username: greptime_user
#    password: greptime_pwd

remote_read:
- url: http://localhost:4000/v1/prometheus/read?db=public
#  basic_auth:
#    username: greptime_user
#    password: greptime_pwd
remote_write:
- url: http://localhost:4000/v1/prometheus/write?db=public&physical_table=greptime_physical_table
#  basic_auth:
#    username: greptime_user
#    password: greptime_pwd

remote_read:
- url: http://localhost:4000/v1/prometheus/read?db=public
#  basic_auth:
#    username: greptime_user
#    password: greptime_pwd

注意

请将 greptime_user(username), greptime_pwd(password) 替换为用户自己的用户名和密码,详情请参考客户端鉴权认证

URL 中的 db 参数表示我们要写入的数据库,是可选的,默认为 public。 如果你想要写入到其他数据库,可以创建新数据库并将 public 替换为新的数据库名称。

GreptimeDB 将多个 Prometheus 指标自动组合到相应的逻辑表中,因此你无需在 remote_write 的 URL 中指定逻辑表。

URL 中的 physical_table 参数可选,表示数据被写入的物理表, 它可以减少小表的存储开销,提高列压缩效率。 在不指定该参数的情况下默认使用 greptime_physical_table 表。如果指定的物理表不存在,该表会被自动创建。

在配置文件中启用 with_metric_engine 时,physical_table 参数才会生效。 该配置默认启用。

下面是 URL 参数的表格:

参数是否必须默认值
db可选public
physical_table可选greptime_physical_table

写入数据成功后,使用下面的命令展示数据库中的表:

sql
show tables;
show tables;
sql
+---------------------------------------------------------------+
| Tables                                                        |
+---------------------------------------------------------------+
| go_memstats_heap_inuse_bytes                                  |
| go_memstats_last_gc_time_seconds                              |
| net_conntrack_listener_conn_closed_total                      |
| prometheus_remote_storage_enqueue_retries_total               |
| prometheus_remote_storage_exemplars_pending                   |
| prometheus_remote_storage_read_request_duration_seconds_count |
| prometheus_rule_group_duration_seconds                        |
| prometheus_rule_group_duration_seconds_count                  |
| ......                                                        |
+---------------------------------------------------------------+
+---------------------------------------------------------------+
| Tables                                                        |
+---------------------------------------------------------------+
| go_memstats_heap_inuse_bytes                                  |
| go_memstats_last_gc_time_seconds                              |
| net_conntrack_listener_conn_closed_total                      |
| prometheus_remote_storage_enqueue_retries_total               |
| prometheus_remote_storage_exemplars_pending                   |
| prometheus_remote_storage_read_request_duration_seconds_count |
| prometheus_rule_group_duration_seconds                        |
| prometheus_rule_group_duration_seconds_count                  |
| ......                                                        |
+---------------------------------------------------------------+

GreptimeDB 中的 Prometheus 指标

当指标被远程写入 GreptimeDB 时,它们将被转换为下面这样:

Sample MetricsIn GreptimeDBGreptimeDB Data Types
NameTable (Auto-created) NameString
ValueColumn (greptime_value)Double
TimestampColumn (greptime_timestamp)Timestamp
LabelColumnString

所有标签列将会被自动创建为主键。当添加新的标签时,它也会自动添加到主键中。

举例: GreptimeDB 表中的 Prometheus 指标

txt
prometheus_remote_storage_samples_total{instance="localhost:9090", job="prometheus",
remote_name="648f0c", url="http://localhost:4000/v1/prometheus/write"} 500
prometheus_remote_storage_samples_total{instance="localhost:9090", job="prometheus",
remote_name="648f0c", url="http://localhost:4000/v1/prometheus/write"} 500

上面这个例子将被转化为表 prometheus_remote_storage_samples_total 中的一行:

ColumnValueColumn Data Type
instancelocalhost:9090String
jobprometheusString
remote_name648f0cString
urlhttp://localhost:4000/v1/prometheus/writeString
greptime_value500Double
greptime_timestampThe sample's unix timestampTimestamp

VictoriaMetrics remote write 协议支持

VictoriaMetrics 微调了 Prometheus remote write 协议,通过 zstd 压缩试图获取更好 的压缩率和更低资源消耗。当你配置了支持该协议的后端时,vmagent 工具会自动切换到 VictoriaMetrics remote write 协议。

GreptimeDB 也支持了这个变种协议。用户只需要将 GreptimeDB 的 remote write 路径配 置给 vmagent 就可以达到效果。例如,对本地安装的 GreptimeDB 来说:

shell
vmagent -remoteWrite.url=http://localhost:4000/v1/prometheus/write
vmagent -remoteWrite.url=http://localhost:4000/v1/prometheus/write