1. 程式人生 > >prometheus 之 查詢語法(函式列表)

prometheus 之 查詢語法(函式列表)

函式列表

一些函式有預設的引數,例如:year(v=vector(time()) instant-vector)。v是引數值,instant-vector是引數型別。vector(time())是預設值。

abs()

abs(v instant-vector)返回輸入向量的所有樣本的絕對值。

absent()

absent(v instant-vector),如果賦值給它的向量具有樣本資料,則返回空向量;如果傳遞的瞬時向量引數沒有樣本資料,則返回不帶度量指標名稱且帶有標籤的樣本值為1的結果

當監控度量指標時,如果獲取到的樣本資料是空的, 使用absent方法對告警是非常有用的

absent(nonexistent{job=“myjob”}) # => key: value = {job=“myjob”}: 1

absent(nonexistent{job=“myjob”, instance=~".*"}) # => {job=“myjob”} 1 so smart !

absent(sum(nonexistent{job=“myjob”})) # => key:value {}: 0

ceil()

ceil(v instant-vector) 是一個向上舍入為最接近的整數。

changes()

changes(v range-vector) 輸入一個範圍向量, 返回這個範圍向量內每個樣本資料值變化的次數。

clamp_max()

clamp_max(v instant-vector, max scalar)函式,輸入一個瞬時向量和最大值,樣本資料值若大於max,則改為max,否則不變

clamp_min()

clamp_min(v instant-vector)函式,輸入一個瞬時向量和最大值,樣本資料值小於min,則改為min。否則不變

count_saclar()

count_scalar(v instant-vector) 函式, 輸入一個瞬時向量,返回key:value=“scalar”: 樣本個數。而count()函式,輸入一個瞬時向量,返回key:value=向量:樣本個數,其中結果中的向量允許通過by條件分組。

day_of_month()

day_of_month(v=vector(time()) instant-vector)函式,返回被給定UTC時間所在月的第幾天。返回值範圍:1~31。

day_of_week()

day_of_week(v=vector(time()) instant-vector)函式,返回被給定UTC時間所在周的第幾天。返回值範圍:0~6. 0表示星期天。

days_in_month()

days_in_month(v=vector(time()) instant-vector)函式,返回當月一共有多少天。返回值範圍:28~31.

delta()

delta(v range-vector)函式,計算一個範圍向量v的第一個元素和最後一個元素之間的差值。返回值:key:value=度量指標:差值

下面這個表示式例子,返回過去兩小時的CPU溫度差:

delta(cpu_temp_celsius{host="zeus"}[2h])

delta函式返回值型別只能是gauges。

deriv()

deriv(v range-vector)函式,計算一個範圍向量v中各個時間序列二階導數,使用簡單線性迴歸

deriv二階導數返回值型別只能是gauges。

drop_common_labels()

drop_common_labels(instant-vector)函式,輸入一個瞬時向量,返回值是key:value=度量指標:樣本值,其中度量指標是去掉了具有相同標籤。 例如:http_requests_total{code=“200”, host=“127.0.0.1:9090”, method=“get”} : 4, http_requests_total{code=“200”, host=“127.0.0.1:9090”, method=“post”} : 5, 返回值: http_requests_total{method=“get”} : 4, http_requests_total{code=“200”, method=“post”} : 5

exp()

exp(v instant-vector)函式,輸入一個瞬時向量, 返回各個樣本值的e指數值,即為e^N次方。特殊情況如下所示:

Exp(+inf) = +Inf Exp(NaN) = NaN

floor()

floor(v instant-vector)函式,與ceil()函式相反。 4.3 為 4 。

histogram_quantile()

histogram_quatile(φ float, b instant-vector) 函式計算b向量的φ-直方圖 (0 ≤ φ ≤ 1) 。參考中文文獻[https://www.howtoing.com/how-to-query-prometheus-on-ubuntu-14-04-part-2/]

holt_winters()

holt_winters(v range-vector, sf scalar, tf scalar)函式基於範圍向量v,生成事件序列資料平滑值。平滑因子sf越低, 對老資料越重要。趨勢因子tf越高,越多的資料趨勢應該被重視。0< sf, tf <=1。 holt_winters僅用於gauges

hour()

hour(v=vector(time()) instant-vector)函式返回被給定UTC時間的當前第幾個小時,時間範圍:0~23。

idelta()

idelta(v range-vector)函式,輸入一個範圍向量,返回key: value = 度量指標: 每最後兩個樣本值差值。

increase()

increase(v range-vector)函式, 輸入一個範圍向量,返回:key:value = 度量指標:last值-first值,自動調整單調性,如:服務例項重啟,則計數器重置。與delta()不同之處在於delta是求差值,而increase返回最後一個減第一個值,可為正為負。

下面的表示式例子,返回過去5分鐘,連續兩個時間序列資料樣本值的http請求增加值。

increase(http_requests_total{job="api-server"}[5m])

increase的返回值型別只能是counters,主要作用是增加圖表和資料的可讀性,使用rate記錄規則的使用率,以便持續跟蹤資料樣本值的變化。

irate

irate(v range-vector)函式, 輸入:範圍向量,輸出:key: value = 度量指標: (last值-last前一個值)/時間戳差值。它是基於最後兩個資料點,自動調整單調性, 如:服務例項重啟,則計數器重置。

下面表示式針對範圍向量中的每個時間序列資料,返回兩個最新資料點過去5分鐘的HTTP請求速率。

irate(http_requests_total{job="api-server"}[5m])

irate只能用於繪製快速移動的計數器。因為速率的簡單更改可以重置FOR子句,利用警報和緩慢移動的計數器,完全由罕見的尖峰組成的圖形很難閱讀。

label_replace()

對於v中的每個時間序列,label_replace(v instant-vector, dst_label string, replacement string, src_label string, regex string) 將正則表示式與標籤值src_label匹配。如果匹配,則返回時間序列,標籤值dst_label被替換的擴充套件替換。$1替換為第一個匹配子組,$2替換為第二個等。如果正則表示式不匹配,則時間序列不會更改。

另一種更容易的理解是:label_replace函式,輸入:瞬時向量,輸出:key: value = 度量指標: 值(要替換的內容:首先,針對src_label標籤,對該標籤值進行regex正則表示式匹配。如果不能匹配的度量指標,則不發生任何改變;否則,如果匹配,則把dst_label標籤的標籤紙替換為replacement 下面這個例子返回一個向量值a帶有foo標籤: label_replace(up{job=“api-server”, serice=“a:c”}, “foo”, “$1”, “service”, “(.):.”)

ln()

ln(v instance-vector)計算瞬時向量v中所有樣本資料的自然對數。特殊例子:

ln(+Inf) = +Inf ln(0) = -Inf ln(x<0) = NaN ln(NaN) = NaN

log2()

log2(v instant-vector)函式計算瞬時向量v中所有樣本資料的二進位制對數。

log10()

log10(v instant-vector)函式計算瞬時向量v中所有樣本資料的10進位制對數。相當於ln()

minute()

minute(v=vector(time()) instant-vector)函式返回給定UTC時間當前小時的第多少分鐘。結果範圍:0~59。

month()

month(v=vector(time()) instant-vector)函式返回給定UTC時間當前屬於第幾個月,結果範圍:0~12。

predict_linear()

predict_linear(v range-vector, t scalar)預測函式,輸入:範圍向量和從現在起t秒後,輸出:不帶有度量指標,只有標籤列表的結果值。

例如:

predict_linear(http_requests_total{code="200",instance="120.77.65.193:9090",job="prometheus",method="get"}[5m], 5)

結果:

{code=“200”,handler=“query_range”,instance=“120.77.65.193:9090”,job=“prometheus”,method=“get”} 1
{code=“200”,handler=“prometheus”,instance=“120.77.65.193:9090”,job=“prometheus”,method=“get”} 4283.449995397104
{code=“200”,handler=“static”,instance=“120.77.65.193:9090”,job=“prometheus”,method=“get”} 22.99999999999999
{code=“200”,handler=“query”,instance=“120.77.65.193:9090”,job=“prometheus”,method=“get”} 130.90381188596754
{code=“200”,handler=“graph”,instance=“120.77.65.193:9090”,job=“prometheus”,method=“get”} 2
{code=“200”,handler=“label_values”,instance=“120.77.65.193:9090”,job=“prometheus”,method=“get”} 2

rate()

rate(v range-vector)函式, 輸入:範圍向量,輸出:key: value = 不帶有度量指標,且只有標籤列表:(last值-first值)/時間差s

rate(http_requests_total[5m])

結果:

{code=“200”,handler=“label_values”,instance=“120.77.65.193:9090”,job=“prometheus”,method=“get”} 0
{code=“200”,handler=“query_range”,instance=“120.77.65.193:9090”,job=“prometheus”,method=“get”} 0
{code=“200”,handler=“prometheus”,instance=“120.77.65.193:9090”,job=“prometheus”,method=“get”} 0.2
{code=“200”,handler=“query”,instance=“120.77.65.193:9090”,job=“prometheus”,method=“get”} 0.003389830508474576
{code=“422”,handler=“query”,instance=“120.77.65.193:9090”,job=“prometheus”,method=“get”} 0
{code=“200”,handler=“static”,instance=“120.77.65.193:9090”,job=“prometheus”,method=“get”} 0
{code=“200”,handler=“graph”,instance=“120.77.65.193:9090”,job=“prometheus”,method=“get”} 0
{code=“400”,handler=“query”,instance=“120.77.65.193:9090”,job=“prometheus”,method=“get”} 0
rate()函式返回值型別只能用counters, 當用圖表顯示增長緩慢的樣本資料時,這個函式是非常合適的。

注意:當rate函式和聚合方式聯合使用時,一般先使用rate函式,再使用聚合操作, 否則,當服務例項重啟後,rate無法檢測到counter重置。

resets()

resets()函式, 輸入:一個範圍向量,輸出:key-value=沒有度量指標,且有標籤列表[在這個範圍向量中每個度量指標被重置的次數]。在兩個連續樣本資料值下降,也可以理解為counter被重置。 示例:

resets(http_requests_total[5m])

結果:
{code=“200”,handler=“label_values”,instance=“120.77.65.193:9090”,job=“prometheus”,method=“get”} 0
{code=“200”,handler=“query_range”,instance=“120.77.65.193:9090”,job=“prometheus”,method=“get”} 0
{code=“200”,handler=“prometheus”,instance=“120.77.65.193:9090”,job=“prometheus”,method=“get”} 0
{code=“200”,handler=“query”,instance=“120.77.65.193:9090”,job=“prometheus”,method=“get”} 0
{code=“422”,handler=“query”,instance=“120.77.65.193:9090”,job=“prometheus”,method=“get”} 0
{code=“200”,handler=“static”,instance=“120.77.65.193:9090”,job=“prometheus”,method=“get”} 0
{code=“200”,handler=“graph”,instance=“120.77.65.193:9090”,job=“prometheus”,method=“get”} 0
{code=“400”,handler=“query”,instance=“120.77.65.193:9090”,job=“prometheus”,method=“get”} 0
resets只能和counters一起使用。

round()

round(v instant-vector, to_nearest 1= scalar)函式,與ceil和floor函式類似,輸入:瞬時向量,輸出:指定整數級的四捨五入值, 如果不指定,則是1以內的四捨五入。

scalar()

scalar(v instant-vector)函式, 輸入:瞬時向量,輸出:key: value = “scalar”, 樣本值[如果度量指標樣本數量大於1或者等於0, 則樣本值為NaN, 否則,樣本值本身]

sort()

sort(v instant-vector)函式,輸入:瞬時向量,輸出:key: value = 度量指標:樣本值[升序排列]

sort_desc()

sort(v instant-vector函式,輸入:瞬時向量,輸出:key: value = 度量指標:樣本值[降序排列]

sqrt()

sqrt(v instant-vector)函式,輸入:瞬時向量,輸出:key: value = 度量指標: 樣本值的平方根

time()

time()函式,返回從1970-01-01到現在的秒數,注意:它不是直接返回當前時間,而是時間戳

vector()

vector(s scalar)函式,返回:key: value= {}, 傳入引數值

year()

year(v=vector(time()) instant-vector), 返回年份。

_over_time()

下面的函式列表允許傳入一個範圍向量,返回一個帶有聚合的瞬時向量:
avg_over_time(range-vector): 範圍向量內每個度量指標的平均值。
min_over_time(range-vector): 範圍向量內每個度量指標的最小值。
max_over_time(range-vector): 範圍向量內每個度量指標的最大值。
sum_over_time(range-vector): 範圍向量內每個度量指標的求和值。
count_over_time(range-vector): 範圍向量內每個度量指標的樣本資料個數。
quantile_over_time(scalar, range-vector): 範圍向量內每個度量指標的樣本資料值分位數,φ-quantile (0 ≤ φ ≤ 1)
stddev_over_time(range-vector): 範圍向量內每個度量指標的總體標準偏差。
stdvar_over_time(range-vector): 範圍向量內每個度量指標的總體標準方差。

轉自:https://github.com/1046102779/prometheus/blob/master/prometheus/querying/functions.md