## 函數列表
---
一些函數有默認的參數,例如:`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中各個時間序列二階導數,使用[簡單線性回歸](https://en.wikipedia.org/wiki/Simple_linear_regression)
`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)`, 返回年份。
### <aggregation>_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): 范圍向量內每個度量指標的總體標準方差。