Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for forecast options in AlertPolicy #7926

Merged
merged 4 commits into from
May 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
jedouard1994 marked this conversation as resolved.
Show resolved Hide resolved
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
jedouard1994 marked this conversation as resolved.
Show resolved Hide resolved
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)
}