diff --git a/mmv1/products/monitoring/AlertPolicy.yaml b/mmv1/products/monitoring/AlertPolicy.yaml index 2390cf688bcb..0831ff4fdd30 100644 --- a/mmv1/products/monitoring/AlertPolicy.yaml +++ b/mmv1/products/monitoring/AlertPolicy.yaml @@ -38,13 +38,20 @@ examples: vars: alert_policy_display_name: 'My Alert Policy' - # skipping tests because the API is full of race conditions + # skipping tests because the API is full of race conditions - !ruby/object:Provider::Terraform::Examples skip_test: true name: 'monitoring_alert_policy_evaluation_missing_data' primary_resource_id: 'alert_policy' vars: alert_policy_display_name: 'My Alert Policy' + # skipping tests because the API is full of race conditions + - !ruby/object:Provider::Terraform::Examples + skip_test: true + name: "monitoring_alert_policy_forecast_options" + primary_resource_id: "alert_policy" + vars: + alert_policy_display_name: "My Alert Policy" custom_code: !ruby/object:Provider::Terraform::CustomCode constants: templates/terraform/constants/monitoring_alert_policy.go.erb custom_import: templates/terraform/custom_import/self_link_as_name.erb @@ -560,6 +567,26 @@ properties: generate spurious alerts, but short enough that unhealthy states are detected and alerted on quickly. + - !ruby/object:Api::Type::NestedObject + name: forecastOptions + description: | + When this field is present, the `MetricThreshold` + condition forecasts whether the time series is + predicted to violate the threshold within the + `forecastHorizon`. When this field is not set, the + `MetricThreshold` tests the current value of the + timeseries against the threshold. + properties: + - !ruby/object:Api::Type::String + name: forecastHorizon + description: | + The length of time into the future to forecast + whether a timeseries will violate the threshold. + If the predicted value is found to violate the + threshold, and the violation is observed in all + forecasts made for the Configured `duration`, + then the timeseries is considered to be failing. + required: true - !ruby/object:Api::Type::Enum name: comparison description: | diff --git a/mmv1/templates/terraform/examples/monitoring_alert_policy_forecast_options.tf.erb b/mmv1/templates/terraform/examples/monitoring_alert_policy_forecast_options.tf.erb new file mode 100644 index 000000000000..b624462640cb --- /dev/null +++ b/mmv1/templates/terraform/examples/monitoring_alert_policy_forecast_options.tf.erb @@ -0,0 +1,23 @@ +resource "google_monitoring_alert_policy" "<%= ctx[:primary_resource_id] %>" { + display_name = "<%= ctx[:vars]["alert_policy_display_name"] %>" + combiner = "OR" + conditions { + display_name = "test condition" + condition_threshold { + filter = "metric.type=\"compute.googleapis.com/instance/disk/write_bytes_count\" AND resource.type=\"gce_instance\"" + duration = "60s" + forecast_options { + forecast_horizon = "3600s" + } + comparison = "COMPARISON_GT" + aggregations { + alignment_period = "60s" + per_series_aligner = "ALIGN_RATE" + } + } + } + + user_labels = { + foo = "bar" + } +} diff --git a/mmv1/third_party/terraform/tests/resource_monitoring_alert_policy_test.go b/mmv1/third_party/terraform/tests/resource_monitoring_alert_policy_test.go index 6cd8f6e594f2..8bbb7508f69e 100644 --- a/mmv1/third_party/terraform/tests/resource_monitoring_alert_policy_test.go +++ b/mmv1/third_party/terraform/tests/resource_monitoring_alert_policy_test.go @@ -15,11 +15,12 @@ import ( func TestAccMonitoringAlertPolicy(t *testing.T) { testCases := map[string]func(t *testing.T){ - "basic": testAccMonitoringAlertPolicy_basic, - "full": testAccMonitoringAlertPolicy_full, - "update": testAccMonitoringAlertPolicy_update, - "mql": testAccMonitoringAlertPolicy_mql, - "log": testAccMonitoringAlertPolicy_log, + "basic": testAccMonitoringAlertPolicy_basic, + "full": testAccMonitoringAlertPolicy_full, + "update": testAccMonitoringAlertPolicy_update, + "mql": testAccMonitoringAlertPolicy_mql, + "log": testAccMonitoringAlertPolicy_log, + "forecast": testAccMonitoringAlertPolicy_forecast, } for name, tc := range testCases { @@ -181,6 +182,29 @@ func testAccCheckAlertPolicyDestroyProducer(t *testing.T) func(s *terraform.Stat } } +func testAccMonitoringAlertPolicy_forecast(t *testing.T) { + + alertName := fmt.Sprintf("tf-test-%s", RandString(t, 10)) + conditionName := fmt.Sprintf("tf-test-%s", RandString(t, 10)) + filter := `metric.type=\"compute.googleapis.com/instance/disk/write_bytes_count\" AND resource.type=\"gce_instance\"` + + VcrTest(t, resource.TestCase{ + PreCheck: func() { acctest.AccTestPreCheck(t) }, + ProtoV5ProviderFactories: ProtoV5ProviderFactories(t), + CheckDestroy: testAccCheckAlertPolicyDestroyProducer(t), + Steps: []resource.TestStep{ + { + Config: testAccMonitoringAlertPolicy_forecastCfg(alertName, conditionName, "ALIGN_RATE", filter), + }, + { + ResourceName: "google_monitoring_alert_policy.forecast", + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + func testAccMonitoringAlertPolicy_basicCfg(alertName, conditionName, aligner, filter string) string { return fmt.Sprintf(` resource "google_monitoring_alert_policy" "basic" { @@ -335,3 +359,32 @@ resource "google_monitoring_alert_policy" "log" { } `, alertName, conditionName) } + +func testAccMonitoringAlertPolicy_forecastCfg(alertName, conditionName, aligner, filter string) string { + return fmt.Sprintf(` +resource "google_monitoring_alert_policy" "forecast" { + display_name = "%s" + enabled = true + combiner = "OR" + + conditions { + display_name = "%s" + + condition_threshold { + aggregations { + alignment_period = "60s" + per_series_aligner = "%s" + } + + duration = "60s" + forecast_options { + forecast_horizon = "3600s" + } + comparison = "COMPARISON_GT" + filter = "%s" + threshold_value = "0.5" + } + } +} +`, alertName, conditionName, aligner, filter) +}