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:

  1. type sample struct {
  2. Labels map[string]string
  3. Value float64
  4. }

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.

first, label 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.
humanizePercentagenumberstringConverts a ratio value to a fraction of 100.
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 (“Table”) 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, .Labels, and .ExternalLabels contain the alert value, the alert labels, and the globally configured external labels, respectively. They are also exposed as the $value, $labels, and $externalLabels variables for convenience.

Console templates

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

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. The globally configured external labels are available as .ExternalLabels. There are also convenience variables for all four: $rawParams, $params, $path, and $externalLabels.

Consoles also have access to all the templates defined with {{define "templateName"}}...{{end}} found in *.lib files 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.