Prometheus 支持在告警的注释和标签中以及在提供的控制台页面中使用模板。 模板能够针对本地数据库运行查询,迭代数据,使用条件,格式化数据等。 Prometheus 模板语言基于 Go 模板系统。
alert: InstanceDown
expr: up == 0
for: 5m
labels:
severity: page
annotations:
summary: "Instance {{$labels.instance}} down"
description: "{{$labels.instance}} of job {{$labels.job}} has been down for more than 5 minutes."
告警字段模板将在每次规则迭代期间为每个触发的告警执行,因此请保持任何查询和模板的轻量级。 如果您需要更复杂的告警模板,建议链接到控制台。
这会显示实例列表以及它们是否已启动
{{ range query "up" }}
{{ .Labels.instance }} {{ .Value }}
{{ end }}
特殊的.
变量包含每次循环迭代的当前样本的值。
{{ with query "some_metric{instance='someinstance'}" }}
{{ . | first | value | humanize }}
{{ end }}
Go 和 Go 的模板语言都是强类型的,因此必须检查是否返回了样本以避免执行错误。 例如,如果尚未运行抓取或规则评估,或者主机已关闭,则可能会发生这种情况。
包含的 prom_query_drilldown
模板处理此问题,允许格式化结果,并链接到表达式浏览器。
{{ with printf "node_memory_MemTotal{job='node',instance='%s'}" .Params.instance | query }}
{{ . | first | value | humanize1024 }}B
{{ end }}
如果以 console.html?instance=hostname
的形式访问,.Params.instance
将评估为 hostname
。
<table>
{{ range printf "node_network_receive_bytes{job='node',instance='%s',device!='lo'}" .Params.instance | query | sortByLabel "device"}}
<tr><th colspan=2>{{ .Labels.device }}</th></tr>
<tr>
<td>Received</td>
<td>{{ with printf "rate(node_network_receive_bytes{job='node',instance='%s',device='%s'}[5m])" .Labels.instance .Labels.device | query }}{{ . | first | value | humanize }}B/s{{end}}</td>
</tr>
<tr>
<td>Transmitted</td>
<td>{{ with printf "rate(node_network_transmit_bytes{job='node',instance='%s',device='%s'}[5m])" .Labels.instance .Labels.device | query }}{{ . | first | value | humanize }}B/s{{end}}</td>
</tr>{{ end }}
</table>
在这里,我们迭代所有网络设备,并显示每个设备的网络流量。
由于 range
操作未指定变量,因此 .Params.instance
在循环内不可用,因为 .
现在是循环变量。
Prometheus 支持定义可以重用的模板。 当与控制台库支持结合使用时,这尤其强大,允许跨控制台共享模板。
{{/* Define the template */}}
{{define "myTemplate"}}
do something
{{end}}
{{/* Use the template */}}
{{template "myTemplate"}}
模板仅限于一个参数。 args
函数可用于包装多个参数。
{{define "myMultiArgTemplate"}}
First argument: {{.arg0}}
Second argument: {{.arg1}}
{{end}}
{{template "myMultiArgTemplate" (args 1 2)}}
此文档是开源的。 请通过提交问题或拉取请求来帮助改进它。