Skip to content

Prometheus

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

首先要为存储 Prometheus 的数据创建一个单独的数据库:

sql
CREATE DATABASE prometheus;
CREATE DATABASE prometheus;

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

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

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

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

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

url 中的 db 参数表示我们要写入的数据库,如果没有指定,则默认为 public

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

sql
use prometheus;
show tables;
use prometheus;
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