【问题标题】:$type in AlertRule Condition in ARM TemplateARM 模板中的 AlertRule 条件中的 $type
【发布时间】:2019-04-04 12:09:17
【问题描述】:

我是 ARM 模板的新手,我正在开发一个模板以将 Web 测试和警报规则部署到应用程序洞察实例。应用洞察是使用 Terraform 创建和维护的,但 Terraform 尚不正确支持警报规则和 Web 测试。我使用的示例取自 this Github issue,但我们现在想将其分离到 Azure DevOps 管道中的一个 ARM 步骤,因为我们需要获取 webhook 警报的 URL,而我们在 Terraform 脚本中无法轻松做到这一点。

我一直在清理此示例的内容,并使用this quickstart template 作为参考点。但是,我在快速入门示例中注意到了一些在我的示例中没有的内容,而且我似乎找不到任何相关信息。

在警报规则定义属性对象中,条件对象具有odata.type,关联的dataSource 具有odata.type,如下所示:

"properties": {
    "name": "[parameters('test').name]",
    "description": "[parameters('test').description]",
    "isEnabled": true,
    "condition": {
        "odata.type": "Microsoft.Azure.Management.Insights.Models.LocationThresholdRuleCondition",
        "dataSource": {
            "odata.type": "Microsoft.Azure.Management.Insights.Models.RuleMetricDataSource",
            "resourceUri": "[resourceId('microsoft.insights/webtests/', parameters('test').name)]",
            "metricName": "GSMT_AvRaW"
         },
         "windowSize": "PT5M",
         "failedLocationCount": "[parameters('test').failedLocationCount]"
},

快速入门示例几乎相同,但在 odata.type 条目之前是 $type 条目,如下所示:

"properties": {
    "name": "[parameters('tests')[copyIndex(1)].name]",
    "description": "[parameters('tests')[copyIndex(1)].description]",
    "isEnabled": true,
    "condition": {
      "$type": "Microsoft.WindowsAzure.Management.Monitoring.Alerts.Models.LocationThresholdRuleCondition, Microsoft.WindowsAzure.Management.Mon.Client",
      "odata.type": "Microsoft.Azure.Management.Insights.Models.LocationThresholdRuleCondition",
      "dataSource": {
        "$type": "Microsoft.WindowsAzure.Management.Monitoring.Alerts.Models.RuleMetricDataSource, Microsoft.WindowsAzure.Management.Mon.Client",
        "odata.type": "Microsoft.Azure.Management.Insights.Models.RuleMetricDataSource",
        "resourceUri": "[resourceId('microsoft.insights/webtests/', parameters('tests')[copyIndex()].name)]",
        "metricName": "GSMT_AvRaW"
      },
      "windowSize": "PT15M",
      "failedLocationCount": "[parameters('tests')[copyIndex(1)].failedLocationCount]"
    },

我的模板似乎仍然可以工作,但我应该包含$type 属性吗?还是它们取决于用例?我在找到这方面的任何文档方面没有取得很大成功。

【问题讨论】:

    标签: azure templates azure-application-insights azure-resource-manager arm-template


    【解决方案1】:

    我发现了一个可能的错误,如果它没有在 Action 和条件中提到 $type,如下所示:

    "condition": {
          "$type": "Microsoft.WindowsAzure.Management.Monitoring.Alerts.Models.ThresholdRuleCondition, Microsoft.WindowsAzure.Management.Mon.Client",
          "odata.type": "Microsoft.Azure.Management.Insights.Models.ThresholdRuleCondition",
          "dataSource": {
            "$type": "Microsoft.WindowsAzure.Management.Monitoring.Alerts.Models.RuleMetricDataSource, Microsoft.WindowsAzure.Management.Mon.Client",
            "odata.type": "Microsoft.Azure.Management.Insights.Models.RuleMetricDataSource",
            "resourceUri": "[resourceId('microsoft.insights/components', variables('appInsName'))]",
            "metricName": "request.duration"
          },
          "threshold": "[parameters('responseTime')]",
          "windowSize": "PT5M"
        },
        "actions": [
          {
            "$type": "Microsoft.WindowsAzure.Management.Monitoring.Alerts.Models.RuleEmailAction, Microsoft.WindowsAzure.Management.Mon.Client",
            "odata.type": "Microsoft.Azure.Management.Insights.Models.RuleEmailAction",
            "sendToServiceOwners": true,
            "customEmails": []
          }
    

    运行此脚本时出错:

    New-AzureRmResourceGroupDeployment -Verbose -ResourceGroupName rg.test.ARM -TemplateFile azuredeploy.json -TemplateParameterFile azuredeploy.parameters.json -DeploymentDebugLogLevel All
    

    错误消息:错误 - 此警报 CPU default-app-name-plan 没有目标资源。

    因此,理想情况下,在从 PowerShell/cli 部署时,应在条件和操作中提供 $type 以避免此错误。

    还有一个类似的问题here

    【讨论】:

    • 我在 Linux 代理上使用 Azure DevOps 管道执行此操作,它部署良好,尽管我遇到了触发警报的问题,但我怀疑错误存在于其他地方。值得注意的是,这是一个 sn-p,而不是整个资源 def,它是较大模板的一部分。如果没有它,它当然不会部署,因为您没有指定要将警报附加到的资源。链接的问题似乎更多地与他们的资源标签有关,而我的问题是正确的。它仍然没有解释 $type 属性实际上是 for 的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多