在告警策略中配置通知内容、自定义接口回调配置和多维分析时,可使用告警通知变量来自定义告警通知内容和多维分析语句,使得收到的告警通知更加清晰准确地描述告警原因。
快速开始
详细日志:{{.QueryLog[0][0]}}
配置完成后如下图所示:


收到告警通知时,附加通知内容将自动替换为下面的值,代表触发告警时最近的一条详细日志。
详细日志:{"content":{"body_bytes_sent":"33352","http_referer":"-","http_user_agent":"Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.17 Safari/537.36","remote_addr":"201.80.83.199","remote_user":"-","request_method":"GET","request_uri":"/content/themes/test-com/images/header_about.jpg","status":"404","time_local":"01/Nov/2018:01:16:31"},"fileName":"/root/testLog/nginx.log","pkg_id":"285A243662909DE3-70A","source":"172.17.0.2","time":1653831150008,"topicId":"a54de372-ffe0-49ae-a12e-c340bb2b03f2"}
通知变量
变量列表
变量 | 含义 | 变量值示例 | 投递日志 | 说明 |
{{.RecordId}} | 告警通知 ID | 76afb409-4648-xxxx-b67a-fe350129ceb4 | 否 | 每次告警通知均使用不同的 ID。 |
{{.RecordGroupId}} | 告警分组 ID | 52746a79-7681-495f-xxxx-2715593e625a | 是 | 告警从触发到恢复,无论中间重复发送了多少次通知,对应的 ID 均相同。 告警策略启用分组触发功能时,同一时间可能产生多条告警,分别对应不同的分组及 ID。 |
{{.UIN}} | 账号 ID | 100007xxx827 | 是 | - |
{{.Nickname}} | 账号昵称 | xx 企业 | 是 | - |
{{.Region}} | 地域 | 广州 注意: 投递日志时为英文,例如 ap-guangzhou。 | 是 | - |
{{.Alarm}} | 告警策略名称 | Nginx 错误日志过多 | 是 | - |
{{.AlarmID}} | 告警策略 ID | notice-3abd7ad6-15b7-4168-xxxx-52e5b961a561 | 是 | - |
{{.Topic}} | 日志主题名称 | nginxLog | 是 | - |
{{.TopicId}} | 日志主题 ID | a54de372-ffe0-49ae-xxxx-c340bb2b03f2 | 是 | - |
{{.QueryParams}} | 执行语句查询参数 | [{ "TopicId": "36c6df96-a6ad-xxxx-99ec-3f0b66dea302", "TopicName": "CDN Demo访问日志日志主题_10000753xxxx", "TopicType": "log", "Query": "* | select approx_percentile(request_time, 0.99) as p99", "grammarVersion": "cql", "StartTime": 1690956330000, "EndTime": 1690957230000, "QueryUrlPath": "/cls/search?grammarVersion=cql&queryBase64=KiB8IHNlbGVjdCBhcHByb3hfcGVyY2VudGlsZShyZXF1ZXN0X3RpbWUsIDAuOTkpIGFzIHA5OQ%3D%3D®ion=ap-chongqing&time=2023-08-02T14%3A05%3A30.000%2C2023-08-02T14%3A20%3A30.000&topic_id=36c6df96-a6ad-xxxx-99ec-3f0b66dea302" }] | 是 | 数组结构,{{.QueryParams[0]}}代表第1个执行语句,{{.QueryParams[1]}}代表第2个执行语句,以此类推。 |
{{.Condition}} | 触发条件 | $1.errorLogCount > 1 | 是 | - |
{{.ConditionGroup}} | 告警分组触发时对应的分组信息 | {"$1.AppName":"userManageService"} | 是 | 告警策略启用分组触发功能时才具备该变量。 |
{{.Level}} | 告警级别(英文) | Info | 是 | Critical Warn Info |
{{.Level_zh}} | 告警级别(中文) | 提醒 | 否 | 紧急 警告 提醒 |
{{.HappenThreshold}} | 告警所需的触发条件持续满足次数 | 1 | 是 | - |
{{.AlertThreshold}} | 告警间隔时间 | 15 | 是 | 单位:分钟 |
{{.Label}} | 附加分类字段 | {"service":"payService"} | 是 | |
{{.StartTime}} | 第一次告警触发时间 | 2022-05-28 18:56:37 | 是 | 时区:Asia/Shanghai |
{{.StartTimeUnix}} | 第一次告警触发时间戳 | 1653735397099 | 是 | 毫秒级 UNIX 时间戳 |
{{.NotifyTime}} | 本次告警通知时间 | 2022-05-28 19:41:37 | 是 | 时区:Asia/Shanghai |
{{.NotifyTimeUnix}} | 本次告警通知时间戳 | 1653738097099 | 是 | 毫秒级 UNIX 时间戳 |
{{.NotifyType}} | 告警通知类型 | 1 | 是 | 1:代表告警通知 2:代表恢复通知 |
{{.ConsecutiveAlertNums}} | 连续告警次数 | 2 | 是 | - |
{{.Duration}} | 告警持续时间 | 0 | 是 | 单位:分钟 |
{{.DetailUrl}} | 告警详情页面链接 | https://alarm.cls.tencentcs.com/MDv2xxJh | 是 | 无需登录账号 |
{{.QueryUrl}} | 第一个执行语句的检索分析链接 | https://alarm.cls.tencentcs.com/T0pkxxMA | 否 | 如需获取其它执行语句链接地址,请使用 {{.QueryParams}} 。 |
{{.SilentUrl}} | 告警屏蔽链接 | https://alarm.cls.tencentcs.com/K6hyxxyG | 是 | 无需登录账号,需{{.CanSilent}}为 true 时方可使用,例如: {{if .CanSilent}}{{.SilentUrl}}{{end}} 。 |
{{.CanSilent}} | 是否支持通过{{.SilentUrl}}直接屏蔽告警 | true | 是 | 在通知渠道组中开启免登录屏蔽告警功能后该值为 true,否则为 false。 |
{{.Message}} | 附加通知内容 | - | 是 | 特指告警策略配置中填写的“附加通知内容”,最大10KB,超出后将自动截断。 |
{{.TriggerParams}} | 告警触发时参数 | $1.errorLogCount=5; | 是 | 执行语句有多条结果同时满足触发条件时,仅记录其中的第一条结果。 仅告警触发时具备该变量,告警恢复时无该变量。 |
{{.QueryResult}} | 执行语句执行结果 | 是 | - | |
{{.TriggerResult}} | 满足触发条件的执行语句结果 | 是 | 仅告警触发时具备该变量,告警恢复时无该变量。 | |
{{.QueryLog}} | 执行语句中检索条件匹配到的详细日志 | 是 | - | |
{{.AnalysisResult}} | 多维分析结果 | 是 | 仅告警触发时具备该变量,告警恢复时无该变量。 | |
{{.AnalysisResultFormat}} | 格式化后的多维分析结果(英文) | - | 否 | 内容与{{.AnalysisResult}}一致,但将 json 格式化为文本形式,使用换行、表格等形式展示,提高内容易读性。 |
{{.AnalysisResultFormat_zh}} | 格式化后的多维分析结果(中文) | - | 否 | |
AlarmExecutionStatus | 告警策略执行结果 | TriggerNotice.SendSuccess | 是 | 仅支持投递日志。 |
AlarmExecutionErrorDetail | 告警策略执行异常详情 | {"TriggerFailedReason":"Parse expression failed: Invalid token: '$'","notify_result_group":null} | 是 | 仅支持投递日志。 |
Silent | 当前告警通知是否被屏蔽 | 全部通知均被屏蔽:SilentAll 部分通知被屏蔽:SilentPartial 未被屏蔽:NotSilent | 是 | 仅支持投递日志。 |
相关说明如下:
{{.QueryResult}}
含义:执行语句执行结果(无论是否满足触发条件)。
说明:数组结构,
{{.QueryResult[0]}}
代表第1个执行语句的执行结果,{{.QueryResult[1]}}
代表第2个执行语句的执行结果,以此类推。每个执行语句最多1000条数据、最大10KB,最多10个执行语句,总计最大100KB,超出限制后将自动截断。变量值示例:
假设告警策略存在两条执行语句:
第1个执行语句:status:>=400 | select count(*) as errorLogCount第2个执行语句:status:>=400 | select count(*) as errorLogCount,request_uri group by request_uri order by count(*) desc
则变量值为:
[[{"errorLogCount": 7}],[{"errorLogCount": 3,"request_uri": "/apple-touch-icon-144x144.png"}, {"errorLogCount": 3,"request_uri": "/feed"}, {"errorLogCount": 1,"request_uri": "/opt/node_apps/test-v5/app/themes/basic/public/static/404.html"}]]
{{.TriggerResult}}
含义:满足触发条件的执行语句执行结果。
说明:数组结构,
{{.TriggerResult[0]}}
代表第1个执行语句的执行结果,{{.TriggerResult[1]}}
代表第2个执行语句的执行结果,以此类推。每个执行语句最多1000条数据、最大10KB,最多10个执行语句,总计最大100KB,超出限制后将自动截断。变量值示例:
在上文的 {{.QueryResult}} 变量值示例的基础上,若触发条件为
$1.errorLogCount > 1
。 则变量值为:[[{"errorLogCount": 7}],[{"errorLogCount": 3,"request_uri": "/apple-touch-icon-144x144.png"}, {"errorLogCount": 3,"request_uri": "/feed"}]]
{{.QueryLog}}
含义:执行语句中检索条件匹配到的详细日志(不包含 SQL 中的过滤条件)。
说明:数组结构,
{{.QueryLog[0]}}
代表第1个执行语句的详细日志,{{.QueryLog[1]}}
代表第2个执行语句的详细日志,以此类推。每个执行语句最多10条数据、最大50KB,最多10个执行语句,总计最大150KB,超出限制后将自动截断。变量值示例:
[[{"content": {"__TAG__": {"pod": "nginxPod","cluster": "testCluster"},"body_bytes_sent": "32847","http_referer": "-","http_user_agent": "Opera/9.80 (Windows NT 6.1; U; en-US) Presto/2.7.62 Version/11.01","remote_addr": "105.86.148.186","remote_user": "-","request_method": "GET","request_uri": "/apple-touch-icon-144x144.png","status": "404","time_local": "01/Nov/2018:00:55:14"},"fileName": "/root/testLog/nginx.log","pkg_id": "285A243662909DE3-5CD","source": "172.17.0.2","time": 1653739000013,"topicId": "a54de372-ffe0-49ae-a12e-c340bb2b03f2"}, {"content": {"__TAG__": {"pod": "nginxPod","cluster": "testCluster"},"body_bytes_sent": "33496","http_referer": "-","http_user_agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.93 Safari/537.36","remote_addr": "222.18.168.242","remote_user": "-","request_method": "GET","request_uri": "/opt/node_apps/test-v5/app/themes/basic/public/static/404.html","status": "404","time_local": "01/Nov/2018:00:54:37"},"fileName": "/root/testLog/nginx.log","pkg_id": "285A243662909DE3-5C8","source": "172.17.0.2","time": 1653738975008,"topicId": "a54de372-ffe0-49ae-a12e-c340bb2b03f2"}]]
{{.AnalysisResult}}
含义:多维分析结果。
说明:对象结构,第一层级对象分别对应每个多维分析的结果。一级对象的 key 为多维分析的名称,value 为该多维分析的结果。仅告警触发(即{{.NotifyType}}=1)时具备该变量,告警恢复(即{{.NotifyType}}=2)时无该变量。每个多维分析最多10KB,最多5个多维分析,总计最大50KB。超出限制后将自动截断
相关原始日志:每个语句最多10条数据,以用户配置为准。
字段 Top5及占比统计:每个语句最多5条数据。
自定义检索分析(SQL):每个语句最多1000条数据。
自定义检索分析(检索):每个语句最多10条数据。
变量值示例:
根据多维分析类型,变量值格式有所区别,示例分别如下。
{"详细日志": [{"LogParseFailure": "I0911 11:30:28.235387 1 leaderelection.go:273] successfully renewed lease kube-system/external-attacher-leader-com-tencent-cloud-csi-cbs","__FILENAME__": "/var/log/tke-log-agent/test-log9-8/stdout-containerd/csi-cbs-controller-58d8c6f79f-6f48w_kube-system_csi-attacher-100034c83b26d21a6cca51ba53ebc36e81f5e6eec04165478ec3a848d105e042.log","__HOSTNAME__": "VM-0-38-centos","__INDEX_STATUS__": "","__PKG_LOGID__": "7C6A04E2682AF44F-D71A","__SOURCE__": "10.0.0.38","__TAG__.cluster_id": "cls-4mhq30b8","__TAG__.container_id": "100034c83b26d21a6cca51ba53ebc36e81f5e6eec04165478ec3a848d105e042","__TAG__.container_name": "csi-attacher","__TAG__.image_name": "ccr.ccs.tencentyun.com/tkeimages/csi-attacher:v3.0.3","__TAG__.namespace": "kube-system","__TAG__.pod_ip": "10.0.0.32","__TAG__.pod_label_app": "cbs-csi-controller","__TAG__.pod_label_metrics": "cbs-csi-controller","__TAG__.pod_label_pod-template-hash": "58d8c6f79f","__TAG__.pod_name": "csi-cbs-controller-58d8c6f79f-6f48w","__TAG__.pod_uid": "ab4f7b63-2cab-442c-a532-125e767d83ec","__TAG__.stream": "stderr","__TIMESTAMP__": "1757590229046"}]}
其中
详细日志
为告警策略中配置的多维分析名称。{"Top URL": [{"count": 77,"ratio": 0.45294117647058824,"value": "/"}, {"count": 20,"ratio": 0.11764705882352941,"value": "/favicon.ico"}, {"count": 7,"ratio": 0.041176470588235294,"value": "/blog/feed"}, {"count": 5,"ratio": 0.029411764705882353,"value": "/test-tile-service"}, {"count": 3,"ratio": 0.01764705882352941,"value": "/android-chrome-192x192.png"}]}
其中
Top URL
为告警策略中配置的多维分析名称。{"详细错误日志": [{"content": {"__TAG__": {"pod": "nginxPod","cluster": "testCluster"},"body_bytes_sent": "32847","http_referer": "-","http_user_agent": "Opera/9.80 (Windows NT 6.1; U; en-US) Presto/2.7.62 Version/11.01","remote_addr": "105.86.148.186","remote_user": "-","request_method": "GET","request_uri": "/apple-touch-icon-144x144.png","status": "404","time_local": "01/Nov/2018:00:55:14"},"fileName": "/root/testLog/nginx.log","pkg_id": "285A243662909DE3-5CD","source": "172.17.0.2","time": 1653739000013,"topicId": "a54de372-ffe0-49ae-a12e-c340bb2b03f2"}, {"content": {"__TAG__": {"pod": "nginxPod","cluster": "testCluster"},"body_bytes_sent": "33496","http_referer": "-","http_user_agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.93 Safari/537.36","remote_addr": "222.18.168.242","remote_user": "-","request_method": "GET","request_uri": "/opt/node_apps/test-v5/app/themes/basic/public/static/404.html","status": "404","time_local": "01/Nov/2018:00:54:37"},"fileName": "/root/testLog/nginx.log","pkg_id": "285A243662909DE3-5C8","source": "172.17.0.2","time": 1653738975008,"topicId": "a54de372-ffe0-49ae-a12e-c340bb2b03f2"}]}
其中
详细错误日志
为告警策略中配置的多维分析名称。{"错误日志URL分布": [{"errorLogCount": 3,"request_uri": "/apple-touch-icon-144x144.png"}, {"errorLogCount": 3,"request_uri": "/feed"}, {"errorLogCount": 1,"request_uri": "/opt/node_apps/test-v5/app/themes/basic/public/static/404.html"}]}
其中
错误日志URL分布
为告警策略中配置的多维分析名称。变量语法
变量语法类似 Go Template 语法,可对告警通知变量进行提取和格式化处理,以更加清晰地呈现在告警通知内容中。所有变量及变量语法均位于
{{ }}
中,外部的文本不会进行处理。变量提取
语法格式
{{.variable[x]}} 或 {{index .variable x}}{{.variable.childNodeName}} 或 {{index .variable "childNodeName"}}
语法说明
变量为数组时,使用
{{.variable[x]}}
按数组下标提取对应的数组元素,其中 x 为大于等于0的整数,等价于 {{index .variable x}}
。变量为对象时,使用
{{.variable.childNodeKey}}
按子级对象名称(key)提取对应的子级对象值(value),等价于 {{index .variable "childNodeName"}}
。注意:
子级对象名称包含空格、
$
、-
等特殊符号或为纯数字时,请使用 {{index .variable "childNodeName"}}
形式的语法,例如 {{index .AnalysisResult "Top URL"}}
、{{index .ConditionGroup "$1.AppName"}}
、{{index .variable "123"}}
。使用示例
{{.QueryResult}}
变量值为:[[{"errorLogCount": 7 //提取该值}],[{"errorLogCount": 3,"request_uri": "/apple-touch-icon-144x144.png"}, {"errorLogCount": 3,"request_uri": "/feed"}, {"errorLogCount": 1,"request_uri": "/opt/node_apps/test-v5/app/themes/basic/public/static/404.html"}]]
通过以下表达式获取第一个数组中的 errorLogCount 的值:
{{.QueryResult[0][0].errorLogCount}}
返回结果:
7
循环遍历
语法格式
{{range .variable}}自定义内容{{.childNode1}}自定义内容{{.childNode2}}...{{end}}
或
{{range $key,$value := .variable}}自定义内容{{$key}}自定义内容{{$value}}...{{end}}
语法说明
变量为数组或包含多个子级对象的对象时,可使用该语法将其中的每个元素/对象按指定格式展示出来。
注意:
在
range
语法内部,默认获取的是.variable
内部的元素/对象,不能直接使用{{.UIN}}
的形式获取外部变量,如需获取请在range
语法外部提前创建独立的变量,例如:{{$UIN := .UIN}} //创建变量{{range .variable}}自定义内容{{.childNode1}}自定义内容{{.childNode2}}...{{$UIN}} //使用变量{{end}}
使用示例
{{.QueryResult}}
变量值为:[[{"errorLogCount": 7}],[{"errorLogCount": 3,"request_uri": "/apple-touch-icon-144x144.png"}, {"errorLogCount": 3,"request_uri": "/feed"}, {"errorLogCount": 1,"request_uri": "/opt/node_apps/test-v5/app/themes/basic/public/static/404.html"}]]
通过以下表达式分别展示第二个数组中每个 request_uri 对应的 errorLogCount:
{{range .QueryResult[1]}}* {{.request_uri}}错误日志数:{{.errorLogCount}}{{end}}
返回结果:
* /apple-touch-icon-144x144.png错误日志数:3* /feed错误日志数:3* /opt/node_apps/test-v5/app/themes/basic/public/static/404.html错误日志数:1
条件判断
语法格式
{{if boolen}}xxx{{end}}
或
{{if boolen}}xxx{{else}}xxx{{end}}
或
{{if boolen}}xxx{{else if boolen}}xxx{{end}}
语法说明
根据条件判断结果分别执行对应的表达式,条件判断支持使用 and or not 进行逻辑运算,支持比较大小值。
eq arg1 arg2 : arg1 == arg2时为truene arg1 arg2 : arg1 != arg2时为truelt arg1 arg2 : arg1 < arg2时为truele arg1 arg2 : arg1 <= arg2时为truegt arg1 arg2 : arg1 > arg2时为truege arg1 arg2 : arg1 >= arg2时为true
使用示例
{{.QueryResult}}
变量值为:[[{"errorLogCount": 7}],[{"errorLogCount": 3,"request_uri": "/apple-touch-icon-144x144.png"}, {"errorLogCount": 3,"request_uri": "/feed"}, {"errorLogCount": 1,"request_uri": "/opt/node_apps/test-v5/app/themes/basic/public/static/404.html"}]]
通过以下表达式分别展示第二个数组中 errorLogCount 大于等于2且小于等于100的 request_uri 及对应的 errorLogCount:
{{range .QueryResult[1]}}{{if and (ge .errorLogCount 2) (le .errorLogCount 100)}}* {{.request_uri}}错误日志数:{{.errorLogCount}}{{end}}{{end}}
返回结果:
* /apple-touch-icon-144x144.png错误日志数:3* /feed错误日志数:3
使用 if 还可以判断字段值是否存在,字段值为空白字符串或不存在时,相当于 false。例如:
{{if .QueryLog[0][0].apple}}apple exist, value is : {{.QueryLog[0][0].apple}}{{else}}apple is not exist{{end}}
移除空白符号
语法格式
{{- xxx}} 或 {{xxx -}}
语法说明
变量语法在执行过程中会保留语法本身带来的空格、缩进和换行等空白符号,例如循环迭代和条件判断使用示例中的返回结果中包含较多空白行,影响展示效果,此时可在
{{ }}
的头部或尾部使用-
移除其前面或后面的空白符号。使用示例
沿用条件判断中的示例,表达式修改为:
{{- range .QueryResult[1]}}{{- if and (ge .errorLogCount 2) (le .errorLogCount 100)}}* {{.request_uri}}错误日志数:{{.errorLogCount}}{{- end}}{{- end}}
返回结果:
* /apple-touch-icon-144x144.png错误日志数:3* /feed错误日志数:3
变量函数
字符串函数
函数 | 说明 | 示例 | |
转义特殊字符 | {{escape .variable}} | 将 " 等特殊符号进行转义,以便于将变量放在 json 中,保持 json 格式正确。 | 变量值: {{.ExecuteQuery[0]}} 的变量值为 status:>=400 | select count(*) as "错误日志数" 函数示例: {{escape .ExecuteQuery[0]}} 返回结果: status:>=400 | select count(*) as \\"错误日志数\\" |
| {{escape_markdown .variable}} | 将 * # 等特殊符号进行转义,以便于将变量放在 markdown 中,保持 markdown 格式正确。适用于企业微信机器人通知渠道。 | |
| {{escape_markdown_html .variable}} | 将 * # 等特殊符号进行转义,以便于将变量放在 markdown 中,保持 markdown 格式正确。适用于钉钉及飞书机器人通知渠道。 | |
截取字符串 | {{substr .variable start}} | 按指定起始位置和长度(可选)对字符串进行截取,起始位置及长度均按照字节数计算,一般英文字母及数字占1个字节,中文占3 - 4个字节。 | 变量值: {{.QueryLog[0][0].fileName}} 的变量值为/root/testLog/nginx.log 函数示例: {{substr .QueryLog[0][0].fileName 6 7 }} 返回结果: testLog |
| {{substr .variable start length}} | | |
| {{extract .variable startStringRegex}} | 按指定起始字符和结束字符(可选)对字符串进行截取,起始字符和结束字符通过正则表达式进行指定。 | 变量值: {{.QueryLog[0][0].fileName}} 的变量值为/root/testLog/nginx.log 函数示例: {{extract .QueryLog[0][0].fileName "/root/" "/nginx"}} 返回结果: testLog |
| {{extract .variable startStringRegex endStringRegex}} | | |
| {{trim .variable}} | 删除原始字符串开头和末尾的空字符。 | 函数示例: {{trim " Hello, World! "}} 返回结果: Hello, World! |
| {{trimPrefix prefix .variable}} | 删除原始字符串指定前缀。 | 函数示例: {{trimPrefix "hello" "hello, world!"}} 返回结果: , world! |
| {{trimSuffix suffix .variable}} | 删除原始字符串指定后缀。 | 函数示例: {{trimSuffix "world!" "hello, world!"}} 返回结果: hello, |
大小写转换 | {{lower .variable}} | 将原始字符串中所有字符转换为小写形式。 | 函数示例: {{lower "HELLO WORLD"}} 返回结果: hello world |
| {{upper .variable}} | 将原始字符串中所有字符转换为大写形式。 | 函数示例: {{upper "hello world"}} 返回结果: HELLO WORLD |
| {{title .variable}} | 将原始字符串进行标题化处理,即每个单词的首字母大写,其余字母小写。 | 函数示例: {{title "hello world"}} 返回结果: Hello World |
字符串替换 | {{replace(oldstr,newstr,originstr)}} | 将指定字符串 originstr 中的某个子字符串oldstr 替换为另一个子字符串newstr 。 | 函数示例: {{replace "foo" "bar" "foo bar foo"}} 返回结果: bar bar bar |
| {{regexReplaceAll(regex,originstr,newstr)}} | 将指定字符串 originstr 中匹配正则表达式regex 的字符串替换为指定字符串newstr 。 | 函数示例: {{regexReplaceAll "h" "hello world" "H"}} 返回结果: Hello world |
字符串匹配 | {{containstr .variable searchstring}} | 变量值中是否包含指定的字符串,判断结果可用于条件判断语法中。 | 变量值: {{.QueryLog[0][0].fileName}} 的变量值为/root/testLog/nginx.log 函数示例: {{if containstr .QueryLog[0][0].fileName "test"}}测试日志{{else}}非测试日志{{end}} 返回结果: 测试日志 |
| {{regexMatch(regex,string)}} | 判断指定字符串和正则表达式是否匹配。匹配则返回 true,否则返回 false。 | 函数示例: {{regexMatch "^hello" "hello world"}} 返回结果: true |
字符串拼接 | {{concat .variable1 .variable2 ...}} | 将指定的多个变量或字符串拼接在一起。 | 函数示例: {{concat .Region .Alarm}} 返回结果: 广州Nginx 错误日志过多 |
字符串分割 | {{splitList delimiter string}} | 将字符串 string 按照分隔符delimiter 切分为数组。 | 函数示例: {{splitList "," "apple,banana,orange"}} 返回结果: ["apple" "banana" "orange"] |
字符串排序 | {{sortAlpha stringList}} | 将字符串列表按字母顺序。 它不会原地排序,而是返回一个排序后的列表副本,符合列表不可变性的原则。 | 函数示例: {{sortAlpha (splitList "," "banana,apple,orange")}} 返回结果: ["apple","banana","orange"] |
统计长度或元素个数 | {{len .variable}} | 获取数组、map 数据结构的元素个数,也可获取字符串的长度,不支持获取数值的长度。 | 变量值: {{.QueryResult}} 变量值为[[{"number":19778,"text":"hello"}],[]] 函数示例:
返回结果:
|
数学计算函数
函数 | 说明 | 示例 | |
加法 | {{add .variable1 .variable2 ...}} | 整数(int64)加法运算。 | 函数示例: {{ add 5 3 }} 返回结果: 8 |
| {{addf .variable1 .variable2 ...}} | 浮点数(float64)加法运算。 | |
减法 | {{sub .variable1 .variable2}} | 整数(int64)减法运算。 | 函数示例: {{ sub 10 3 }} 返回结果: 7 |
| {{subf .variable1 .variable2}} | 浮点数(float64)减法运算。 | |
除法 | {{div .variable1 .variable2}} | 整数(int64)除法运算。 | 函数示例: {{ div 10 2 }} 返回结果: 5 |
| {{divf .variable1 .variable2}} | 浮点数(float64)除法运算。 | |
取模 | {{mod .variable1 .variable2}} | 整数(int64)取模运算。 | 函数示例: {{ mod 10 3 }} 返回结果: 1 |
乘法 | {{mul .variable1 .variable2}} | 整数(int64)乘法运算。 | 函数示例: {{ mul 4 5 }} 返回结果: 20 |
| {{mulf .variable1 .variable2}} | 浮点数(float64)乘法运算。 | |
最小值 | {{min .variable1 .variable2 ...}} | 整数(int64)中获取最小值。 | 函数示例: {{ min 3 7 }} 返回结果: 3 |
| {{minf .variable1 .variable2 ...}} | 浮点数(float64)中获取最小值。 | |
最大值 | {{max .variable1 .variable2 ...}} | 整数(int64)中获取最大值。 | 函数示例: {{ max 3 7 }} 返回结果: 7 |
| {{maxf .variable1 .variable2 ...}} | 浮点数(float64)中获取最大值。 | |
取整 | {{ceil .variable}} | 向上取整。 | 函数示例: {{ ceil 5.3 }} 返回结果: 6 |
| {{floor .variable}} | 向下取整。 | |
四舍五入 | {{round .variable decimal}} | 对数值 variable 进行四舍五入,保留指定小数位decimal 。 | 函数示例: {{ round 5.6748 2 }} 返回结果: 5.67 |
类型转换函数
函数 | 说明 | 示例 |
{{atoi .variable}} | 将字符串转换为整数。 | 函数示例:
返回结果:
|
{{float64 .variable}} | 转换为 float64类型。 | |
{{int .variable}} | 转换为与系统宽度匹配的 int 类型。 | |
{{int64 .variable}} | 转换为 int64类型。 | |
{{toString .variable}} | 转换为字符串。 | |
时间函数
函数 | 说明 | 示例 | |
UNIX 时间戳转换 | {{fromUnixTime .variable}} | 将 UNIX 时间戳(支持毫秒及秒级时间戳)转换为可读的日期时间。其中时区可选,默认为 Asia/Shanghai。 | 变量值: {{.QueryLog[0][0].time}} 的变量值为1653893435008 函数示例:
返回结果:
|
| {{fromUnixTime .variable "timezone"}} | | |
时间格式化 | {{date "2006-01-02 15:04:05" .variable}} | 将指定时间戳转换为指定格式的字符串,第一个参数表示时间格式,以 2006-01-02 15:04:05 为基础,可调整格式。 | 函数示例:
返回结果:
说明:.NotifyTimeUnix 为毫秒时间戳,使用 div 除以 1000 后转换为秒时间戳。 |
| {{dateInZone "2006-01-02 15:04:05" .variable timezone}} | 与 date 函数功能类似,可指定时区。 | |
| {{duration .variable}} | 将持续时间(秒)转换为人眼易读的格式。 | 函数示例: {{duration (mul .Duration 60)}} 返回结果: 4h23m0s 说明:.Duration 为告警持续时间,单位分钟,使用 mul 乘以60后转换为秒。 |
JSON 格式化函数
函数 | 说明 | 示例 |
{{toPrettyJson .variable}} | 将指定参数转换为格式化的 JSON 字符串,便于展示。 | 函数示例: {{toPrettyJson .TriggerResult}} 返回结果:
|
编解码/加密函数
函数 | 说明 | 示例 |
{{base64_encode .variable}} {{base64_decode .variable}} {{base64url_encode .variable}} {{base64url_decode .variable}} {{url_encode .variable}} {{url_decode .variable}} | 对指定的变量或字符串进行编码(encode)或解码(decode),其中 base64url 在编解码过程中不会去除或补充尾部的"="。 | 函数示例:
返回结果:
|
{{md5 .variable}} {{md5 .variable | base64_encode}} {{md5 .variable | base64url_encode}} {{sha1 .variable}} {{sha1 .variable | base64_encode}} {{sha1 .variable | base64url_encode}} {{sha256 .variable}} {{sha256 .variable | base64_encode}} {{sha256 .variable | base64url_encode}} {{sha512 .variable}} {{sha512 .variable | base64_encode}} {{sha512 .variable | base64url_encode}} | 对指定的变量或字符串按照特定的加密算法进行加密,默认返回结果编码格式为16进制字符串,可按需修改为 base64或 base64url 编码格式。 | 函数示例:
返回结果:
|
{{hmac_md5 .variable "Secretkey"}} {{hmac_md5 .variable "Secretkey" | base64_encode}} {{hmac_md5 .variable "Secretkey" | base64url_encode}} {{hmac_sha1 .variable "Secretkey"}} {{hmac_sha1 .variable "Secretkey" | base64_encode}} {{hmac_sha1 .variable "Secretkey" | base64url_encode}} {{hmac_sha256 .variable "Secretkey"}} {{hmac_sha256 .variable "Secretkey" | base64_encode}} {{hmac_sha256 .variable "Secretkey" | base64url_encode}} {{hmac_sha512 .variable "Secretkey"}} {{hmac_sha512 .variable "Secretkey" | base64_encode}} {{hmac_sha512 .variable "Secretkey" | base64url_encode}} | 对指定的变量或字符串按照特定的加密算法进行加密,默认返回结果编码格式为16进制字符串,可按需修改为 base64或 base64url 编码格式。其中 Secretkey 为 hmac 加密算法中的密钥,可按需进行修改。 | 函数示例:
返回结果:
|
参考案例
案例1:在告警通知中展示最近一条详细日志
需求场景
将符合执行语句检索条件的最近一条详细日志添加至告警通知中,以 key:value 的形式展示,每行一个 key,不包含 CLS 预置字段和元数据字段。
通知内容配置
{{range $key,$value := .QueryLog[0][0].content}}{{if not (containstr $key "__TAG__")}}{{- $key}}:{{$value}}{{- end}}{{- end}}
其中
.QueryLog[0][0]
代表符合告警策略第一条执行语句检索条件的最近一条详细日志,其值为:{"content": {"__TAG__": {"a": "b12fgfe","c": "fgerhcdhgj"},"body_bytes_sent": "33704","http_referer": "-","http_user_agent": "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.3319.102 Safari/537.36","remote_addr": "247.0.249.191","remote_user": "-","request_method": "GET","request_uri": "/products/hadoop)","status": "404","time_local": "01/Nov/2018:07:54:08"},"fileName": "/root/testLog/nginx.log","pkg_id": "285A243662909DE3-210B","source": "172.17.0.2","time": 1653908859008,"topicId": "a54de372-ffe0-49ae-a12e-c340bb2b03f2"}
告警通知内容
remote_addr:247.0.249.191time_local:01/Nov/2018:07:54:08http_user_agent:Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.3319.102 Safari/537.36remote_user:-http_referer:-body_bytes_sent:33704request_method:GETrequest_uri:/products/hadoop)status:404
案例2:在告警通知中展示多个执行语句对应的详细日志
需求场景
告警策略具备多个执行语句时,将每个语句中对应的详细日志添加至告警通知中。
通知内容配置
{{- range .QueryLog}}{{- range .}}{{.content}}{{- end}}{{- end}}
其中
.QueryLog
代表符合告警策略所有执行语句检索条件的详细日志(最多不超过10条),其值为:{"QueryLog": [[{"content": {"__TAG__": {"pod": "nginxPod","cluster": "testCluster"},"body_bytes_sent": "32847","http_referer": "-","http_user_agent": "Opera/9.80 (Windows NT 6.1; U; en-US) Presto/2.7.62 Version/11.01","remote_addr": "105.86.148.186","remote_user": "-","request_method": "GET","request_uri": "/apple-touch-icon-144x144.png","status": "404","time_local": "01/Nov/2018:00:55:14"},"fileName": "/root/testLog/nginx.log","pkg_id": "285A243662909DE3-5CD","source": "172.17.0.2","time": 1653739000013,"topicId": "a54de372-ffe0-49ae-a12e-c340bb2b03f2"}, {"content": {"__TAG__": {"pod": "nginxPod","cluster": "testCluster"},"body_bytes_sent": "33496","http_referer": "-","http_user_agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.93 Safari/537.36","remote_addr": "222.18.168.242","remote_user": "-","request_method": "GET","request_uri": "/opt/node_apps/test-v5/app/themes/basic/public/static/404.html","status": "404","time_local": "01/Nov/2018:00:54:37"},"fileName": "/root/testLog/nginx.log","pkg_id": "285A243662909DE3-5C8","source": "172.17.0.2","time": 1653738975008,"topicId": "a54de372-ffe0-49ae-a12e-c340bb2b03f2"}],[{"content": {"__TAG__": {"pod": "nginxPod","cluster": "testCluster"},"body_bytes_sent": "32847","http_referer": "-","http_user_agent": "Opera/9.80 (Windows NT 6.1; U; en-US) Presto/2.7.62 Version/11.01","remote_addr": "105.86.148.186","remote_user": "-","request_method": "GET","request_uri": "/apple-touch-icon-144x144.png","status": "404","time_local": "01/Nov/2018:00:55:14"},"fileName": "/root/testLog/nginx.log","pkg_id": "285A243662909DE3-5CD","source": "172.17.0.2","time": 1653739000013,"topicId": "a54de372-ffe0-49ae-a12e-c340bb2b03f2"}, {"content": {"__TAG__": {"pod": "nginxPod","cluster": "testCluster"},"body_bytes_sent": "33496","http_referer": "-","http_user_agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.93 Safari/537.36","remote_addr": "222.18.168.242","remote_user": "-","request_method": "GET","request_uri": "/opt/node_apps/test-v5/app/themes/basic/public/static/404.html","status": "404","time_local": "01/Nov/2018:00:54:37"},"fileName": "/root/testLog/nginx.log","pkg_id": "285A243662909DE3-5C8","source": "172.17.0.2","time": 1653738975008,"topicId": "a54de372-ffe0-49ae-a12e-c340bb2b03f2"}]]}
告警通知内容
{"__TAG__":{"pod":"nginxPod","cluster":"testCluster"},"body_bytes_sent":"32847","http_referer":"-","http_user_agent":"Opera/9.80 (Windows NT 6.1; U; en-US) Presto/2.7.62 Version/11.01","remote_addr":"105.86.148.186","remote_user":"-","request_method":"GET","request_uri":"/apple-touch-icon-144x144.png","status":"404","time_local":"01/Nov/2018:00:55:14"},{"__TAG__":{"pod":"nginxPod","cluster":"testCluster"},"body_bytes_sent":"33496","http_referer":"-","http_user_agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.93 Safari/537.36","remote_addr":"222.18.168.242","remote_user":"-","request_method":"GET","request_uri":"/opt/node_apps/test-v5/app/themes/basic/public/static/404.html","status":"404","time_local":"01/Nov/2018:00:54:37"},{"__TAG__":{"pod":"nginxPod","cluster":"testCluster"},"body_bytes_sent":"32847","http_referer":"-","http_user_agent":"Opera/9.80 (Windows NT 6.1; U; en-US) Presto/2.7.62 Version/11.01","remote_addr":"105.86.148.186","remote_user":"-","request_method":"GET","request_uri":"/apple-touch-icon-144x144.png","status":"404","time_local":"01/Nov/2018:00:55:14"},{"__TAG__":{"pod":"nginxPod","cluster":"testCluster"},"body_bytes_sent":"33496","http_referer":"-","http_user_agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.93 Safari/537.36","remote_addr":"222.18.168.242","remote_user":"-","request_method":"GET","request_uri":"/opt/node_apps/test-v5/app/themes/basic/public/static/404.html","status":"404","time_local":"01/Nov/2018:00:54:37"}
案例3:在告警通知中展示执行语句执行结果
需求场景
将执行语句执行结果中符合触发条件的部分添加至告警通知中,以列表的形式展示。
告警策略执行语句为:
status:>=400 | select count(*) as errorLogCount,request_uri group by request_uri order by count(*) desc
触发条件为:$1.errorLogCount > 10
通知内容配置
{{range .QueryResult[0]}}{{- if gt .errorLogCount 10}}{{.request_uri}}错误日志数:{{.errorLogCount}}{{- end}}{{- end}}
其中
.QueryResult[0]
代表告警策略第一条执行语句的执行结果,其值为:[{"errorLogCount": 161,"request_uri": "/apple-touch-icon-144x144.png"}, {"errorLogCount": 86,"request_uri": "/opt/node_apps/test-v5/app/themes/basic/public/static/404.html"}, {"errorLogCount": 33,"request_uri": "/feed"}, {"errorLogCount": 26,"request_uri": "/wp-login.php"}, {"errorLogCount": 10,"request_uri": "/safari-pinned-tab.svg"}, {"errorLogCount": 7,"request_uri": "/mstile-144x144.png"}, {"errorLogCount": 4,"request_uri": "/atom.xml"}, {"errorLogCount": 3,"request_uri": "/content/plugins/prettify-gc-syntax-highlighter/launch.js?ver=3.5.2?ver=3.5.2"}]
告警通知内容
/apple-touch-icon-144x144.png错误日志数:161/opt/node_apps/elastic-v5/app/themes/basic/public/static/404.html错误日志数:86/feed错误日志数:33/wp-login.php错误日志数:26
案例4:在多维分析中使用执行语句执行结果
需求场景
告警策略中配置多维分析时,需将执行语句的结果中的某个字段的值作为多维分析的过滤条件。
告警策略配置

其中
{{.QueryResult[0][0].host}}
代表第一条执行语句执行结果第一行中的host
。注意:
变量值为字符串时,请使用引号包裹以符合检索分析语句规则。检索条件中使用双引号包裹,例如
"{{.QueryResult[0][0].host}}",
SQL 中需使用单引号包裹,例如'{{.QueryResult[0][0].host}}'
。