Prometheus支持在警報的註釋和標籤以及服務的控制檯頁面(如/consoles/index.html.example)中進行模板化。模板具有對本地數據庫運行查詢,遍歷數據,使用條件,格式化數據等功能。Prometheus模板語言基於Go模板系統。
數據結構
處理時間序列數據的主要數據結構是sample,其定義爲:
type sample struct {
Labels map[string]string
Value float64
}
sample的指標名稱被編碼在labels map的特定標籤__name__中。
[]sample
表示sample列表。
Go中interface{}
類似於C中的void指針。
功能
除了Go模板提供的默認功能外,Prometheus還提供了一些功能,可簡化模板中查詢結果的處理。
如果在管道中使用函數,則管道值將作爲最後一個參數傳遞。
Queries
Name | Arguments | Returns | Notes |
---|---|---|---|
query | query string | []sample | 查詢數據庫,不支持返回範圍向量。 |
first | []sample | sample | 相當於 index a 0 |
label | label, sample | string | 相當於 index sample.Labels label |
value | sample | float64 | 相當於 sample.Value |
sortByLabel | label, []samples | []sample | 按給定標籤對samples進行排序,這個功能很穩定。 |
first
,label
以及value
旨在使查詢結果中的管道易於使用。
Numbers
Name | Arguments | Returns | Notes |
---|---|---|---|
humanize | number | string | 使用指標前綴將數字轉換爲更易讀的格式。 |
humanize1024 | number | string | 類似於humanize ,但是使用1024作爲基礎,而不是1000。 |
humanizeDuration | number | string | 將持續時間(以秒爲單位)轉換爲更具可讀性的格式。 |
humanizePercentage | number | string | 將比率值轉換爲100的分數。 |
humanizeTimestamp | number | string | 將以秒爲單位的Unix時間戳轉換爲更具可讀性的格式。 |
人性化功能旨在產生合理的輸出以供人類使用,並且不保證在Prometheus各版本之間返回相同的結果。
############
Strings
Name | Arguments | Returns | Notes |
---|---|---|---|
title | string | string | strings.Title, capitalises first character of each word. |
toUpper | string | string | strings.ToUpper, converts all characters to upper case. |
toLower | string | string | strings.ToLower, converts all characters to lower case. |
match | pattern, text | boolean | regexp.MatchString Tests for a unanchored regexp match. |
reReplaceAll | pattern, replacement, text | string | Regexp.ReplaceAllString Regexp substitution, unanchored. |
graphLink | expr | string | Returns path to graph view in the expression browser for the expression. |
tableLink | expr | string | Returns path to tabular ("Console") view in the expression browser for the expression. |
Others
Name | Arguments | Returns | Notes |
---|---|---|---|
args | []interface{} | map[string]interface{} | This converts a list of objects to a map with keys arg0, arg1 etc. This is intended to allow multiple arguments to be passed to templates. |
tmpl | string, []interface{} | nothing | Like the built-in template , but allows non-literals as the template name. Note that the result is assumed to be safe, and will not be auto-escaped. Only available in consoles. |
safeHtml | string | string | Marks string as HTML not requiring auto-escaping. |
模板類型差異
每種類型的模板都提供了可用於參數化模板的不同信息,並且還有其他一些區別。
警報字段模板
.Value
,.Labels
和分別ExternalLabels
包含警報值,警報標籤和全局配置的外部標籤。爲了方便起見$value
,它們也顯示爲$labels
,和$externalLabels
變量。
控制檯模板
控制檯在上公開/consoles/
,並從-web.console.templates
標誌所指向的目錄中獲取。
控制檯模板使用html / template呈現 ,該模板提供自動轉義。要繞過自動轉義,請使用safe*
功能。
URL參數可在中作爲映射使用.Params
。要訪問具有相同名稱的多個URL參數,.RawParams
是每個參數的列表值的映射。URL路徑可在中使用.Path
,但/consoles/
前綴除外。全局配置的外部標籤爲 .ExternalLabels
。還有所有四個慣用變量: $rawParams
,$params
,$path
,和$externalLabels
。
控制檯還可以訪問在該標誌所指向的目錄{{define "templateName"}}...{{end}}
中的*.lib
文件中找到的所有定義的模板-web.console.libraries
。由於這是一個共享的名稱空間,請注意避免與其他用戶衝突。模板名稱以prom
,_prom
和開頭, __
保留給Prometheus使用,上面列出的功能也是如此。