Skip to content

Commit

Permalink
Fix bug where warning threshold duration could not be applied (#211)
Browse files Browse the repository at this point in the history
* Fix warning duration check looking at critical duration

* Add tests for bug
  • Loading branch information
lambcode authored Feb 21, 2024
1 parent 8853075 commit f5587c1
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 1 deletion.
94 changes: 94 additions & 0 deletions lightstep/resource_alert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
),
},
},
})
}
2 changes: 1 addition & 1 deletion lightstep/resource_metric_condition.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit f5587c1

Please sign in to comment.