Skip to content

Commit 77b0c74

Browse files
authored
feat(alerts): add keep_firing_for_minutes field (#387)
1 parent bec2891 commit 77b0c74

File tree

4 files changed

+37
-2
lines changed

4 files changed

+37
-2
lines changed

sysdig/internal/client/v2/model.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,8 @@ type AlertV2Common struct {
501501
}
502502

503503
type AlertV2ConfigPrometheus struct {
504-
Query string `json:"query"`
504+
Query string `json:"query"`
505+
KeepFiringForSec *int `json:"keepFiringForSec,omitempty"`
505506
}
506507

507508
type AlertV2Prometheus struct {

sysdig/resource_sysdig_monitor_alert_v2_prometheus.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ package sysdig
22

33
import (
44
"context"
5-
v2 "github.com/draios/terraform-provider-sysdig/sysdig/internal/client/v2"
65
"strconv"
76
"time"
87

8+
v2 "github.com/draios/terraform-provider-sysdig/sysdig/internal/client/v2"
9+
910
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1011
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
12+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
1113
)
1214

1315
func resourceSysdigMonitorAlertV2Prometheus() *schema.Resource {
@@ -34,6 +36,11 @@ func resourceSysdigMonitorAlertV2Prometheus() *schema.Resource {
3436
Type: schema.TypeString,
3537
Required: true,
3638
},
39+
"keep_firing_for_minutes": {
40+
Type: schema.TypeInt,
41+
Optional: true,
42+
ValidateFunc: validation.IntAtLeast(1),
43+
},
3744
}),
3845
}
3946
}
@@ -139,6 +146,10 @@ func buildAlertV2PrometheusStruct(d *schema.ResourceData) *v2.AlertV2Prometheus
139146

140147
config := v2.AlertV2ConfigPrometheus{}
141148
config.Query = d.Get("query").(string)
149+
if keepFiringForMinutes, ok := d.GetOk("keep_firing_for_minutes"); ok {
150+
kff := keepFiringForMinutes.(int) * 60
151+
config.KeepFiringForSec = &kff
152+
}
142153

143154
alert := &v2.AlertV2Prometheus{
144155
AlertV2Common: *alertV2Common,
@@ -154,6 +165,11 @@ func updateAlertV2PrometheusState(d *schema.ResourceData, alert *v2.AlertV2Prome
154165
}
155166

156167
_ = d.Set("query", alert.Config.Query)
168+
if alert.Config.KeepFiringForSec != nil {
169+
_ = d.Set("keep_firing_for_minutes", *alert.Config.KeepFiringForSec/60)
170+
} else {
171+
_ = d.Set("keep_firing_for_minutes", nil)
172+
}
157173

158174
return
159175
}

sysdig/resource_sysdig_monitor_alert_v2_prometheus_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ func TestAccAlertV2Prometheus(t *testing.T) {
3535
{
3636
Config: alertV2PrometheusWithGroup(rText()),
3737
},
38+
{
39+
Config: alertV2PrometheusWithKeepFiringFor(rText()),
40+
},
3841
{
3942
ResourceName: "sysdig_monitor_alert_v2_prometheus.sample",
4043
ImportState: true,
@@ -70,3 +73,17 @@ resource "sysdig_monitor_alert_v2_prometheus" "sample" {
7073
}
7174
`, name, name)
7275
}
76+
77+
func alertV2PrometheusWithKeepFiringFor(name string) string {
78+
return fmt.Sprintf(`
79+
resource "sysdig_monitor_alert_v2_prometheus" "sample" {
80+
name = "TERRAFORM TEST - PROMQL %s"
81+
description = "TERRAFORM TEST - PROMQL %s"
82+
severity = "high"
83+
query = "(elasticsearch_jvm_memory_used_bytes{area=\"heap\"} / elasticsearch_jvm_memory_max_bytes{area=\"heap\"}) * 100 > 80"
84+
trigger_after_minutes = 10
85+
enabled = false
86+
keep_firing_for_minutes = 10
87+
}
88+
`, name, name)
89+
}

website/docs/r/monitor_alert_v2_prometheus.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ By defining this field, the user can add link to notificatons.
7373
### Prometheus alert arguments
7474

7575
* `query` - (Required) PromQL-based metric expression to alert on. Example: `histogram_quantile(0.99, rate(etcd_http_successful_duration_seconds_bucket[5m]) > 0.15` or `predict_linear(sysdig_fs_free_bytes{fstype!~"tmpfs"}[1h], 24*3600) < 10000000000`.
76+
* `keep_firing_for_minutes` - (Optional) Alert resolution delay before actually resolving an alert.
7677

7778
## Attributes Reference
7879

0 commit comments

Comments
 (0)