Template reference

Prometheus supports templating in the annotations and labels of alerts,as well as in served console pages. Templates have the ability to runqueries against the local database, iterate over data, use conditionals,format data, etc. The Prometheus templating language is based on the Gotemplating 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 defaultfunctions provided by Gotemplating, Prometheus provides functions for easier processing of queryresults 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 consumptionby humans, and are not guaranteed to return the same results between Prometheusversions.

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 toparameterize templates, and have a few other differences.

Alert field templates

.Value, .Labels, and ExternalLabels contain the alert value, the alertlabels, and the globally configured external labels, respectively. They arealso exposed as the $value, $labels, and $externalLabels variables forconvenience.

Console templates

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

Console templates are rendered withhtml/template, which providesauto-escaping. To bypass the auto-escaping use the safe* functions.,

URL parameters are available as a map in .Params. To access multiple URLparameters by the same name, .RawParams is a map of the list values for eachparameter. 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 toby the -web.console.libraries flag. As this is a shared namespace, take careto avoid clashes with other users. Template names beginning with prom,prom, and _ are reserved for use by Prometheus, as are the functionslisted above.