Prometheus Query Language
GreptimeDB 可以作为 Grafana 中 Prometheus 的替代品,因为 GreptimeDB 支持 PromQL(Prometheus Query Language)。GreptimeDB 在 Rust 中重新实现了 PromQL,并通过接口将能力开放,包括 Prometheus 的 HTTP API、GreptimeDB 的 HTTP API 和 SQL 接口。
Prometheus 的 HTTP API
GreptimeDB 实现了兼容 Prometheus 的一系列 API,通过 /v1/prometheus 路径对外提
供服务:
- Instant queries
/api/v1/query - Range queries
/api/v1/query_range - Series
/api/v1/series - Label names
/api/v1/labels - Label values
/api/v1/label/<label_name>/values
这些接口的输入和输出与原生的 Prometheus HTTP API 相同,用户可以把 GreptimeDB 当
作 Prometheus 的直接替换。例如,在 Grafana 中我们可以设置
http://localhost:4000/v1/prometheus/ 作为其 Prometheus 数据源的地址。
访问 Prometheus 文档 获得更详细的说明。
你可以通过设置 HTTP 请求的 db 参数来指定 GreptimeDB 中的数据库名。
例如,以下查询将返回 public 数据库中 process_cpu_seconds_total 指标的 CPU 使用率:
curl -X POST \
-H 'Authorization: Basic <base64-encoded-credentials>' \
--data-urlencode 'query=irate(process_cpu_seconds_total[1h])' \
--data-urlencode 'start=2024-11-24T00:00:00Z' \
--data-urlencode 'end=2024-11-25T00:00:00Z' \
--data-urlencode 'step=1h' \
'http://localhost:4000/v1/prometheus/api/v1/query_range?db=public'
如果你使用启用了身份验证的 GreptimeDB,则需要 Authorization header,请参阅鉴权。
该 API 的查询字符串参数与原始 Prometheus API 的查询字符串参数相同。
你需要在 HTTP 的 query param 设置 db 参数,或者通过 --header 'x-greptime-db-name: <database name>' 设置在 HTTP 请求头中。
输出格式与 Prometheus API 完全兼容:
{
"status": "success",
"data": {
"resultType": "matrix",
"result": [
{
"metric": {
"job": "node",
"instance": "node_exporter:9100",
"__name__": "process_cpu_seconds_total"
},
"values": [
[
1732618800,
"0.0022222222222222734"
],
[
1732622400,
"0.0009999999999999788"
],
[
1732626000,
"0.0029999999999997585"
],
[
1732629600,
"0.002222222222222175"
]
]
}
]
}
}