Skip to content

Commit

Permalink
feat(fastly_alerts): add percentage alerts (#845)
Browse files Browse the repository at this point in the history
* Added percent alerts

* Added docs with optional ignore_below field on alerts

* Renamed tests func and change description of ignore_below

* Change name of tests to include one underscore

* Added missing types and evaluation period on alerts
  • Loading branch information
jsonroy-fastly authored May 9, 2024
1 parent 5171b89 commit 2393b86
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 8 deletions.
8 changes: 6 additions & 2 deletions docs/resources/alert.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,13 @@ $ terraform import fastly_alert.example xxxxxxxxxxxxxxxxxxxx

Required:

- `period` (String) The length of time to evaluate whether the conditions have been met. The data is polled every minute. One of: `5m`, `15m`, `30m`.
- `period` (String) The length of time to evaluate whether the conditions have been met. The data is polled every minute. One of: `2m`, `3m`, `5m`, `15m`, `30m`.
- `threshold` (Number) Threshold used to alert.
- `type` (String) Type of strategy to use to evaluate. One of: `above_threshold`, `below_threshold`.
- `type` (String) Type of strategy to use to evaluate. One of: `above_threshold`, `all_above_threshold`, `below_threshold`, `percent_absolute`, `percent_decrease`, `percent_increase`.

Optional:

- `ignore_below` (Number) Threshold for the denominator value used in evaluations that calculate a rate or ratio. Usually used to filter out noise.


<a id="nestedblock--dimensions"></a>
Expand Down
19 changes: 16 additions & 3 deletions fastly/resource_fastly_alert.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,15 @@ func resourceFastlyAlert() *schema.Resource {
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"ignore_below": {
Type: schema.TypeFloat,
Optional: true,
Description: "Threshold for the denominator value used in evaluations that calculate a rate or ratio. Usually used to filter out noise.",
},
"period": {
Type: schema.TypeString,
Required: true,
Description: "The length of time to evaluate whether the conditions have been met. The data is polled every minute. One of: `5m`, `15m`, `30m`.",
Description: "The length of time to evaluate whether the conditions have been met. The data is polled every minute. One of: `2m`, `3m`, `5m`, `15m`, `30m`.",
},
"threshold": {
Type: schema.TypeFloat,
Expand All @@ -70,7 +75,7 @@ func resourceFastlyAlert() *schema.Resource {
"type": {
Type: schema.TypeString,
Required: true,
Description: "Type of strategy to use to evaluate. One of: `above_threshold`, `below_threshold`.",
Description: "Type of strategy to use to evaluate. One of: `above_threshold`, `all_above_threshold`, `below_threshold`, `percent_absolute`, `percent_decrease`, `percent_increase`.",
},
},
},
Expand Down Expand Up @@ -291,11 +296,19 @@ func buildDimensions(data map[string][]string, v map[string]any) map[string][]st
}

func buildEvaluationStrategy(v map[string]any) map[string]any {
return map[string]any{
// Required attributes
m := map[string]any{
"type": v["type"].(string),
"period": v["period"].(string),
"threshold": v["threshold"].(float64),
}

// Optional attributes
if value, ok := v["ignore_below"]; ok {
m["ignore_below"] = value.(float64)
}

return m
}

func buildStringSlice(s *schema.Set) []string {
Expand Down
87 changes: 84 additions & 3 deletions fastly/resource_fastly_alert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)

func TestAccFastlyAlert_basic(t *testing.T) {
func TestAccFastlyAlert_Basic(t *testing.T) {
var service gofastly.ServiceDetail
serviceName := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
domainName := fmt.Sprintf("fastly-test.tf-%s.com", acctest.RandString(10))
Expand Down Expand Up @@ -75,7 +75,7 @@ func TestAccFastlyAlert_basic(t *testing.T) {
})
}

func TestAccFastlyAlert_basic_stats(t *testing.T) {
func TestAccFastlyAlert_BasicStats(t *testing.T) {
var service gofastly.ServiceDetail
serviceName := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
domainName := fmt.Sprintf("fastly-test.tf-%s.com", acctest.RandString(10))
Expand Down Expand Up @@ -134,7 +134,7 @@ func TestAccFastlyAlert_basic_stats(t *testing.T) {
})
}

func TestAccFastlyAlert_basic_stats_aggregate(t *testing.T) {
func TestAccFastlyAlert_BasicStatsAggregate(t *testing.T) {
service := gofastly.ServiceDetail{
Name: gofastly.ToPointer(""),
ServiceID: gofastly.ToPointer(""),
Expand Down Expand Up @@ -193,6 +193,69 @@ func TestAccFastlyAlert_basic_stats_aggregate(t *testing.T) {
})
}

func TestAccFastlyAlert_BasicStatsAggregatePercent(t *testing.T) {
service := gofastly.ServiceDetail{
Name: gofastly.ToPointer(""),
ServiceID: gofastly.ToPointer(""),
}

createAlert := gofastly.AlertDefinition{
Description: "Terraform percent test",
Dimensions: map[string][]string{},
// 25 percent increase
EvaluationStrategy: map[string]any{
"type": "percent_increase",
"period": "2m",
"threshold": 0.25,
"ignore_below": float64(10),
},
Metric: "status_4xx",
Name: fmt.Sprintf("Terraform test percent alert %s", acctest.RandString(10)),
Source: "stats",
}
updateAlert := gofastly.AlertDefinition{
Description: "Terraform test with new description",
Dimensions: map[string][]string{},
// 10 percent increase
EvaluationStrategy: map[string]any{
"type": "percent_increase",
"period": "2m",
"threshold": 0.1,
"ignore_below": float64(10),
},
Metric: "status_4xx",
Name: fmt.Sprintf("Terraform test update percent alert %s", acctest.RandString(10)),
Source: "stats",
}

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
},
ProviderFactories: testAccProviders,
CheckDestroy: testAccCheckAlertDestroy,
Steps: []resource.TestStep{
{
Config: testAccAlertPercentAggregateStatsConfig(createAlert),
Check: resource.ComposeTestCheckFunc(
testAccCheckFastlyAlertsRemoteState(&service, "", createAlert),
),
},
{
Config: testAccAlertPercentAggregateStatsConfig(updateAlert),
Check: resource.ComposeTestCheckFunc(
testAccCheckFastlyAlertsRemoteState(&service, "", updateAlert),
),
},
{
ResourceName: "fastly_alert.tf_percent",
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccCheckFastlyAlertsRemoteState(service *gofastly.ServiceDetail, serviceName string, expected gofastly.AlertDefinition) resource.TestCheckFunc {
return func(_ *terraform.State) error {
if gofastly.ToValue(service.Name) != serviceName {
Expand Down Expand Up @@ -359,3 +422,21 @@ resource "fastly_alert" "tf_bar" {
}
}`, alert.Name, alert.Description, alert.Source, alert.Metric, alert.EvaluationStrategy["type"], alert.EvaluationStrategy["period"], alert.EvaluationStrategy["threshold"])
}

func testAccAlertPercentAggregateStatsConfig(alert gofastly.AlertDefinition) string {
return fmt.Sprintf(`
resource "fastly_alert" "tf_percent" {
name = "%s"
description = "%s"
service_id = ""
source = "%s"
metric = "%s"
evaluation_strategy {
type = "%s"
period = "%s"
threshold = %v
ignore_below = %v
}
}`, alert.Name, alert.Description, alert.Source, alert.Metric, alert.EvaluationStrategy["type"], alert.EvaluationStrategy["period"], alert.EvaluationStrategy["threshold"], alert.EvaluationStrategy["ignore_below"])
}

0 comments on commit 2393b86

Please sign in to comment.