

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 自定义通知
<a name="v9-alerting-notifications"></a>

****  
本文档主题专为支持 **Grafana 9.x 版本**的 Grafana 工作区而设计。  
对于支持 Grafana 10.x 版本的 Grafana 工作区，请参阅[使用 Grafana 版本 10](using-grafana-v10.md)。  
对于支持 Grafana 8.x 版本的 Grafana 工作区，请参阅[使用 Grafana 版本 8](using-grafana-v8.md)。

使用通知模板自定义通知。

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

通知模板不与电子邮件或 Slack 等特定联系点集成绑定。但您可以选择为不同的联系点集成创建单独的通知模板。

您可以使用通知模板：
+ 在通知中添加、移除或重新排序信息，包括摘要、描述、标签和注释、值和链接
+ 为文本设置粗体和斜体格式，添加或移除换行符

您不能使用通知模板：
+ 更改 Slack 和 Microsoft Teams 等即时消息服务中的通知设计

**Topics**
+ [使用 Go 的模板语言](v9-alerting-notifications-go-templating.md)
+ [创建通知模板](v9-alerting-create-templates.md)
+ [模板参考](v9-alerting-template-reference.md)

# 使用 Go 的模板语言
<a name="v9-alerting-notifications-go-templating"></a>

****  
本文档主题专为支持 **Grafana 9.x 版本**的 Grafana 工作区而设计。  
对于支持 Grafana 10.x 版本的 Grafana 工作区，请参阅[使用 Grafana 版本 10](using-grafana-v10.md)。  
对于支持 Grafana 8.x 版本的 Grafana 工作区，请参阅[使用 Grafana 版本 8](using-grafana-v8.md)。

您可以使用 Go 的模板语言 [text/template](https://pkg.go.dev/text/template) 编写通知模板。

本节概述了 Go 的模板语言以及如何用 text/template 编写模板。

## dot
<a name="v9-go-dot"></a>

里面 text/template 有一个叫做 dot 的特殊光标，写成`.`了。您可以将此光标看作一个变量，其值会根据在模板中使用的位置而变化。例如，在通知模板 `.` 的开头引用 `ExtendedData` 对象，该对象包含多个字段，例如包括 `Alerts`、`Status`、`GroupLabels`、`CommonLabels`、`CommonAnnotations` 和 `ExternalURL`。但是，当在列表上的 `range` 中使用、在 `with` 中使用或编写要在其他模板中使用的功能模板时，dot 可能引用其他内容。您可以在 [创建通知模板](v9-alerting-create-templates.md) 中看到相关示例，在 [模板参考](v9-alerting-template-reference.md) 中看到所有数据和函数。

## 开始和结束标签
<a name="v9-go-openclosetags"></a>

在 text/template 中，模板以 `{{` 开头，以 `}}` 结尾，无论模板是打印变量还是运行 if 语句等控制结构。这与 Jinja 等其他模板语言不同，在 Jinja 中，打印变量使用 `{{` 和 `}}`，控制结构使用 `{%` 和 `%}`。

## Print（打印）
<a name="v9-go-print"></a>

要打印某项的值，请使用 `{{` 和 `}}`。您可以打印 dot 的值、dot 的字段、函数的结果和[变量](#v9-go-variables)的值。例如，要打印 `Alerts` 字段，其中 dot 引用 `ExtendedData`，您可以编写以下内容：

```
{{ .Alerts }}
```

## 迭代警报
<a name="v9-go-iterate-alerts"></a>

要仅打印每个警报的标签，而不是有关警报的所有信息，可使用 `range` 在 `ExtendedData` 中迭代警报：

```
{{ range .Alerts }}
{{ .Labels }}
{{ end }}
```

范围内的 dot 不再引用 `ExtendedData`，而是引用 `Alert`。您可以使用 `{{ .Labels }}` 打印每个警报的标签。之所以有效，是因为 `{{ range .Alerts }}` 会更改 dot，以引用警报列表中的当前警报。范围完成后，dot 将重置为范围开始之前的值，在本例中为 `ExtendedData`：

```
{{ range .Alerts }}
{{ .Labels }}
{{ end }}
{{/* does not work, .Labels does not exist here */}}
{{ .Labels }}
{{/* works, cursor was reset */}}
{{ .Status }}
```

## 迭代注释和标签
<a name="v9-go-iterate-labels"></a>

我们编写一个模板，以 `The name of the label is $name, and the value is $value` 格式打印每个警报的标签，其中 `$name` 和 `$value` 包含每个标签的名称和值。

与前面的示例一样，使用一个范围迭代 `.Alerts` 中的警报，使 dot 引用警报列表中的当前警报，然后在排序标签上使用第二个范围，使 dot 第二次更新以引用当前标签。在第二个范围内，使用 `.Name` 和 `.Value` 打印每个标签的名称和值：

```
{{ range .Alerts }}
{{ range .Labels.SortedPairs }}
The name of the label is {{ .Name }}, and the value is {{ .Value }}
{{ end }}
{{ range .Annotations.SortedPairs }}
The name of the annotation is {{ .Name }}, and the value is {{ .Value }}
{{ end }}
{{ end }}
```

## If 语句
<a name="v9-go-if"></a>

可在模板中使用 if 语句。例如，要在 `.Alerts` 中没有警报的情况下打印 `There are no alerts`，您可以编写以下内容：

```
{{ if .Alerts }}
There are alerts
{{ else }}
There are no alerts
{{ end }}
```

## With
<a name="v9-go-with"></a>

With 与 if 语句类似，但又有所不同，`with` 会更新 dot 以引用 with 的值：

```
{{ with .Alerts }}
There are {{ len . }} alert(s)
{{ else }}
There are no alerts
{{ end }}
```

## 变量
<a name="v9-go-variables"></a>

中的变量 text/template 必须在模板中创建。例如，要使用 dot 的当前值创建一个名为 `$variable` 的变量，您可以编写以下内容：

```
{{ $variable := . }}
```

您可以在范围或 `with` 内使用 `$variable`，以引用定义变量时的 dot 值，而不是当前 dot 的值。

例如，您不能编写在第二个范围中使用 `{{ .Labels }}` 的模板，因为这里的 dot 引用的是当前标签，而不是当前警报：

```
{{ range .Alerts }}
{{ range .Labels.SortedPairs }}
{{ .Name }} = {{ .Value }}
{{/* does not work because in the second range . is a label not an alert */}}
There are {{ len .Labels }}
{{ end }}
{{ end }}
```

您可以在第一个范围内和第二个范围之前定义一个名为 `$alert` 的变量来解决此问题：

```
{{ range .Alerts }}
{{ $alert := . }}
{{ range .Labels.SortedPairs }}
{{ .Name }} = {{ .Value }}
{{/* works because $alert refers to the value of dot inside the first range */}}
There are {{ len $alert.Labels }}
{{ end }}
{{ end }}
```

## 带索引的范围
<a name="v9-go-rangeindex"></a>

您可以在范围的开头定义 index 和 value 变量来获取范围内每个警报的索引：

```
{{ $num_alerts := len .Alerts }}
{{ range $index, $alert := .Alerts }}
This is alert {{ $index }} out of {{ $num_alerts }}
{{ end }}
```

## 定义模板
<a name="v9-go-define"></a>

您可以使用 `define` 和双引号中的模板名称来定义可在其他模板中使用的模板。定义的模板不能与其他模板同名，包括 `__subject`、`__text_values_list`、`__text_alert_list`、`default.title` 和 `default.message` 等默认模板。如果创建的模板与默认模板或其他通知模板中的模板同名，Grafana 可能会使用其中任何一个模板。当存在两个或多个同名模板时，Grafana 不会阻止或显示错误消息。

```
{{ define "print_labels" }}
{{ end }}
```

## 嵌入模板
<a name="v9-go-embed"></a>

您可以使用 `template`、双引号中的模板名称以及应传递给模板的光标在模板中嵌入定义的模板：

```
{{ template "print_labels" . }}
```

## 将数据传递给模板
<a name="v9-go-passdata"></a>

在模板中，dot 将引用传递给模板的值。

例如，如果向模板传递了触发警报列表，则 dot 引用该触发警报列表：

```
{{ template "print_alerts" .Alerts }}
```

如果模板传递了警报的排序标签，则 dot 引用已排序标签的列表：

```
{{ template "print_labels" .SortedLabels }}
```

这在编写可重用模板时很有用。例如，要打印所有警报，您可以编写以下内容：

```
{{ template "print_alerts" .Alerts }}
```

然后，如果仅打印触发警报，您可以这样编写：

```
{{ template "print_alerts" .Alerts.Firing }}
```

这是可以的，因为 `.Alerts` 和 `.Alerts.Firing` 都是警报列表。

```
{{ define "print_alerts" }}
{{ range . }}
{{ template "print_labels" .SortedLabels }}
{{ end }}
{{ end }}
```

## 评论
<a name="v9-go-comments"></a>

您可以使用 `{{/*` 和 `*/}}` 添加注释：

```
{{/* This is a comment */}}
```

要防止注释添加换行符，请使用：

```
{{- /* This is a comment with no leading or trailing line breaks */ -}}
```

## 缩进
<a name="v9-go-indentation"></a>

您可以使用缩进、制表符和空格以及换行符，来提高模板的可读性：

```
{{ range .Alerts }}
  {{ range .Labels.SortedPairs }}
    {{ .Name }} = {{ .Value }}
  {{ end }}
{{ end }}
```

但模板中的缩进也将出现在文本中。接下来，我们看如何将其移除。

## 移除空格和换行符
<a name="v9-go-removespace"></a>

正在 text/template 使用`{{-``-}}`并删除前导和尾随空格以及换行符。

例如，使用缩进和换行符来提高模板的可读性：

```
{{ range .Alerts }}
  {{ range .Labels.SortedPairs }}
    {{ .Name }} = {{ .Value }}
  {{ end }}
{{ end }}
```

缩进和换行符也将出现在文本中：

```
    alertname = "Test"

    grafana_folder = "Test alerts"
```

您可以移除文本中的缩进和换行符，将每个范围开始处的 `}}` 更改为 `-}}`：

```
{{ range .Alerts -}}
  {{ range .Labels.SortedPairs -}}
    {{ .Name }} = {{ .Value }}
  {{ end }}
{{ end }}
```

模板中的缩进和换行符现已从文本中消失：

```
alertname = "Test"
grafana_folder = "Test alerts"
```

# 创建通知模板
<a name="v9-alerting-create-templates"></a>

****  
本文档主题专为支持 **Grafana 9.x 版本**的 Grafana 工作区而设计。  
对于支持 Grafana 10.x 版本的 Grafana 工作区，请参阅[使用 Grafana 版本 10](using-grafana-v10.md)。  
对于支持 Grafana 8.x 版本的 Grafana 工作区，请参阅[使用 Grafana 版本 8](using-grafana-v8.md)。

创建可重用的通知模板发送给联系点。

您可以向通知模板添加一个或多个模板。

通知模板名称必须唯一。同一通知模板或不同通知模板中不能有两个同名的模板。避免定义与默认模板同名的模板，例如：`__subject`、`__text_values_list`、`__text_alert_list`、`default.title` 和 `default.message`。

在“联系点”选项卡中，您可以看到通知模板的列表。

## 创建通知模板
<a name="v9-alerting-creating-templates"></a>

**创建通知模板**

1. 单击**添加模板**。

1. 选择通知模板的名称，例如 `email.subject`。

1. 在内容字段中写入模板的内容。

   例如：

   ```
   {{ if .Alerts.Firing -}}
      {{ len .Alerts.Firing }} firing alerts
      {{ end }}
      {{ if .Alerts.Resolved -}}
      {{ len .Alerts.Resolved }} resolved alerts
      {{ end }}
   ```

1. 点击保存。

   `{{ define "email.subject" }}`（其中 `email.subject` 是模板名称），`{{ end }}` 自动添加到内容的开头和结尾。

**创建包含多个模板的通知模板：**

1. 单击**添加模板**。

1. 输入整个通知模板的名称。例如 `email`。

1. 在内容字段中写入每个模板，包括每个模板开头和结尾的 `{{ define "name-of-template" }}` 和 `{{ end }}`。您可以为通知模板中的每个模板使用描述性名称，例如，`email.subject` 或 `email.message`。在这种情况下，不要重复使用您在上面输入的通知模板的名称。

   下面的部分展示了您可能创建的模板的详细示例。

1. 点击保存。

## 为电子邮件主题创建模板
<a name="v9-alerting-create-template-subject"></a>

为电子邮件主题创建模板，其中包含触发和已解决的警报数量，如以下示例所示：

```
1 firing alerts, 0 resolved alerts
```

**为电子邮件主题创建模板**

1. 使用以下内容创建一个名为 `email.subject` 的模板：

   ```
   {{ define "email.subject" }}
   {{ len .Alerts.Firing }} firing alerts, {{ len .Alerts.Resolved }} resolved alerts
   {{ end }}
   ```

1. 创建联系点集成时，请使用模板将其放入带有 `template` 关键字的**主题**字段中。

   ```
   {{ template "email.subject" . }}
   ```

## 为电子邮件消息创建模板
<a name="v9-alerting-create-template-message"></a>

为电子邮件消息创建模板，其中包含所有触发和已解决的警报数量，如以下示例所示：

```
There are 2 firing alerts, and 1 resolved alerts

Firing alerts:

- alertname=Test 1 grafana_folder=GrafanaCloud has value(s) B=1
- alertname=Test 2 grafana_folder=GrafanaCloud has value(s) B=2

Resolved alerts:

- alertname=Test 3 grafana_folder=GrafanaCloud has value(s) B=0
```

**为电子邮件消息创建模板**

1. 创建一个名为 `email` 的通知模板，内容中包含两个模板：`email.message_alert` 和 `email.message`。

   `email.message_alert` 模板用于打印每个触发和已解决警报的标签和值，而 `email.message` 模板包含电子邮件的结构。

   ```
   {{- define "email.message_alert" -}}
   {{- range .Labels.SortedPairs }}{{ .Name }}={{ .Value }} {{ end }} has value(s)
   {{- range $k, $v := .Values }} {{ $k }}={{ $v }}{{ end }}
   {{- end -}}
   
   {{ define "email.message" }}
   There are {{ len .Alerts.Firing }} firing alerts, and {{ len .Alerts.Resolved }} resolved alerts
   
   {{ if .Alerts.Firing -}}
   Firing alerts:
   {{- range .Alerts.Firing }}
   - {{ template "email.message_alert" . }}
   {{- end }}
   {{- end }}
   
   {{ if .Alerts.Resolved -}}
   Resolved alerts:
   {{- range .Alerts.Resolved }}
   - {{ template "email.message_alert" . }}
   {{- end }}
   {{- end }}
   
   {{ end }}
   ```

1. 创建联系点集成时，请使用模板将其放入带有 `template` 关键字的**正文**字段中。

   ```
   {{ template "email.message" . }}
   ```

## 为 Slack 消息标题创建模板
<a name="v9-alerting-create-template-slack-title"></a>

为 Slack 消息标题创建模板，其中包含触发和已解决的警报数量，如以下示例所示：

```
1 firing alerts, 0 resolved alerts
```

**为 Slack 消息标题创建模板**

1. 使用以下内容创建一个名为 `slack.title` 的模板：

   ```
   {{ define "slack.title" }}
   {{ len .Alerts.Firing }} firing alerts, {{ len .Alerts.Resolved }} resolved alerts
   {{ end }}
   ```

1. 创建联系点集成时，请使用模板将其放入带有 `template` 关键字的**标题**字段中。

   ```
   {{ template "slack.title" . }}
   ```

## 为 Slack 消息内容创建模板
<a name="v9-alerting-create-template-slack-message"></a>

为 Slack 消息内容创建模板，其中包含所有触发和已解决警报的描述，包括其标签、注释和控制面板 URL：

```
1 firing alerts:

[firing] Test1
Labels:
- alertname: Test1
- grafana_folder: GrafanaCloud
Annotations:
- description: This is a test alert
Go to dashboard: https://example.com/d/dlhdLqF4z?orgId=1

1 resolved alerts:

[firing] Test2
Labels:
- alertname: Test2
- grafana_folder: GrafanaCloud
Annotations:
- description: This is another test alert
Go to dashboard: https://example.com/d/dlhdLqF4z?orgId=1
```

**为 Slack 消息内容创建模板**

1. 创建一个名为 `slack` 的模板，内容中包含两个模板：`slack.print_alert` 和 `slack.message`。

   `slack.print_alert` 模板用于打印标签、注释和 DashboardUrl，而 `slack.message` 模板包含通知的结构。

   ```
   {{ define "slack.print_alert" -}}
   [{{.Status}}] {{ .Labels.alertname }}
   Labels:
   {{ range .Labels.SortedPairs -}}
   - {{ .Name }}: {{ .Value }}
   {{ end -}}
   {{ if .Annotations -}}
   Annotations:
   {{ range .Annotations.SortedPairs -}}
   - {{ .Name }}: {{ .Value }}
   {{ end -}}
   {{ end -}}
   {{ if .DashboardURL -}}
     Go to dashboard: {{ .DashboardURL }}
   {{- end }}
   {{- end }}
   
   {{ define "slack.message" -}}
   {{ if .Alerts.Firing -}}
   {{ len .Alerts.Firing }} firing alerts:
   {{ range .Alerts.Firing }}
   {{ template "slack.print_alert" . }}
   {{ end -}}
   {{ end }}
   {{ if .Alerts.Resolved -}}
   {{ len .Alerts.Resolved }} resolved alerts:
   {{ range .Alerts.Resolved }}
   {{ template "slack.print_alert" .}}
   {{ end -}}
   {{ end }}
   {{- end }}
   ```

1. 创建联系点集成时，请使用模板将其放入带有 `template` 关键字的**正文**字段中。

   ```
   {{ template "slack.message" . }}
   ```

## 使用共享模板对电子邮件和 Slack 进行模板化
<a name="v9-alerting-create-shared-templates"></a>

您可以共享同一个模板，而不是为每个联系点创建单独的通知模板，例如电子邮件和 Slack。

例如，如果要发送包含此主题的电子邮件和包含标题 `1 firing alerts, 0 resolved alerts` 的 Slack 消息，则可以创建共享模板。

**创建共享模板**

1. 使用以下内容创建一个名为 `common.subject_title` 的模板：

   ```
   {{ define "common.subject_title" }}
   {{ len .Alerts.Firing }} firing alerts, {{ len .Alerts.Resolved }} resolved alerts
   {{ end }}
   ```

1. 对于电子邮件，请从电子邮件联系点集成中的主题字段运行模板：

   ```
   {{ template "common.subject_title" . }}
   ```

1. 对于 Slack，请从 Slack 联系点集成中的标题字段运行模板：

   ```
   {{ template "common.subject_title" . }}
   ```

## 使用通知模板
<a name="v9-alerting-use-notification-templates"></a>

使用联系点中的模板来自定义通知。

**在创建联系点时使用模板**

1. 从**警报**菜单中，选择**联系点**，以查看现有联系点列表。

1. 选择**添加联系点**。或者，您可以选择要编辑的联系点旁边的**编辑**图标（笔）来编辑现有联系点。

1. 在一个或多个字段（如**消息**或**主题**）中输入要使用的模板。要输入模板，请使用表单`{{ template "template_name" . }}`，*template\$1name*替换为要使用的模板的名称。

1. 单击**保存联系点**。

# 模板参考
<a name="v9-alerting-template-reference"></a>

****  
本文档主题专为支持 **Grafana 9.x 版本**的 Grafana 工作区而设计。  
对于支持 Grafana 10.x 版本的 Grafana 工作区，请参阅[使用 Grafana 版本 10](using-grafana-v10.md)。  
对于支持 Grafana 8.x 版本的 Grafana 工作区，请参阅[使用 Grafana 版本 8](using-grafana-v8.md)。

本节提供了创建模板的参考信息。

## 模板数据
<a name="v9-alerting-template-data"></a>

以下数据将传递给消息模板。


| Name | Type | 注意 | 
| --- | --- | --- | 
|  `Receiver`  |  字符串  |  要向其发送通知的联系点的名称。  | 
|  `Status`  |  字符串  |  如果至少有一个警报已触发，则为“触发”，否则为“已解决”。  | 
|  `Alerts`  |  警报  |  此通知中包含的警报对象列表（见下文）。  | 
|  `GroupLabels`  |  KeyValue  |  这些警报的分组标签。  | 
|  `CommonLabels`  |  KeyValue  |  此通知中包含的所有警报的通用标签。  | 
|  `CommonAnnotations`  |  KeyValue  |  此通知中包含的所有警报的通用注释。  | 
|  `ExternalURL`  |  字符串  |  指向发送通知的 Grafana 的返回链接。如果使用外部 Alertmanager，则是指向此 Alertmanager 的返回链接。  | 

`Alerts` 类型公开了两个函数，用于筛选返回的警报。
+ `Alerts.Firing`：返回已触发警报的列表。
+ `Alerts.Resolved`：返回已解决警报的列表。

**警报（类型）**

警报类型包含以下数据。


| Name | Type | 注意 | 
| --- | --- | --- | 
|  Status  |  字符串  |  `firing` 或 `resolved`。  | 
|  标签  |  KeyValue  |  附加到警报的一组标签。  | 
|  Annotations  |  KeyValue  |  附加到警报的一组注释。  | 
| 值 | KeyValue | 所有表达式的值，包括经典条件 | 
|  StartsAt  |  time.Time  |  警报开始触发的时间。  | 
|  EndsAt  |  time.Time  |  仅在知道警报结束时间时设置。否则，设置为从收到最后一次警报的时间起算的可配置的超时周期。  | 
|  GeneratorURL  |  字符串  |  指向 Grafana 或外部 Alertmanager 的返回链接。  | 
|  SilenceURL  |  字符串  |  指向静默警报的链接（其中预填了此警报的标签）。仅适用于 Grafana 管理的警报。  | 
|  DashboardURL  |  字符串  |  指向 Grafana 控制面板的链接（适用于警报规则属于 Grafana 控制面板的情况）。仅适用于 Grafana 管理的警报。  | 
|  PanelURL  |  字符串  |  指向 Grafana 控制面板中面板的链接（适用于警报规则属于 Grafana 控制面板中面板的情况）。仅适用于 Grafana 管理的警报。  | 
|  指纹  |  字符串  |  可用于识别警报的指纹。  | 
|  ValueString  |  字符串  |  该字符串包含警报中每个简化表达式的标签和值。  | 

 **ExtendedData**

该 ExtendedData 对象包含以下属性。


| Name | 类型 | 说明 | 示例 | 
| --- | --- | --- | --- | 
|  接收方  |  `string`  |  发送通知的联系点名称。  |  `{{ .Receiver }}`  | 
|  Status  |  `string`  |  状态为 `firing if at least one alert is firing, otherwise resolved.`。  |  `{{ .Status }}`  | 
|  警报  |  `[]Alert`  |  此通知中所有触发警报和已解决警报的列表。  |  `There are {{ len .Alerts }} alerts`  | 
|  触发警报  |  `[]Alert`  |  此通知中所有触发警报的列表。  |  `There are {{ len .Alerts.Firing }} firing alerts`  | 
|  已解决警报  |  `[]Alert`  |  此通知中所有已解决警报的列表。  |  `There are {{ len .Alerts.Resolved }} resolved alerts`  | 
|  GroupLabels  |  `KeyValue`  |  此通知中对这些警报进行分组的标签。  |  `{{ .GroupLabels }}`  | 
|  CommonLabels  |  `KeyValue`  |  此通知中所有警报的通用标签。  |  `{{ .CommonLabels }}`  | 
|  CommonAnnotations  |  `KeyValue`  |  此通知中所有警报的通用注释。  |  `{{ .CommonAnnotations }}`  | 
|  ExternalURL  |  `string`  |  指向发送此通知的 Grafana 工作区或 Alertmanager 的链接。  |  `{{ .ExternalURL }}`  | 

**KeyValue type**

`KeyValue`类型是一组表示标签和注释的 key/value 字符串对。

除了直接访问存储为 `KeyValue` 的数据外，还有方法可以对这些数据进行排序、删除和转换。


| Name | 参数 | 返回值 | 注意 | 示例 | 
| --- | --- | --- | --- | --- | 
|  SortedPairs  |    |  键值字符串对的排序列表  |    | `{{ .Annotations.SortedPairs }}` | 
|  删除  |  []string  |  KeyValue  |  返回不带给定键 Key/Value 的地图副本。  | `{{ .Annotations.Remove "summary" }}` | 
|  名称  |    |  []string  |  标签名称列表  | `{{ .Names }}` | 
|  值  |    |  []string  |  标签值列表  | `{{ .Values }}` | 

**时间**

时间来自 Go [https://pkg.go.dev/time#Time](https://pkg.go.dev/time#Time) 包。您可以用多种不同的格式打印时间。例如，要以 `Monday, 1st January 2022 at 10:00AM` 格式打印警报触发的时间，请编写以下模板：

```
{{ .StartsAt.Format "Monday, 2 January 2006 at 3:04PM" }}
```

您可以在[此处](https://pkg.go.dev/time#pkg-constants)找到 Go 的时间格式参考。

## 模板函数
<a name="v9-alerting-template-functions"></a>

使用模板函数，您可以处理标签和注释，以生成动态通知。可使用以下函数。


| Name | 参数类型 | 返回类型 | 说明 | 
| --- | --- | --- | --- | 
|  `humanize`  |  数字或字符串  |  字符串  |  使用公制前缀将数字转换为更易读的格式。  | 
|  `humanize1024`  |  数字或字符串  |  字符串  |  与 humanize 类似，但使用 1024 作为基数，而不是 1000。  | 
|  `humanizeDuration`  |  数字或字符串  |  字符串  |  将以秒为单位的持续时间转换为更易读的格式。  | 
|  `humanizePercentage`  |  数字或字符串  |  字符串  |  将比率值转换为百分比。  | 
|  `humanizeTimestamp`  |  数字或字符串  |  字符串  |  将以秒为单位的 Unix 时间戳转换为更易读的格式。  | 
|  `title`  |  字符串  |  字符串  |  strings.Title，将每个单词的第一个字符大写。  | 
|  `toUpper`  |  字符串  |  字符串  |  字符串。 ToUpper，将所有字符转换为大写。  | 
|  `toLower`  |  字符串  |  字符串  |  字符串。 ToLower，将所有字符转换为小写。  | 
|  `match`  |  模式、文本  |  布尔值  |  regexp。 MatchString 测试未锚定的 regexp 匹配项。  | 
|  `reReplaceAll`  |  模式、置换、文本  |  字符串  |  Regexp。 ReplaceAllString 正则表达式替换，未锚定。  | 
|  `graphLink`  |  字符串：包含 `expr` 和 `datasource` 字段的 JSON 对象  |  字符串  |  返回给定表达式和数据来源在 Explore 中图形视图的路径。  | 
|  `tableLink`  |  字符串：包含 `expr` 和 `datasource` 字段的 JSON 对象  |  字符串  |  返回给定表达式和数据来源在 Explore 中表格视图的路径。  | 
|  `args`  |  []interface\$1\$1  |  map[string]interface\$1\$1  |  将对象列表转换为带键的映射，例如 arg0、arg1。使用此函数可将多个参数传递给模板。  | 
|  `externalURL`  |  nothing  |  字符串  |  返回代表外部 URL 的字符串。  | 
|  `pathPrefix`  |  nothing  |  字符串  |  返回外部 URL 的路径。  | 

下表列出了每个函数的使用示例。


| 函数 | TemplateString | Input | 预期 | 
| --- | --- | --- | --- | 
|  humanize  |  \$1 humanize \$1value \$1  |  1234567.0  |  1.235M  | 
|  humanize1024  |  \$1 humanize1024 \$1value \$1  |  1048576.0  |  1Mi  | 
|  humanizeDuration  |  \$1 humanizeDuration \$1value \$1  |  899.99  |  14m 59s  | 
|  humanizePercentage  |  \$1 humanizePercentage \$1value \$1  |  0.1234567  |  12.35%  | 
|  humanizeTimestamp  |  \$1 humanizeTimestamp \$1value \$1  |  1435065584.128  |  2015-06-23 13:19:44.128 \$10000 UTC  | 
|  删除实例快照  |  \$1 \$1value \$1 title \$1  |  aa bB CC  |  Aa Bb Cc  | 
|  toUpper  |  \$1 \$1value \$1 toUpper \$1  |  aa bB CC  |  AA BB CC  | 
|  toLower  |  \$1 \$1value \$1 toLower \$1  |  aa bB CC  |  aa bb cc  | 
|  match  |  \$1 match "a\$1" \$1labels.instance \$1  |  aa  |  true  | 
|  reReplaceAll  |  \$1\$1 reReplaceAll “localhost :( .\$1)” “我的.domain：\$11" \$1labels.instance\$1\$1  |  localhost:3000  |  my.domain:3000  | 
|  graphLink  |  \$1\$1 graphLink "\$1\$1"expr\$1": \$1"up\$1", \$1"datasource\$1": \$1"gdev-prometheus\$1"\$1" \$1\$1  |    |  /explore?left=["now-1h","now","gdev-prometheus",\$1"datasource":"gdev-prometheus","expr":"up","instant":false,"range":true\$1]  | 
|  tableLink  |  \$1\$1 tableLink "\$1\$1"expr\$1":\$1"up\$1", \$1"datasource\$1":\$1"gdev-prometheus\$1"\$1" \$1\$1  |    |  /explore?left=["now-1h","now","gdev-prometheus",\$1"datasource":"gdev-prometheus","expr":"up","instant":true,"range":false\$1]  | 
|  args  |  \$1\$1define "x"\$1\$1\$1\$1.arg0\$1\$1 \$1\$1.arg1\$1\$1\$1\$1end\$1\$1\$1\$1template "x" (args 1 "2")\$1\$1  |    |  1 2  | 
|  externalURL  |  \$1 externalURL \$1  |    |  http://localhost/path/prefix  | 
|  pathPrefix  |  \$1 pathPrefix \$1  |    |  /path/prefix  | 