【Azure Logic App】使用Logic App来定制Monitor Alert邮件内容遇见无法获取SearchResults的情况 - LuBu0505/My-Code GitHub Wiki

问题描述

在使用Azure Monitor获取自定义告警指标并发出告警邮件时,默认的告警邮件内容不满足需要,需要把自定义查询语句的结果也直接显示在邮件中。

查阅官方文档(Customize alert notifications by using Logic Apps),可以通过Logic App来自定义Alert邮件内容。 image.png

但是,在实验中,遇见了内容为空的情况!在获取内容部分,使用的配置是 string(triggerBody()?['data']?['alertContext']?['SearchResults']), 如下:

{
 "type": "InitializeVariable",
 "inputs": {
   "variables": [
     {
       "name": "Get-Context",
       "type": "string",
       "value": "@string(triggerBody()?['data']?['alertContext']?['SearchResults'])"
     }
   ]
 },
 "runAfter": {
   "Initialize_variable": [
     "Succeeded"
   ]
 }
}

这是什么情况呢?如何来解决这个问题呢?

问题解答

同样,也需要对比Alert的官方文档,在文档中发现特别说明( the search results aren't embedded in the log search alerts payload. ): image.png

所以 SearchResults 中不包含Alert 内容是设计使然。如果想获取结果,需要通过或HTTP请求去获取 LinkToSearchResultsAPI 的返回值。

在Logic App中需要进行的操作是,添加一个变量 ResultURL 和 HTTP 操作。

变量 ResultURL 的值设置为:string(triggerBody()?['data']?['alertContext']?['condition']?['allOf'][0]?['LinkToSearchResultsAPI']) image.png

 第一步添加变量 ResultURL 的配置代码如下:

{
 "type": "InitializeVariable",
 "inputs": {
   "variables": [
     {
       "name": "ResultURL",
       "type": "string",
       "value": "@string(triggerBody()?['data']?['alertContext']?['condition']?['allOf'][0]?['LinkToSearchResultsAPI'])"
     }
   ]
 },
 "runAfter": {
   "Initialize_variable_1": [
     "SUCCEEDED"
   ]
 }
}

如此调整之后,就可以实现从Alert中获取到告警的详细内容!

参考资料

Customize alert notifications by using Logic Apps : https://learn.microsoft.com/en-us/azure/azure-monitor/alerts/alerts-logic-apps?tabs=send-email

Sample log search alert when the monitoringService = Log Alerts V2 : https://learn.microsoft.com/en-us/azure/azure-monitor/alerts/alerts-common-schema#sample-log-search-alert-when-the-monitoringservice--log-alerts-v2

当在复杂的环境中面临问题,格物之道需:浊而静之徐清,安以动之徐生。 云中,恰是如此!