【Azure Logic App】使用Logic App来定制Monitor Alert邮件内容遇见无法获取SearchResults的情况
问题描述
在使用Azure Monitor获取自定义告警指标并发出告警邮件时,默认的告警邮件内容不满足需要,需要把自定义查询语句的结果也直接显示在邮件中。
查阅官方文档(Customize alert notifications by using Logic Apps),可以通过Logic App来自定义Alert邮件内容。
但是,在实验中,遇见了内容为空的情况!在获取内容部分,使用的配置是 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. ):
所以 SearchResults 中不包含Alert 内容是设计使然。如果想获取结果,需要通过或HTTP请求去获取 LinkToSearchResultsAPI 的返回值。
在Logic App中需要进行的操作是,添加一个变量 ResultURL 和 HTTP 操作。
变量 ResultURL 的值设置为:string(triggerBody()?['data']?['alertContext']?['condition']?['allOf'][0]?['LinkToSearchResultsAPI'])
第一步添加变量 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
当在复杂的环境中面临问题,格物之道需:浊而静之徐清,安以动之徐生。 云中,恰是如此!
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· NetPad:一个.NET开源、跨平台的C#编辑器
· PowerShell开发游戏 · 打蜜蜂
2023-12-18 【Azure App Service】当App Service中使用系统标识无法获取Access Token时