From f5587c15c55cd38f5340a8881032432d232436ab Mon Sep 17 00:00:00 2001 From: Brian Lamb <5820125+lambcode@users.noreply.github.com> Date: Wed, 21 Feb 2024 16:16:57 -0700 Subject: [PATCH] Fix bug where warning threshold duration could not be applied (#211) * Fix warning duration check looking at critical duration * Add tests for bug --- lightstep/resource_alert_test.go | 94 ++++++++++++++++++++++++++ lightstep/resource_metric_condition.go | 2 +- 2 files changed, 95 insertions(+), 1 deletion(-) diff --git a/lightstep/resource_alert_test.go b/lightstep/resource_alert_test.go index 52121791..64229e42 100644 --- a/lightstep/resource_alert_test.go +++ b/lightstep/resource_alert_test.go @@ -1171,3 +1171,97 @@ resource "lightstep_alert" "test" { }, }) } + +func TestAccAlertWithOnlyWarningThresholdDurations(t *testing.T) { + var condition client.UnifiedCondition + + conditionConfig := ` +resource "lightstep_alert" "test" { + project_name = "` + testProject + `" + name = "Too many requests" + + expression { + is_multi = true + is_no_data = true + no_data_duration_ms = 60000 + operand = "above" + thresholds { + warning = 5 + warning_duration_ms = 120000 + } + } + + query { + query_name = "a" + hidden = false + display = "line" + query_string = "metric requests | rate 1h | filter service_name == frontend | group_by [method], mean" + } +} +` + + resourceName := "lightstep_alert.test" + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccMetricConditionDestroy, + Steps: []resource.TestStep{ + { + Config: conditionConfig, + Check: resource.ComposeTestCheckFunc( + testAccCheckLightstepAlertExists(resourceName, &condition), + resource.TestCheckResourceAttr(resourceName, "name", "Too many requests"), + resource.TestCheckResourceAttr(resourceName, "expression.0.no_data_duration_ms", "60000"), + resource.TestCheckResourceAttr(resourceName, "expression.0.thresholds.0.warning_duration_ms", "120000"), + ), + }, + }, + }) +} + +func TestAccAlertWithOnlyCriticalThresholdDurations(t *testing.T) { + var condition client.UnifiedCondition + + conditionConfig := ` +resource "lightstep_alert" "test" { + project_name = "` + testProject + `" + name = "Too many requests" + + expression { + is_multi = true + is_no_data = true + no_data_duration_ms = 60000 + operand = "above" + thresholds { + critical = 10 + critical_duration_ms = 180000 + } + } + + query { + query_name = "a" + hidden = false + display = "line" + query_string = "metric requests | rate 1h | filter service_name == frontend | group_by [method], mean" + } +} +` + + resourceName := "lightstep_alert.test" + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccMetricConditionDestroy, + Steps: []resource.TestStep{ + { + Config: conditionConfig, + Check: resource.ComposeTestCheckFunc( + testAccCheckLightstepAlertExists(resourceName, &condition), + resource.TestCheckResourceAttr(resourceName, "name", "Too many requests"), + resource.TestCheckResourceAttr(resourceName, "expression.0.no_data_duration_ms", "60000"), + resource.TestCheckResourceAttr(resourceName, "expression.0.thresholds.0.critical_duration_ms", "180000"), + ), + }, + }, + }) +} diff --git a/lightstep/resource_metric_condition.go b/lightstep/resource_metric_condition.go index 89714323..b1973429 100644 --- a/lightstep/resource_metric_condition.go +++ b/lightstep/resource_metric_condition.go @@ -1050,7 +1050,7 @@ func buildThresholds(singleExpression map[string]interface{}) (client.Thresholds } warningDuration := thresholdsObj["warning_duration_ms"] - if warningDuration != nil && warningDuration != "" && criticalDuration != 0 { + if warningDuration != nil && warningDuration != "" && warningDuration != 0 { d, ok := warningDuration.(int) if !ok { return t, fmt.Errorf("unexpected format for warning_duration_ms")