Skip to content

Commit

Permalink
Add support for forecast options in AlertPolicy (GoogleCloudPlatform#…
Browse files Browse the repository at this point in the history
…7926)

Co-authored-by: James Edouard <[email protected]>
  • Loading branch information
2 people authored and ericayyliu committed Jul 26, 2023
1 parent ac2c607 commit d400aab
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 6 deletions.
29 changes: 28 additions & 1 deletion mmv1/products/monitoring/AlertPolicy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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: |
Expand Down
Original file line number Diff line number Diff line change
@@ -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"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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" {
Expand Down Expand Up @@ -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)
}

0 comments on commit d400aab

Please sign in to comment.