TEMPLATE REFERENCE

Prometheus supports templating in the annotations and labels of alerts, as well as in served console pages. Templates have the ability to run queries against the local database, iterate over data, use conditionals, format data, etc. The Prometheus templating language is based on the Go templating system.

Data Structures

The primary data structure for dealing with time series data is the sample, defined as:

type sample struct {
        Labels map[string]string
        Value  float64
}

The metric name of the sample is encoded in a special __name__ label in the Labels map.

[]sample means a list of samples.

interface{} in Go is similar to a void pointer in C.

Functions

In addition to the default functions provided by Go templating, Prometheus provides functions for easier processing of query results in templates.

If functions are used in a pipeline, the pipeline value is passed as the last argument.

Queries

NameArgumentsReturnsNotes
queryquery string[]sampleQueries the database, does not support returning range vectors.
first[]samplesampleEquivalent to index a 0
labellabel, samplestringEquivalent to index sample.Labels label
valuesamplefloat64Equivalent to sample.Value
sortByLabellabel, []samples[]sampleSorts the samples by the given label. Is stable.

firstlabel and value are intended to make query results easily usable in pipelines.

Numbers

NameArgumentsReturnsNotes
humanizenumberstringConverts a number to a more readable format, using metric prefixes.
humanize1024numberstringLike humanize, but uses 1024 as the base rather than 1000.
humanizeDurationnumberstringConverts a duration in seconds to a more readable format.
humanizeTimestampnumberstringConverts a Unix timestamp in seconds to a more readable format.

Humanizing functions are intended to produce reasonable output for consumption by humans, and are not guaranteed to return the same results between Prometheus versions.

Strings

NameArgumentsReturnsNotes
titlestringstringstrings.Title, capitalises first character of each word.
toUpperstringstringstrings.ToUpper, converts all characters to upper case.
toLowerstringstringstrings.ToLower, converts all characters to lower case.
matchpattern, textbooleanregexp.MatchString Tests for a unanchored regexp match.
reReplaceAllpattern, replacement, textstringRegexp.ReplaceAllString Regexp substitution, unanchored.
graphLinkexprstringReturns path to graph view in the expression browser for the expression.
tableLinkexprstringReturns path to tabular ("Console") view in the expression browser for the expression.

Others

NameArgumentsReturnsNotes
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.
tmplstring, []interface{}nothingLike 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.
safeHtmlstringstringMarks string as HTML not requiring auto-escaping.

Template type differences

Each of the types of templates provide different information that can be used to parameterize templates, and have a few other differences.

Alert field templates

.Value and .Labels contain the alert value and labels. They are also exposed as the $value and $labelsvariables for convenience.

Console templates

Consoles are exposed on /consoles/, and sourced from the directory pointed to by the -web.console.templatesflag.

Console templates are rendered with html/template, which provides auto-escaping. To bypass the auto-escaping use the safe* functions.,

URL parameters are available as a map in .Params. To access multiple URL parameters by the same name, .RawParams is a map of the list values for each parameter. The URL path is available in .Path, excluding the /consoles/ prefix.

Consoles also have access to all the templates defined with {{define "templateName"}}...{{end}} found in *.libfiles in the directory pointed to by the -web.console.libraries flag. As this is a shared namespace, take care to avoid clashes with other users. Template names beginning with prom_prom, and __ are reserved for use by Prometheus, as are the functions listed above.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值