菜单
Grafana Cloud Enterprise 开源 RSS

通知模板

你可以使用通知模板来更改通知的标题、消息和格式。

Grafana 提供了一个默认的通知标题模板 (default.title) 和一个默认的通知消息模板 (default.message)。这两个模板都显示常见的告警详情。

你还可以创建通知模板来自定义通知消息的内容和格式。例如:

  • 个性化电子邮件的主题或消息的标题。
  • 修改通知中的文本,例如选择或忽略某些标签、注释和链接。
  • 使用粗体和斜体样式格式化文本,以及添加或移除换行符。

但是,存在一些限制。你不能:

  • 修改视觉外观:向电子邮件通知添加 HTML 或 CSS 以进行视觉更改。修改 Slack 或 Microsoft Teams 等消息服务中的通知设计,例如添加自定义块或自适应卡片。
  • 管理媒体和数据:自定义传递给模板的数据结构或格式,例如添加新的 JSON 字段或发送用于 webhook 的 XML 数据。修改 webhook 中超出配置中定义的 HTTP 头,或调整图片数量、大小或位置。

下面是一个示例,它在通知中显示每个告警的摘要和描述注释:

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 -}}

通知消息将如下所示:

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.

注意

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

相反,你应该:

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

为联系点选择通知模板

通知模板不与特定的联系点集成(如电子邮件或 Slack)绑定,同一个模板可以跨多个联系点共享。

通知模板被分配给联系点,以确定发送给联系点集成的通知消息。

A flow of the alert notification process, from querying the alert rule to sending the alert notification message.
告警通知过程的流程,从查询告警规则到发送告警通知消息。

默认情况下,Grafana 提供默认模板,如 {{define "default.title"}}{{define "default.message"}},用于格式化通知消息。

更多信息

有关如何编写通知模板的更多详情,请参阅:

提示

关于模板化的实际示例,请参阅我们的“模板化入门”教程