菜单
Grafana Cloud Enterprise 开源

通知模板示例

通知模板允许您更改默认通知消息。

您可以修改通知消息的内容和格式。例如,您可以自定义内容以仅显示特定信息,或调整格式以适应特定的联系点,如 Slack 或电子邮件。

注意

避免在通知模板中添加关于警报实例的额外信息,因为这些信息仅在通知消息中可见。

相反,您应该

使用标注或标签将信息直接添加到警报中,确保这些信息在 Grafana 内的警报状态和警报历史记录中也可见。然后,您可以在通知模板中打印新的警报标注或标签。

此页面提供了各种示例,说明如何模板化常见的通知消息。有关通知模板的更多详细信息,请参阅

基本示例

通知模板可以使用点 (.) 访问通知数据。以下示例展示了模板语言的一些基本用法。

例如,要检查通知中是否存在所有警报共有的常用标签 (.CommonLabels),请使用 if

Go
{{ define "custom_message" -}}
{{ if .CommonLabels }}
Alerts have common labels
{{ else }}
There are no common labels
{{ end }}
{{ end }}

或者,要遍历通知中的警报并打印特定标签,请使用 rangeindex

Go
{{ define "custom_message" -}}
{{ range .Alerts }}
The name of the alert is {{ index .Labels "alertname" }}
{{ end }}
{{ end }}

或者,您可以使用 . 符号来打印键的值。

Go
{{ define "custom_message" -}}
{{ range .Alerts }}
The name of the alert is {{ .Labels.alertname }}
{{ end }}
{{ end }}

然后,您可以通过传递通知数据(点 .来使用该模板

Go
{{ template "custom_message" . }}
template_output
The name of the alert is InstanceDown

The name of the alert is CpuOverload

这是一个示例,展示了如何显示通知中每个警报的摘要和描述标注。

Go
{{ define "custom.alerts" -}}
{{ len .Alerts }} alert(s)
{{ range .Alerts -}}
  {{ template "alert.summary_and_description" . -}}
{{ end -}}
{{ end -}}
{{ define "alert.summary_and_description" }}
  Summary: {{.Annotations.summary}}
  Status: {{ .Status }}
  Description: {{.Annotations.description}}
{{ end -}}

在此示例中

  • 定义了一个模板 (alert.summary_and_description),用于打印一个警报summarystatusdescription
  • 主模板 custom.alerts 遍历通知数据中的警报列表 (.Alerts),执行 alert.summary_and_description 模板以打印每个警报的详细信息。

通过传递点 (.) 执行模板

Go
{{ template "custom.alerts" . }}
template_output
2 alert(s)

  Summary: The database server db1 has exceeded 75% of available disk space.
  Status: firing
  Description: This alert fires when a database server is at risk of running out of disk space. You should take measures to increase the maximum available disk space as soon as possible to avoid possible corruption.

  Summary: The web server web1 has been responding to 5% of HTTP requests with 5xx errors for the last 5 minutes.
  Status: resolved
  Description: This alert fires when a web server responds with more 5xx errors than is expected. This could be an issue with the web server or a backend service.

以下示例与前一个类似,但它分离了触发和已解决的警报。

Go
{{ define "custom.firing_and_resolved_alerts" -}}
{{ len .Alerts.Resolved }} resolved alert(s)
{{ range .Alerts.Resolved -}}
  {{ template "alert.summary_and_description" . -}}
{{ end }}
{{ len .Alerts.Firing }} firing alert(s)
{{ range .Alerts.Firing -}}
  {{ template "alert.summary_and_description" . -}}
{{ end -}}
{{ end -}}
{{ define "alert.summary_and_description" }}
  Summary: {{.Annotations.summary}}
  Status: {{ .Status }}
  Description: {{.Annotations.description}}
{{ end -}}

模板分别访问 .Alerts.Firing.Alerts.Resolved,而不是 .Alerts,以打印每个警报的详细信息。

通过传递点 (.) 运行模板

Go
{{ template "custom.firing_and_resolved_alerts" . }}
template_output
1 resolved alert(s)

  Summary: The database server db1 has exceeded 75% of available disk space.
  Status: resolved
  Description: This alert fires when a database server is at risk of running out of disk space. You should take measures to increase the maximum available disk space as soon as possible to avoid possible corruption.

1 firing alert(s)

  Summary: The web server web1 has been responding to 5% of HTTP requests with 5xx errors for the last 5 minutes.
  Status: firing
  Description: This alert fires when a web server responds with more 5xx errors than is expected. This could be an issue with the web server or a backend service.

此示例仅显示通知中所有警报共有的标签和标注。

Go
{{ define "custom.common_labels_and_annotations" -}}
{{ len .Alerts.Resolved }} resolved alert(s)
{{ len .Alerts.Firing }} firing alert(s)
Common labels: {{ len .CommonLabels.SortedPairs }}
{{ range .CommonLabels.SortedPairs -}}
- {{ .Name }} = {{ .Value }}
{{ end }}
Common annotations: {{ len .CommonAnnotations.SortedPairs }}
{{ range .CommonAnnotations.SortedPairs }}
- {{ .Name }} = {{ .Value }}
{{ end }}
{{ end -}}

请注意,.CommonAnnotations.CommonLabels通知数据的一部分。

通过将点 (.) 作为参数来执行模板

Go
{{ template "custom.common_labels_and_annotations" . }}
template_output
1 resolved alert(s)
1 firing alert(s)
Common labels: 2
- grafana_folder = server_alerts
- team = server_admin

Common annotations: 0

此示例显示通知中每个警报的所有标签和标注。

Go
{{ define "custom.alert_labels_and_annotations" -}}
{{ len .Alerts.Resolved }} resolved alert(s)
{{ range .Alerts.Resolved -}}
  {{ template "alert.labels_and_annotations" . -}}
{{ end }}
{{ len .Alerts.Firing }} firing alert(s)
{{ range .Alerts.Firing -}}
  {{ template "alert.labels_and_annotations" . -}}
{{ end -}}
{{ end -}}
{{ define "alert.labels_and_annotations" }}
Alert labels: {{ len .Labels.SortedPairs }}
{{ range .Labels.SortedPairs -}}
- {{ .Name }} = {{ .Value }}
{{ end -}}
Alert annotations: {{ len .Annotations.SortedPairs }}
{{ range .Annotations.SortedPairs -}}
- {{ .Name }} = {{ .Value }}
{{ end -}}
{{ end -}}

在此示例中

  • custom.alert_labels_and_annotations 模板遍历已解决和触发的警报列表,类似于之前的示例。然后,它为每个警报执行 alert.labels_and_annotations
  • alert.labels_and_annotations 模板通过访问 .Labels.SortedPairs.Annotations.SortedPairs 来打印所有警报标签和标注。

通过传递点 (.) 运行模板

Go
{{ template "custom.alert_labels_and_annotations" . }}
template_output
1 resolved alert(s)

Alert labels: 4
- alertname = db_server_disk_space
- grafana_folder = server_alerts
- server = db1
- team = server_admin

Alert annotations: 2
- summary = The database server db1 has exceeded 75% of available disk space.
- description = This alert fires when a database server is at risk of running out of disk space. You should take measures to increase the maximum available disk space as soon as possible to avoid possible corruption.

1 firing alert(s)

Alert labels: 4
- alertname = web_server_http_errors
- grafana_folder = server_alerts
- server = web1
- team = server_admin

Alert annotations: 2
- summary = The web server web1 has been responding to 5% of HTTP requests with 5xx errors for the last 5 minutes.
- description = This alert fires when a web server responds with more 5xx errors than is expected. This could be an issue with the web server or a backend service.

请注意,以下示例仅适用于 Grafana 管理的警报。它显示一些警报数据,例如 DashboardURLPanelURLSilenceURL,这些数据仅限于 Grafana 管理的警报。

Go
{{ define "custom.alert_additional_details" -}}
{{ len .Alerts.Resolved }} resolved alert(s)
{{ range .Alerts.Resolved -}}
  {{ template "alert.additional_details" . -}}
{{ end }}
{{ len .Alerts.Firing }} firing alert(s)
{{ range .Alerts.Firing -}}
  {{ template "alert.additional_details" . -}}
{{ end -}}
{{ end -}}
{{ define "alert.additional_details" }}
- Dashboard: {{ .DashboardURL }}
- Panel: {{ .PanelURL }}
- AlertGenerator: {{ .GeneratorURL }}
- Silence: {{ .SilenceURL }}
- RunbookURL: {{ .Annotations.runbook_url}}
{{ end -}}

传递点 (.) 执行模板

Go
{{ template "custom.alert_additional_details" . }}
template_output
1 resolved alert(s)

- Dashboard: https://example.com/d/
- Panel: https://example.com/d/
- AlertGenerator: ?orgId=1
- Silence: https://example.com/alerting/silence/new
- RunbookURL: https://example.com/on-call/db_server_disk_space

1 firing alert(s)

- Dashboard: https://example.com/d/
- Panel: https://example.com/d/
- AlertGenerator: ?orgId=1
- Silence: https://example.com/alerting/silence/new
- RunbookURL: https://example.com/on-call/web_server_http_errors

标题或主题提供通知内容的一行摘要。

这是一个基本示例,显示通知中触发和已解决的警报数量。

Go
{{ define "custom_title" -}}
{{ if gt (.Alerts.Firing | len) 0 }}🚨 {{ .Alerts.Firing | len }} firing alerts. {{ end }}{{ if gt (.Alerts.Resolved | len) 0 }}✅ {{ .Alerts.Resolved | len }} resolved alerts.{{ end }}
{{ end -}}

通过将点 (.) 作为参数来执行模板

Go
{{ template "custom_title" . }}
template_output
🚨 1 firing alerts. ✅ 1 resolved alerts.

下一个示例是 Grafana 中使用的默认标题/主题模板的副本。

Go
{{ define "copy_of_default_title" -}}
[{{ .Status | toUpper }}{{ if eq .Status "firing" }}:{{ .Alerts.Firing | len }}{{ if gt (.Alerts.Resolved | len) 0 }}, RESOLVED:{{ .Alerts.Resolved | len }}{{ end }}{{ end }}] {{ .GroupLabels.SortedPairs.Values | join " " }} {{ if gt (len .CommonLabels) (len .GroupLabels) }}({{ with .CommonLabels.Remove .GroupLabels.Names }}{{ .Values | join " " }}{{ end }}){{ end }}
{{ end }}

这是一个更高级的示例

通过传递点 (.) 执行模板

Go
{{ template "copy_of_default_title" . }}
template_output
[FIRING:1, RESOLVED:1] api warning (sql_db)