You need to enable JavaScript to run this app.
文档中心
全栈可观测平台

全栈可观测平台

复制全文
下载 pdf
通知模板
云监控扩展函数与变量
复制全文
下载 pdf
云监控扩展函数与变量
通知内容模板中允许使用预置的告警变量,本文为您介绍云监控预置的告警变量结构和参数含义。​
扩展函数​
云监控通知内容模版中,支持 Go Template 内置函数和扩展函数。详情如下表所示:​
​
函数​
说明​
Go Template 内置函数​
通知内容模版支持使用 Go Template 内置函数,详情请参见 官方文档。​
translate​
​
将变量值转换为自然语言。​
translate(type string, lang string, val string) string​
  • type:变量名​
  • 告警模版:支持 Region、Type、Level、Namespace 变量。​
  • 事件模版:支持 Region、Type、Level、Source、Event 变量。​
  • lang:转换目标语言,当前只支持zh(中文)和 en(英文)。​
  • val:变量值。​
通用整形数字计算函数​
  • 和:add(i ...interface{}) int64​
  • 差:sub(a, b interface{}) int64​
  • 最大值:max(a interface{}, i ...interface{}) int64​
  • 最小值:min(a interface{}, i ...interface{}) int64​
集合操作函数​
切片​
  • slice(list interface{}, indices ...int) interface{}​
map​
  • get(m map[string]interface{}, key string) interface{}​
字符串函数​
  • 截断:trunc(c int, s string) string​
  • 截取:substr(start int, end int, s string) string​
  • 去除空白字符:trim(s string) string​
  • 包含:contains(substr string, str string) bool​
  • 前缀:hasPrefix(substr string, str string) bool​
  • 后缀:hasSuffix(substr string, str string) bool​
  • 转义:quote(str ...interface{}) string​
  • 拼接:join(sep string, v interface{}) string​
  • 替换:replace(old string, new string, src string) string​
  • 转字符串:toString(v interface{}) string​
时间函数​
  • 如果变量是time.Time类型,可以使用时间函数.Format定义时间显示格式。例如 {{ .HappenedAt.Format "2006-01-02 15:04:05(UTCZ07:00)" }}。​
JSON 操作​
  • JSON:toJson(v interface{}) string​
  • PrettyJSON:toPrettyJson(v interface{}) string​
​
数据结构​
本文通过以下 JSON 示例代码,展示了云监控告警模版所支持的变量、参数和数据结构。方便您理解 Go Template 的常用语法和使用方式。​
​
{​
"MaxCount": 5,​
"Type": "Metric",​
"SiteName": "云监控",​
"AccountName": "user-demo",​
"AccountId": "21000*****",​
"RuleName": "alarm-demo",​
"RuleId": "19251*****",​
"RuleURL": "https://console.volcengine.com/observe/cloud-monitor/alert/strategy?id=19251*****",​
"RuleType": "static",​
"Namespace": "VCM_ECS",​
"SubNamespace": "Instance",​
"Region": "cn-beijing",​
"HappenedAt": "2025-01-02 00:00:00(UTC+08:00)",​
"RuleCondition": "QPS 平均值环比上周期上涨或下降 0%, 持续 10 个周期(1 周期=1 分钟)",​
"Level": "critical",​
"ProjectName": "default",​
"TagSet": [​
{​
"Key": "test",​
"Value": "test"​
}​
],​
"Resources": [​
{​
"AlertGroupId": "67fd19******",​
"AlertGroupURL": "https://console.volcengine.com/observe/cloud-monitor/alert/history/detail?id=67fd19*****",​
"Id": "i-yd9j*****",​
"Name": "ecs-demo",​
"DetailURL": "https://console.volcengine.com/observe/cloud-monitor/product/*******",​
"Region": "cn-beijing",​
"FirstAlertTime": "2025-07-06 00:00:00(UTC+08:00)",​
"LastAlertTime": "2025-07-07 00:00:00(UTC+08:00)",​
"LastSendLevel": "warning",​
"Metrics": [​
{​
"Name": "CpuTotal",​
"Unit": "%",​
"Threshold": "80",​
"CurrentValue": "90",​
"Description": "CPU 使用率",​
"Warn": "true",​
"IsTimeCompare": "true",​
"InherentUnit": "%",​
"LastValue": "85",​
"LastDescription": "上周期 CPU 使用率",​
"CurrentValue": "91",​
"CurrentDescription": "当前 CPU 使用率"​
}​
],​
"Dimensions": [​
{​
"Name": "ResourceID",​
"Value": "i-yd9j*****",​
"Description": "实例 ID"​
}​
],​
"ProjectName": "default",​
"TagSet": [​
{​
"Key": "test",​
"Value": "test"​
}​
],​
"Metadatas": [​
{​
"Name": "IP.Inside",​
"Value": "192.168.0.1",​
"Description": "私网 IP 地址"​
}​
]​
}​
], ​
"HistoryURL": "https://console.volcengine.com/observe/cloud-monitor/alert/history",​
"CreateSilenceURL": "https://console.volcengine.com/observe/cloud-monitor/notice/silencePolicy/create?ruleId=19251*****"​
}​
​
变量说明​
说明​
  • 对于非嵌套变量,可以使用 Go Template 标准方式,获取变量的值。例如{{ .Region }}。​
  • 对于嵌套变量,需要通过自定义变量的方式获取所有的资源。例如定义$resource变量,然后通过遍历的方式获取所有的资源{{ range $idx, $resource := .Resources }}。对于内部变量,则需使用$resource.字段名的方式获取变量的值,例如{{ $resource.Id }}。​
通知内容模板中,可以使用的预置变量和说明,如下表所示。​
​
变量名​
类型​
说明​
示例值​
用法示例​
MaxCount​
Int​
配置通知模板在当前发送渠道的最大渲染资源数量。​
5​
{{ .MaxCount }}​
Type​
String​
告警类型,取值:​
  • Metric:告警​
  • MetricRecovered:告警恢复​
  • MetricsNoData:无数据告警​
  • NoDataRecovered:无数据告警恢复​
Metric​
  • 直接渲染:{{ .Type }}​
  • 使用扩展函数翻译为自然语言:{{ translate "Type" "zh" .Type }},例如将Metric翻译为告警。​
  • 定义一个是否为告警恢复的变量:{{- $isRecovered := contains "Recovered" .Type -}}​
SiteName​
String​
站点名称。​
云监控​
{{ .SiteName }}​
AccountName​
String​
账号名称。​
user-demo​
{{ .AccountName }}​
AccountId​
String​
账号 ID。​
21000*****​
{{ .AccountId }}​
RuleName​
String​
告警策略名称。​
alarm-demo​
{{ .RuleName }}​
RuleId​
String​
告警策略 ID。​
19251*****​
{{ .RuleId }}​
RuleURL​
String​
告警策略详情链接。​
https://console.volcengine.com/observe/cloud-monitor/alert/strategy?id=19251*****4565*****​
{{ .RuleURL }}​
RuleType​
String​
告警策略类型,取值:​
  • static:静态资源范围。​
  • dynamic:动态资源范围。​
  • account:账号级别指标告警。​
static​
{{ .RuleType }}​
Namespace​
String​
告警策略所属命名空间。​
VCM_ECS​
  • 直接渲染:{{ .Namespace }}​
  • 使用扩展函数翻译为自然语言:{{ translate "Namespace" "zh" .Namespace }}​
SubNamespace​
String​
监控指标所属维度。​
Instance​
{{ .SubNamespace }}​
Region​
String​
告警策略所属地域。​
cn-beijing​
  • 直接渲染:{{ .Region }}​
  • 使用扩展函数翻译为自然语言:{{ translate "Region" "zh" .Region }},例如将cn-beijing翻译为 华北2(北京)。​
HappenedAt​
time.Time​
告警发生时间。​
说明​
当 Type 取值为 MetricRecovered 或 NoDataRecovered 时,该字段表示告警恢复时间。​
2025-01-02 00:00:00(UTC+08:00)​
格式化时间:{{ .HappenedAt.Format "2006-01-02 15:04:05(UTCZ07:00)" }}​
RuleCondition​
String​
告警触发条件。​
QPS 平均值环比上周期上涨或下降 0%, 持续 10 个周期(1 周期=1 分钟)​
{{ .RuleCondition }}​
Level​
String​
告警级别,取值:​
  • critical:严重​
  • warning:警告​
  • notice:通知​
critical​
  • 直接渲染:{{ .Level }}​
  • 使用扩展函数翻译为自然语言:{{ translate "Level" "zh" .Level }},例如将critical翻译为严重。​
ProjectName​
String​
告警策略所属项目名称。​
default​
{{ .Project }}​
TagSet​
Array of Tag​
告警策略标签,每个标签包含Key和Value。​
-​
遍历标签:​
​
{{ range $idx, $tag := .Tags }}​
{{ $tag.Key }}: {{ $tag.Value }}​
{{ end }}​
​
Resources​
Array of ResourceMetric​
触发告警的云产品资源列表。​
-​
遍历资源:​
​
{{ range $idx, $resource := .Resources }}​
{{ $resource.Name }} ({{ $resource.Id}})​
{{ end }}​
​
HistoryURL​
String​
告警历史页面链接。​
https://console.volcengine.com/observe/cloud-monitor/alert/history​
{{ .HistoryURL }}​
CreateSilenceURL​
String​
创建告警屏蔽策略的链接。​
https://console.volcengine.com/observe/cloud-monitor/notice/silencePolicy/create?ruleId=19251*****​
{{ .CreateSilenceURL }}​
​
Tag​
​
变量名​
类型​
说明​
示例值​
用法示例​
Key​
String​
标签键。​
test​
{{ $tag.Key }}​
Value​
String​
标签值。​
test​
{{ $tag.Value }}​
​
ResourceMetric​
​
变量名​
类型​
说明​
示例值​
用法示例​
AlertGroupId​
String​
告警历史 ID。​
67fd19******​
{{ $resource.AlertGroupId }}​
AlertGroupURL​
String​
云产品资源告警详情链接。​
https://console.volcengine.com/observe/cloud-monitor/alert/history/detail?id=67fd19*****​
{{ $resource.AlertGroupURL }}​
Id​
String​
云产品资源 ID。​
i-yd9j*****​
{{ $resource.Id }}​
Name​
String​
云产品资源名称。​
ecs-demo​
{{ $resource.Name }}​
DetailURL​
String​
云产品资源监控链接。​
https://console.volcengine.com/observe/cloud-monitor/product/*******​
{{ $resource.DetailURL }}​
Region​
String​
云产品资源所属地域。​
cn-beijing​
  • 直接渲染:{{ $resource.Region }}​
  • 使用扩展函数翻译为自然语言:{{ translate "Region" "zh" $resource.Region }},例如将cn-beijing翻译为华北2(北京)。​
FirstAlertTime​
time.Time​
告警首次发生时间。​
2025-01-01 00:00:00(UTC+08:00)​
  • 格式化时间:{{ $resource.FirstAlertTime.Format "2006-01-02 15:04:05(UTCZ07:00)" }}​
LastAlertTime​
time.Time​
告警最后发生时间。​
说明​
该字段与 FirstAlertTime 的差值为告警持续时长。​
2025-01-02 00:00:00(UTC+08:00)​
  • 格式化时间:{{ $resource.LastAlertTime.Format "2006-01-02 15:04:05(UTCZ07:00)" }}​
  • 计算持续时长:{{ ($resource.LastAlertTime.Sub $resource.FirstAlertTime) | toString }}​
LastSendLevel​
String​
上一次发送告警级别,取值:​
  • critical:严重​
  • warning:警告​
  • notice:通知​
告警首次发送时,该值为空。​
warning​
  • 直接渲染:{{ .Level }}​
  • 使用扩展函数翻译为自然语言:{{ translate "Level" "zh" $resource.LastSendLevel }},例如将warning翻译为警告。​
Metrics​
Array of MetricValue​
告警策略配置的指标列表。​
当值为 NaN时,表示无数据。​
-​
遍历指标:​
​
{{ range $idx, $metric := $resource.Metrics }}​
{{ $metric.Name }}: {{ $metric.CurrentValue }}{{ $metric.Unit }}​
{{ end }}​
​
Dimensions​
Array of MetricDimension​
云产品资源的维度列表。​
-​
遍历维度:​
​
{{ range $idx, $dimension := $resource.Dimensions }}​
{{ $dimension.Name }}: {{ $dimension.Value }}​
{{ end }}​
​
ProjectName​
String​
云产品资源所属的项目名称。​
default​
{{ $resource.ProjectName }}​
TagSet​
Array of Tag​
云产品资源标签。​
-​
遍历标签:​
​
{{ range $key, $val := $resource.Tags }}​
{{ $key }}: {{ $val }}​
{{ end }}​
​
Metadatas​
Array of Metadata​
云产品资源用于告警的元数据。​
-​
遍历元数据:​
​
{{ range $idx, $meta := $resource.Metadatas }}​
{{ $meta.Name }}: {{ $meta.Value }}​
{{ end }}​
​
​
MetricValue​
​
变量名​
类型​
说明​
示例值​
用法示例​
Name​
String​
指标名称。​
CpuTotal​
{{ $metric.Name }}​
Unit​
String​
阈值单位。​
%​
{{ $metric.Unit }}​
Threshold​
String​
指标阈值。​
80​
{{ $metric.Threshold }}​
CurrentValue​
String​
当前指标值。​
90​
{{ $metric.CurrentValue }}​
Description​
String​
指标描述。​
CPU 使用率​
{{ $metric.Description }}​
Warn​
Bool​
指标是否满足阈值条件。取值:​
  • true:满足阈值。​
  • false:不满足阈值。​
true​
{{ $metric.Warn }}​
IsTimeCompare​
Bool​
是否同比、环比。取值:​
  • true:同比。​
  • false:环比。​
true​
{{ $metric.IsTimeCompare }}​
InherentUnit​
String​
指标单位。​
%​
{{ $metric.IsTimeCompare }}​
LastValue​
String​
上次指标值。​
85​
{{ $metric.LastValue }}​
LastDescription​
String​
上次指标描述,格式为{上周期}/{昨天同一时刻}/{上周同一时刻}{指标名}。​
上周期 CPU 使用率​
{{ $metric.LastDescription }}​
CurrentValue​
String​
当前指标值。​
91​
{{ $metric.CurrentValue }}​
CurrentDescription​
String​
当前指标描述,格式为{当前}{指标名}。​
当前 CPU 使用率​
{{ $metric.CurrentDescription }}​
​
MetricDimension​
​
变量名​
类型​
说明​
示例​
用法示例​
Name​
String​
云产品维度名称。​
ResourceID​
{{ $dimension.Name }}​
Value​
String​
云产品维度值。​
i-yd9j*****ajrd7*****​
{{ $dimension.Value }}​
Description​
String​
云产品维度描述。​
实例 ID​
{{ $dimension.Description }}​
​
Metadata​
说明​
不同云产品用于告警的元信息不同,部分产品可能不存在用于告警的元信息。​
​
变量名​
类型​
说明​
示例​
用法示例​
Name​
String​
元数据名称。​
IP.Inside​
{{ $meta.Name }}​
Value​
String​
元数据值。​
192.168.0.1​
{{ $meta.Value }}​
Description​
String​
维度描述。​
私网 IP 地址​
{{ $meta.Description }}​
​
配置示例​
除系统预置的通知模板外,您可以基于告警变量的数据结构、Go Template 语法和 Markdown 语法,配置自定义消息模板的内容,示例如下。​
​
**告警信息**:​
​
---​
​
告警类型:{{ .Type }}​
账号名称:{{ .AccountName }}​
账号 ID:{{ .AccountId }}​
告警策略名称:{{ .RuleName }}​
告警策略 ID:{{ .RuleId }}​
告警策略详情:{{ .RuleURL }}​
告警策略类型:{{ .RuleType }}​
告警涉及的云产品:{{ .Namespace }}​
告警涉及的云产品维度:{{ .SubNamespace }}​
云产品所在地域:{{ .Region }}​
告警发生时间:{{ .HappenedAt }}​
告警触发条件:{{ .RuleCondition }}​
告警级别:{{ .Level }}​
配置告警屏蔽策略的链接:{{ .CreateSilenceURL }}​
​
---​
​
**产生告警的资源详情**:​
{{- range $idx, $resource := .Resources }}​
云产品资源 ID:{{- $resource.Id }}​
云产品资源名称:{{- $resource.Name }}​
云产品资源所属地域:{{- $resource.Region }}​
云产品资源告警详情链接:{{- $resource.AlertGroupURL }}​
告警首次发生时间:{{- $resource.FirstAlertTime }}​
告警最后发生时间:{{- $resource.LastAlertTime }}​
告警策略配置的指标列表:{{ range $idx, $metric := $resource.Metrics }}​
指标名称:{{ $metric.Name }}​
阈值单位:{{ $metric.Unit }}​
指标阈值:{{ $metric.Threshold }}​
指标当前值:{{ $metric.CurrentValue }}​
{{- end }}​
{{- end }}​
​
​
最近更新时间:2026.07.31 11:02:19
这个页面对您有帮助吗?
有用
有用
无用
无用