Prometheus PromQL 基礎


時序 4 種類型

Prometheus 時序數據分為 Counter, Gauge, Histogram, Summary 四種類型。

Counter

Counter 表示收集的數據是按照某個趨勢(增加/減少)一直變化的,我們往往用它記錄服務請求總量、錯誤總數等。
例如 Prometheus serverhttp_requests_total, 表示 Prometheus 處理的 http 請求總數,我們可以使用 delta, 很容易得到任意區間數據的增量,這個會在 PromQL 一節中細講。

Gauge

Gauge 表示搜集的數據是一個瞬時的值,與時間沒有關系,可以任意變高變低,往往可以用來記錄內存使用率磁盤使用率等。

例如 Prometheus servergo_goroutines, 表示 Prometheus 當前 goroutines 的數量。

Histogram

Histogram 由 <basename>_bucket{le="<upper inclusive bound>"}<basename>_bucket{le="+Inf"}, <basename>_sum<basename>_count 組成,主要用於表示一段時間范圍內對數據進行采樣(通常是請求持續時間或響應大小),並能夠對其指定區間以及總數進行統計,通常它采集的數據展示為直方圖。
例如 Prometheus server 中 prometheus_local_storage_series_chunks_persisted, 表示 Prometheus 中每個時序需要存儲的 chunks 數量,我們可以用它計算待持久化的數據的分位數。

Summary

Summary 和 Histogram 類似,由 <basename>{quantile="<φ>"}<basename>_sum<basename>_count 組成,主要用於表示一段時間內數據采樣結果(通常是請求持續時間或響應大小),它直接存儲了 quantile 數據,而不是根據統計區間計算出來的。
例如 Prometheus server 中 prometheus_target_interval_length_seconds

Histogram vs Summary

  • 都包含 <basename>_sum<basename>_count
  • Histogram 需要通過 <basename>_bucket 計算 quantile, 而 Summary 直接存儲了 quantile 的值。

操作符

Prometheus 查詢語句中,支持常見的各種表達式操作符,例如

算術運算符:

支持的算術運算符有 +,-,*,/,%,^, 例如 http_requests_total * 2 表示將 http_requests_total 所有數據 double 一倍。

比較運算符:

支持的比較運算符有 ==,!=,>,<,>=,<=, 例如 http_requests_total > 100 表示 http_requests_total 結果中大於 100 的數據。

邏輯運算符:

支持的邏輯運算符有 and,or,unless, 例如 http_requests_total == 5 or http_requests_total == 2 表示 http_requests_total 結果中等於 5 或者 2 的數據。

聚合運算符:

支持的聚合運算符有 sum,min,max,avg,stddev,stdvar,count,count_values,bottomk,topk,quantile, 例如 max(http_requests_total) 表示 http_requests_total 結果中最大的數據。

注意,和四則運算類型,Prometheus 的運算符也有優先級,它們遵從(^)> (*, /, %) > (+, -) > (==, !=, <=, <, >=, >) > (and, unless) > (or) 的原則。


免責聲明!

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



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