PromQL

PromQL 是 Prometheus 的查詢語言,用於查詢 Prometheus 的監控資料。PromQL 提供了許多內建的函數,用於查詢和處理監控資料。

你也可以使用 PromQL 來撰寫告警的條件,當 Metrics 符合告警條件時,立即發送告警通知相關人員處理。

Instant Vector 與 Range Vector

PromQL 有兩種主要的資料型態:Instant Vector 與 Range Vector。

Instant Vector 是一個時間點的資料,例如:現在機器的運行狀態。

up

Range Vector 是一段時間內的資料,例如:過去 5 分鐘的機器運行狀態。

up[5m]

資料 Label

從 Prometheus 獲取的資料可以帶上一組 Label,這些 Label 可以用來篩選資料。

up{source_cloud="Azure", region="CanadaCentral"}

如剛剛所說,你可以抓取一段時間內的資料,並且可以使用 Label 來篩選資料。

up{source_cloud="Aws", region="ap-northeast-1"}[5m]

函式

PromQL 提供了許多內建的函式,可以用來處理資料。例如 minmaxavg 等等。

avg(up)

這個查詢會回傳所有 up 的平均值。

avg(up{source_cloud="Aws", region="ap-northeast-1"})

這個查詢會回傳 source_cloudAwsregionap-northeast-1 的所有 up 的平均值。

取得記憶體使用量

如果想在 Grafana 上觀察機器的記憶體使用量,可以使用 node exporter 來得到關於記憶體的 Metrics,PromQL 的查詢語法如下:

(
  node_memory_MemTotal_bytes
  - (
    node_memory_MemFree_bytes
    + node_memory_Buffers_bytes
    + node_memory_Cached_bytes
  )
) / node_memory_MemTotal_bytes

記憶體的使用量為 node_memory_MemFree_bytesnode_memory_Buffers_bytesnode_memory_Cached_bytes 的總和。

  • node_memory_MemTotal_bytes:總記憶體大小
  • node_memory_MemFree_bytes:可用的記憶體大小
  • node_memory_Buffers_bytes:Buffer 的大小
  • node_memory_Cached_bytes:Cache 的大小

This site uses Just the Docs, a documentation theme for Jekyll.