Prometheus 是一个时序型数据库, 广泛用于监控及报警.
1. 术语
-
metric: 度量指标, 由一个名字加若干个label组成. 比如
http_request_total{method="POST", path="/api/login"}, 该metric名字为http_request_total, 有两个label:method和path. -
metric类型:
-
Counter: 一种累加的计数器.
比如 请求数 . -
Gauge: 一个瞬时的数值, 比如 内存使用率 .
-
Histogram(直方图):
-
将数据按照一定的区间分组查看.
-
数据累计求和.
-
数据量计数.
-
-
Summary(数据摘要):
-
数值百分位查看.
-
数据累计求和.
-
数据量计数.
-
-
-
instance: 一个暴露metrics端点的进程.
-
job: 同一类别的instance. 比如启动4个api服务, 每个服务都是一个instance, 这4个服务同属于一个
apijob.
2. 安装
version: '3'
services:
prometheus:
image: prom/prometheus
container_name: prometheus
volumes:
- /data/softs/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml
- /data/softs/prometheus/:/prometheus/
ports:
- 19000:9090
networks:
- promethus
networks:
promethus:
3. PromQL
用于 Prometheus 数据查询的DSL语言.
3.1. 查询类型
-
瞬时Selector
Instant vector: 查询一组时序数据, 比如http_requests_total. -
区间Selector
Range vector:-
查询过去一段时间内的时序数据, 比如
http_requests_total[5m]查询过去5分钟内的数据. -
查询一段时间前的数据, 比如
http_requests_total offset 1w查询过去1星期前的数据.
-
-
表量Selector
Scalar: 一个瞬时数值.
3.2. 操作符
3.2.1. 算术类运算符
-
+ -
- -
* -
/ -
% -
^
3.2.2. 比较类运算符
-
== -
!= -
> -
< -
>= -
<=
3.2.4. 聚合操作符
-
sum -
min -
max -
avg -
stddev -
stdvar -
count -
count_values -
bottomk -
topk -
quantile
3.3. 向量匹配
method_code:http_errors:rate5m{method="get", code="500"} 24
method_code:http_errors:rate5m{method="get", code="404"} 30
method_code:http_errors:rate5m{method="put", code="501"} 3
method_code:http_errors:rate5m{method="post", code="500"} 6
method_code:http_errors:rate5m{method="post", code="404"} 21
method:http_requests:rate5m{method="get"} 600
method:http_requests:rate5m{method="del"} 34
method:http_requests:rate5m{method="post"} 120
3.3.1. one-to-one
匹配 label 相同的向量, 可以使用 ignoring 忽略不参与匹配的 label , 或使用 on 指定参与匹配的 label .
-
method_code:http_errors:rate5m{code="500"} / ignoring(code) method:http_requests:rate5m: 忽略code, 只匹配method.-
{method="get"} 0.04 // 24 / 600 -
{method="post"} 0.05 // 6 / 120
-
-
method_code:http_errors:rate5m{code="500"} / on(method) method:http_requests:rate5m: 只匹配method.
3.4. one-to-many
使用 group_left/group_right 指明哪方元素多.
-
method_code:http_errors:rate5m / on(method) group_left method:http_requests:rate5m-
{method="get", code="500"} 0.04 // 24 / 600 -
{method="get", code="404"} 0.05 // 30 / 600 -
{method="post", code="500"} 0.05 // 6 / 120 -
{method="post", code="404"} 0.175 // 21 / 120
-