【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

 

 

posted @   路边两盏灯  阅读(9)  评论(2编辑  收藏  举报
相关博文:
阅读排行:
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· NetPad:一个.NET开源、跨平台的C#编辑器
· PowerShell开发游戏 · 打蜜蜂
历史上的今天:
2023-12-18 【Azure App Service】当App Service中使用系统标识无法获取Access Token时
点击右上角即可分享
微信分享提示