简体   繁体   English

使用 Terraform(或 ARM 模板)为 Web 测试创建 Azure 警报规则?

[英]Creating an Azure Alert Rule for a Webtest with Terraform (or ARM template)?

I'm trying to create an Alert Rule for an Application Insights → Availability → Standard (preview) test.我正在尝试为 Application Insights → 可用性 → 标准(预览)测试创建警报规则。 First off, how do you create a standard test?首先,您如何创建标准测试? For now, I resorted to creating the resource by using a resource "azurerm_resource_group_template_deployment" … ;现在,我求助于使用resource "azurerm_resource_group_template_deployment" … see gist.github.com/alexs77/0b2bd07aae7fbf22bd3c145132475e7d for exactly how.具体方法请参见gist.github.com/alexs77/0b2bd07aae7fbf22bd3c145132475e7d

But even if it were a azurerm_application_insights_web_test , how would an Alert Rule (ie. azurerm_monitor_metric_alert - right?) look like?但即使它是azurerm_application_insights_web_test ,警报规则(即azurerm_monitor_metric_alert - 对吗?)会是什么样子? I'm confused about this part:我对这部分感到困惑:

# …
resource "azurerm_monitor_metric_alert" "example" {
  # …
  criteria {
    metric_name      = "???"
    metric_namespace = "???"
    # …
  }
# …

What are the correct values to use there?在那里使用的正确值是什么?

I am aware about this ancient blog post Creating an Application Insights Web Test and Alert Programmatically from 2015 - 6 years ago.我知道这篇古老的博客文章从 2015 年到 6 年前以编程方式创建 Application Insights Web 测试和警报 It makes you go to the Azure Resource Explorer :它使您从 go 到Azure 资源浏览器

In Resource Explorer, open your subscription and resource group, then providers, Microsoft Insights.在资源资源管理器中,打开订阅和资源组,然后打开提供程序和 Microsoft Insights。 There you'll see two folders that will be important to us today: webtests and alertrules .在那里你会看到两个对我们今天很重要的文件夹: webtests 和alertrules

When I go to "microsoft.insights", the "folder" alertrules isn't there.当我将 go 转到“microsoft.insights”时,“文件夹” alertrules不存在。

资源浏览器 -> microsoft.insights:没有警报规则

I'm trying to create an Alert Rule for an Application Insights → Availability → Standard (preview) test.我正在尝试为 Application Insights → 可用性 → 标准(预览)测试创建警报规则。 First off, how do you create a standard test?首先,您如何创建标准测试?

As Standard Test is a preview feature, its currently not added in terraform-azurerm-provider .由于Standard测试是一个预览功能,它目前没有添加到terraform-azurerm-provider中。 It can be only deployed from ARM templates as of now.目前只能从ARM 模板部署。 Only Classic Test can be deployed from Terraform for now.目前只能从Terraform部署Classic测试。


But even if it were a azurerm_application_insights_web_test, how would an Alert Rule (ie. azurerm_monitor_metric_alert - right?) look like?但即使它是 azurerm_application_insights_web_test,警报规则(即 azurerm_monitor_metric_alert - 对吗?)会是什么样子?

You don't have to use criteria / dynamic criteria instead you should use application_insights_web_test_location_availability_criteria .您不必使用criteria / dynamic criteria ,而是应该使用application_insights_web_test_location_availability_criteria So, metric_name and metric_namespace won't be required.因此, metric_namemetric_namespace Using that your code for classic web test with alert rule will look something like below:使用带有警报规则的经典 web 测试的代码将如下所示:

provider "azurerm" {
  features{}
}
data "azurerm_application_insights" "example" {
  name="ansumantestapp"
  resource_group_name = "ansbalrg"
}

resource "azurerm_application_insights_web_test" "example" {
  name                    = "tf-test-appinsights-webtest"
  location                = data.azurerm_application_insights.example.location
  resource_group_name     = data.azurerm_application_insights.example.resource_group_name
  application_insights_id = data.azurerm_application_insights.example.id
  kind                    = "ping"
  frequency               = 300
  timeout                 = 60
  enabled                 = true
  geo_locations           = ["us-ca-sjc-azr", "us-va-ash-azr"]

  configuration = <<XML
<WebTest Name="WebTest1" Id="ABD48585-0831-40CB-9069-682EA6BB3583" Enabled="True" CssProjectStructure="" CssIteration="" Timeout="0" WorkItemIds="" xmlns="http://microsoft.com/schemas/VisualStudio/TeamTest/2010" Description="" CredentialUserName="" CredentialPassword="" PreAuthenticate="True" Proxy="default" StopOnError="False" RecordedResultFile="" ResultsLocale="">
  <Items>
    <Request Method="GET" Guid="a5f10126-e4cd-570d-961c-cea43999a200" Version="1.1" Url="http://microsoft.com" ThinkTime="0" Timeout="300" ParseDependentRequests="True" FollowRedirects="True" RecordResult="True" Cache="False" ResponseTimeGoal="0" Encoding="utf-8" ExpectedHttpStatusCode="200" ExpectedResponseUrl="" ReportingName="" IgnoreHttpStatusCode="False" />
  </Items>
</WebTest>
XML

}
resource "azurerm_monitor_action_group" "main" {
  name                = "example-actiongroup"
  resource_group_name = data.azurerm_application_insights.example.resource_group_name
  short_name          = "exampleact"
  email_receiver {
    name                    = "sendtoadmin"
    email_address           = "myemailid"
    use_common_alert_schema = true
  }
}

resource "azurerm_monitor_metric_alert" "example" {
  name                = "example-metricalert"
  resource_group_name = data.azurerm_application_insights.example.resource_group_name
  scopes = [azurerm_application_insights_web_test.example.id,data.azurerm_application_insights.example.id]
  description         = "PING test alert"

application_insights_web_test_location_availability_criteria {
  web_test_id = azurerm_application_insights_web_test.example.id
  component_id = data.azurerm_application_insights.example.id
  failed_location_count = 2
}

  action {
    action_group_id = azurerm_monitor_action_group.main.id
  }
}

Output: Output:

在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM