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 (分位數)

二、常用函數


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM