Open-Source Wikis

/

Prometheus

/

Subsystems

/

Template

prometheus/prometheus

Template

Purpose

template/template.go is a thin layer over Go's text/template and html/template. It adds Prometheus-specific helpers used by alert templates and the legacy console UI templates.

File

template/
├── template.go            # Helpers + Expander wrapper.
├── template_test.go
└── template_amd64_test.go # arch-specific test for humanizeTimestamp.

What it adds

Helper Purpose
value Returns the active sample's value.
query Run a sub-PromQL expression and return its Vector (for use with range).
first First element of a slice.
humanize / humanize1024 SI / IEC unit-prefixed formatting.
humanizePercentage Percent formatting.
humanizeDuration Human-friendly duration formatting.
humanizeTimestamp RFC-3339 + age summary.
safeHtml Escape-bypass for HTML strings.
pathPrefix The configured external URL prefix.
parseDuration, formatDuration Duration parsing and formatting.
match, reReplaceAll Regexp helpers.
title, toUpper, toLower String helpers.
args Build a map from key/value pairs (for sub-template invocation).
tmpl Recursive template invocation.
externalURL The global.external_url config value.

Usage in alert templates

- alert: HighRequestErrors
  expr: rate(http_errors_total[5m]) > 10
  for: 10m
  labels:
    severity: warning
  annotations:
    summary: 'Errors on {{ $labels.job }} (instance: {{ $labels.instance }})'
    description: |
      Service is experiencing {{ humanize $value }} errors per second.
      Dashboard: {{ $externalURL }}/graph?g0.expr=rate(http_errors_total[5m])

$labels and $value are pre-bound; $externalURL is the configured external URL.

Sandboxing

The query helper goes through the active engine but bypasses the per-step sample budget — long-running templates can in theory load samples without enforcement. Templates that depend on query should be defensive (use topk, time bounds, etc.).

Console templates

Console templates under consoles/ (legacy, served by web/web.go) use the same helpers via template.NewTemplateExpander(...). The legacy console UI is being deprecated alongside the 2.x Mantine UI — see docs/migration.md.

Entry points for modification

  • Add a helper: edit templateFuncs in template/template.go. Provide both a function and a regression test.
  • Change sandboxing: add a wrapper around query to enforce per-template sample budgets.

See Rules for where the template package is consumed.

Built by Factory AutoWiki from public repository content. It is a generated preview for codebase exploration, not source-maintained documentation.

Template – Prometheus wiki | Factory