PromQL-Prometheus常用查询语句

一、基本操作

  • PromQL 查询出来的数值分为两种 :
    • 瞬时向量 :包含该时间序列中最新的一个样本值
    • 区间向量 :一段时间范围内的数据
# 瞬时向量 :包含该时间序列中最新的一个样本值
	http_request_total
	
# 区间向量 :一段时间范围内的数据
	# 中括号中的内容表示过滤
	http_request_total{endpoint="service"}[3m] offset 1h

	http_request_total{endpoint="service",handler="/*",instance="10.244.3.41:3000",job="prometheus-operator-grafana",method="get",namespace="monitoring",pod="prometheus-operator-grafana-7d6f684db9-ff5zh",service="prometheus-operator-grafana",statuscode="302"}

	http_request_total{endpoint="service",handler="/*",instance="10.244.3.41:3000",job="prometheus-operator-grafana",method="get",namespace="monitoring",pod="prometheus-operator-grafana-7d6f684db9-ff5zh",service="prometheus-operator-grafana",statuscode="302"}[3m]	# 3分钟之内的

	http_request_total{endpoint="service",handler="/*",instance="10.244.3.41:3000",job="prometheus-operator-grafana",method="get",namespace="monitoring",pod="prometheus-operator-grafana-7d6f684db9-ff5zh",service="prometheus-operator-grafana",statuscode="302"}[3m] offset 1h	# offset位移变量,一个小时前的,五分钟的数据
  • offset :位移变量,查看多时间之前的数据 offset 30m
  • Labelsets :过滤条件,支持正则表达式
    • 精确过滤 :http_request_total{endpoint="service"}[3m] offset 1h
    • 正则匹配 :http_request_total{endpoint=~".*service"}[3m] offset 1h
    • 取反 :http_request_total{endpoint!="service"}[3m] offset 1h
    • 取多个值 :http_request_total{handler=~"/password|/login"}[3m] offset 1h

1.1、数学运算 + 、-、*、/、%、^

  • 除法 :查看node内存总大小 ,将bytest转为Mi
    • node_memory_MemTotal_bytes / 1024 / 1024
    • node_memory_MemTotal_bytes / 1024 / 1024 > 7000

1.2、集合运算 :and 、or、unless排除

  • and :node_memory_MemTotal_bytes / 1024 / 1024 <= 786000 and node_memory_MemTotal_bytes / 1024 / 1024 == 7860.5078125

1.3、聚合操作

  • Sum() 求和 :sum(node_memory_MemTotal_bytes) / 1024 ^2
  • Min() 最小值
  • max() 最大值
  • avg() 平均值
  • stddev() 标准差
  • count() 条目数统计
  • count_values() 条目value值统计 : count_values("value", node_memory_MemTotal_bytes)
  • By 基于哪个值进行统计 :
    • sum(http_request_total) by (statuscode) # 基于状态码统计
    • sum(http_request_total) by (statuscode, handler)
  • Topk(N,) 对统计的结果获取前N个
    • topk(5, sum(http_request_total) by (statuscode, handler)) # 前5个
  • bottomk(N,) 对统计的结果获取后N个
    • bottomk(3, sum(http_request_total) by (statuscode, handler)) # 后3个
  • Quantile(#, ) 取当前数据的中位数:
sum (求和)
min (最小值)
max (最大值)
avg (平均值)
stddev (标准差)
stdvar (标准方差)
count (计数)
count_values (对value进行计数)
bottomk (后n条时序)
topk (前n条时序)
quantile (分位数)

二、常用函数

posted @ 2021-05-24 18:16  SRE运维充电站  阅读(2380)  评论(0编辑  收藏  举报