Metrics
官方解釋是
Metrics are numerical measurements in layperson terms. (通俗地講柠新,Metrics就是數(shù)字測(cè)量)
Prometheus fundamentally stores all data as time series(Prometheus把所有數(shù)據(jù)都存儲(chǔ)為時(shí)間序列)
Every time series is uniquely identified by its metric name and optional key-value pairs called labels.(每個(gè)時(shí)間序列都由metric name和可選的鍵值對(duì)Metric labels作為唯一標(biāo)識(shí))
Samples form the actual time series data. Each sample consists of: a float64 value, a millisecond-precision timestamp(樣本組成了實(shí)際的時(shí)序數(shù)據(jù),每個(gè)樣本由一個(gè)float64的值和一個(gè)毫秒精度的時(shí)間戳組成)
<metric name>{<label name>=<label value>, ...} value@timestamp
# 例子:
node_load15{instance="192.168.243.99:9100", job="node_exporter"} 0.05 @1727773666.825
- node_load15是metric name,意思是機(jī)器15分鐘內(nèi)的平均負(fù)載(Linux top命令上面的3個(gè)load average之一)
- 這個(gè)metric有2個(gè)標(biāo)簽笛粘,instance標(biāo)明了這條數(shù)據(jù)來自哪臺(tái)機(jī)器,job就是Prometheus配置文件里面scrape_configs.job_name外莲,這2個(gè)標(biāo)簽是Prometheus自動(dòng)給加上的
- 整個(gè)例子的意思是队贱,192.168.243.99這臺(tái)機(jī)器在時(shí)間戳1727773666.825這一刻的15分鐘平均負(fù)載是0.05
Metric types
Prometheus有4種Metric types:
- Counter —— 計(jì)數(shù)器,數(shù)據(jù)只增不減
- Gauge —— 儀表盤住拭,數(shù)據(jù)可增可減
- Summary —— 摘要類型挪略,用于分析數(shù)據(jù)分布情況
- Histogram —— 直方圖類型,用于分析數(shù)據(jù)分布情況
訪問 node_exporter_IP:9100/metrics 就能看見其所有的Metric滔岳,比如:
每一組metrics都由# HELP 和 # TYPE開頭杠娱。HELP是這段數(shù)據(jù)的說明,TYPE是這段數(shù)據(jù)的類型
PromQL
PromQL(Prometheus Query Language)是Prometheus tsdb的查詢語言谱煤,可實(shí)時(shí)查找/計(jì)算/聚合時(shí)間序列數(shù)據(jù)摊求,并將結(jié)果在圖表中展示。
語法:<metric name>{<label name>=<label value>, ...}[duration]
- 瞬時(shí)向量刘离,表示時(shí)間序列中最新的一個(gè)樣本值睹簇,(上圖第一個(gè)示例奏赘,返回值只有1個(gè))
- 區(qū)間向量,表示給定時(shí)間范圍內(nèi)的一組樣本(上圖第二個(gè)示例太惠,返回值有多個(gè))磨淌,時(shí)間范圍可以使用 秒(s)、分鐘(m)凿渊、小時(shí)(h)梁只、天(d)、周(w)和年(y)埃脏。比如[1m]就是返回1分鐘內(nèi)的監(jiān)控?cái)?shù)據(jù)
標(biāo)簽的篩選
支持精確匹配和正則匹配搪锣,以及它們的取反操作
精確匹配
= , !=
例子:查詢node_cpu_guest_seconds_total
CPU等于1 和 不等于1的數(shù)據(jù)
正則匹配
=~ , !~
例子:查詢 node_disk_io_time_seconds_total
中device是dm-數(shù)字 的數(shù)據(jù)和不是dm-數(shù)字 的數(shù)據(jù)
算數(shù)運(yùn)算
支持 \+ 加, - 減, * 乘, / 除, % 取模, ^ 冪
運(yùn)算
例 獲取可用內(nèi)存:
比較運(yùn)算
== , != , > , < , >= , <=
例:node_load15 > 0.9
邏輯運(yùn)算
and 且彩掐, or 或, unless 非
例:找出近15分鐘負(fù)載大于0.9且內(nèi)存使用率大于80%的機(jī)器:
node_load15 > 0.9 and (1 - ((node_memory_MemFree_bytes + node_memory_Buffers_bytes + node_memory_Cached_bytes) / node_memory_MemTotal_bytes)) > 0.8
聚合 操作
sum 构舟、max、min堵幽、avg狗超、count 、count_values朴下、topk(樣本值前K個(gè)最大值)努咐、stddev(標(biāo)準(zhǔn)差)、stdvar(方差), bottomk(樣本值后K個(gè)最小值), quantile(統(tǒng)計(jì)分位數(shù))
例: