Skip to content

Commit 76abc9d

Browse files
authored
feat(alerts): add unreported_alert_notifications_retention_seconds argument to alerts (#416)
1 parent 69b15b2 commit 76abc9d

12 files changed

+163
-20
lines changed

sysdig/internal/client/v2/model.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -597,8 +597,9 @@ type AlertV2ConfigMetric struct {
597597

598598
type AlertV2Metric struct {
599599
AlertV2Common
600-
DurationSec int `json:"durationSec"`
601-
Config AlertV2ConfigMetric `json:"config"`
600+
DurationSec int `json:"durationSec"`
601+
Config AlertV2ConfigMetric `json:"config"`
602+
UnreportedAlertNotificationsRetentionSec *int `json:"unreportedAlertNotificationsRetentionSec"`
602603
}
603604

604605
type alertV2MetricWrapper struct {
@@ -619,8 +620,9 @@ type AlertV2ConfigDowntime struct {
619620

620621
type AlertV2Downtime struct {
621622
AlertV2Common
622-
DurationSec int `json:"durationSec"`
623-
Config AlertV2ConfigDowntime `json:"config"`
623+
DurationSec int `json:"durationSec"`
624+
Config AlertV2ConfigDowntime `json:"config"`
625+
UnreportedAlertNotificationsRetentionSec *int `json:"unreportedAlertNotificationsRetentionSec"`
624626
}
625627

626628
type alertV2DowntimeWrapper struct {
@@ -656,8 +658,9 @@ type AlertV2ConfigFormBasedPrometheus struct {
656658

657659
type AlertV2FormBasedPrometheus struct {
658660
AlertV2Common
659-
DurationSec int `json:"durationSec"` // not really used but the api wants it set to 0 in POST/PUT
660-
Config AlertV2ConfigFormBasedPrometheus `json:"config"`
661+
DurationSec int `json:"durationSec"` // not really used but the api wants it set to 0 in POST/PUT
662+
Config AlertV2ConfigFormBasedPrometheus `json:"config"`
663+
UnreportedAlertNotificationsRetentionSec *int `json:"unreportedAlertNotificationsRetentionSec"`
661664
}
662665

663666
type alertV2FormBasedPrometheusWrapper struct {
@@ -666,8 +669,9 @@ type alertV2FormBasedPrometheusWrapper struct {
666669

667670
type AlertV2Change struct {
668671
AlertV2Common
669-
DurationSec int `json:"durationSec"` // not really used but the api wants it set to 0 in POST/PUT
670-
Config AlertV2ConfigChange `json:"config"`
672+
DurationSec int `json:"durationSec"` // not really used but the api wants it set to 0 in POST/PUT
673+
Config AlertV2ConfigChange `json:"config"`
674+
UnreportedAlertNotificationsRetentionSec *int `json:"unreportedAlertNotificationsRetentionSec"`
671675
}
672676

673677
type alertV2ChangeWrapper struct {

sysdig/resource_sysdig_monitor_alert_v2_change.go

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,11 @@ func resourceSysdigMonitorAlertV2Change() *schema.Resource {
128128
Type: schema.TypeInt,
129129
Required: true,
130130
},
131+
"unreported_alert_notifications_retention_seconds": {
132+
Type: schema.TypeInt,
133+
Optional: true,
134+
ValidateFunc: validation.IntAtLeast(60),
135+
},
131136
})),
132137

133138
CustomizeDiff: func(ctx context.Context, diff *schema.ResourceDiff, i interface{}) error {
@@ -300,10 +305,17 @@ func buildAlertV2ChangeStruct(d *schema.ResourceData) (*v2.AlertV2Change, error)
300305
//LongerRangeSec
301306
config.LongerRangeSec = d.Get("longer_time_range_seconds").(int)
302307

308+
var unreportedAlertNotificationsRetentionSec *int
309+
if unreportedAlertNotificationsRetentionSecInterface, ok := d.GetOk("unreported_alert_notifications_retention_seconds"); ok {
310+
u := unreportedAlertNotificationsRetentionSecInterface.(int)
311+
unreportedAlertNotificationsRetentionSec = &u
312+
}
313+
303314
alert := &v2.AlertV2Change{
304-
AlertV2Common: *alertV2Common,
305-
DurationSec: 0,
306-
Config: config,
315+
AlertV2Common: *alertV2Common,
316+
DurationSec: 0,
317+
Config: config,
318+
UnreportedAlertNotificationsRetentionSec: unreportedAlertNotificationsRetentionSec,
307319
}
308320
return alert, nil
309321
}
@@ -337,5 +349,11 @@ func updateAlertV2ChangeState(d *schema.ResourceData, alert *v2.AlertV2Change) e
337349

338350
_ = d.Set("longer_time_range_seconds", alert.Config.LongerRangeSec)
339351

352+
if alert.UnreportedAlertNotificationsRetentionSec != nil {
353+
_ = d.Set("unreported_alert_notifications_retention_seconds", *alert.UnreportedAlertNotificationsRetentionSec)
354+
} else {
355+
_ = d.Set("unreported_alert_notifications_retention_seconds", nil)
356+
}
357+
340358
return nil
341359
}

sysdig/resource_sysdig_monitor_alert_v2_change_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ func TestAccAlertV2Change(t *testing.T) {
5454
{
5555
Config: alertV2ChangeWithEnabled(rText()),
5656
},
57+
{
58+
Config: alertV2ChangeWithUnreportedAlertNotificationsRetentionSec(rText()),
59+
},
5760
{
5861
Config: alertV2ChangeWithWarningThreshold(rText()),
5962
},
@@ -295,6 +298,22 @@ resource "sysdig_monitor_alert_v2_change" "sample" {
295298
`, name)
296299
}
297300

301+
func alertV2ChangeWithUnreportedAlertNotificationsRetentionSec(name string) string {
302+
return fmt.Sprintf(`
303+
resource "sysdig_monitor_alert_v2_change" "sample" {
304+
name = "TERRAFORM TEST - CHANGE %s"
305+
metric = "sysdig_container_cpu_used_percent"
306+
group_aggregation = "avg"
307+
time_aggregation = "avg"
308+
operator = ">="
309+
threshold = 50
310+
shorter_time_range_seconds = 300
311+
longer_time_range_seconds = 3600
312+
unreported_alert_notifications_retention_seconds = 60 * 60 * 24 * 30
313+
}
314+
`, name)
315+
}
316+
298317
func alertV2ChangeWithWarningThreshold(name string) string {
299318
return fmt.Sprintf(`
300319
resource "sysdig_monitor_notification_channel_email" "nc_email1" {

sysdig/resource_sysdig_monitor_alert_v2_downtime.go

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ func resourceSysdigMonitorAlertV2Downtime() *schema.Resource {
4747
Required: true,
4848
ValidateFunc: validation.StringInSlice([]string{"sysdig_container_up", "sysdig_program_up", "sysdig_host_up"}, true),
4949
},
50+
"unreported_alert_notifications_retention_seconds": {
51+
Type: schema.TypeInt,
52+
Optional: true,
53+
ValidateFunc: validation.IntAtLeast(60),
54+
},
5055
})),
5156
}
5257
}
@@ -173,10 +178,17 @@ func buildAlertV2DowntimeStruct(d *schema.ResourceData) *v2.AlertV2Downtime {
173178

174179
config.NoDataBehaviour = "DO_NOTHING"
175180

181+
var unreportedAlertNotificationsRetentionSec *int
182+
if unreportedAlertNotificationsRetentionSecInterface, ok := d.GetOk("unreported_alert_notifications_retention_seconds"); ok {
183+
u := unreportedAlertNotificationsRetentionSecInterface.(int)
184+
unreportedAlertNotificationsRetentionSec = &u
185+
}
186+
176187
alert := &v2.AlertV2Downtime{
177-
AlertV2Common: *alertV2Common,
178-
DurationSec: minutesToSeconds(d.Get("trigger_after_minutes").(int)),
179-
Config: config,
188+
AlertV2Common: *alertV2Common,
189+
DurationSec: minutesToSeconds(d.Get("trigger_after_minutes").(int)),
190+
Config: config,
191+
UnreportedAlertNotificationsRetentionSec: unreportedAlertNotificationsRetentionSec,
180192
}
181193
return alert
182194
}
@@ -198,5 +210,11 @@ func updateAlertV2DowntimeState(d *schema.ResourceData, alert *v2.AlertV2Downtim
198210

199211
_ = d.Set("metric", alert.Config.Metric.ID)
200212

213+
if alert.UnreportedAlertNotificationsRetentionSec != nil {
214+
_ = d.Set("unreported_alert_notifications_retention_seconds", *alert.UnreportedAlertNotificationsRetentionSec)
215+
} else {
216+
_ = d.Set("unreported_alert_notifications_retention_seconds", nil)
217+
}
218+
201219
return nil
202220
}

sysdig/resource_sysdig_monitor_alert_v2_downtime_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ func TestAccAlertV2Downtime(t *testing.T) {
2727
{
2828
Config: alertV2DowntimeWithName(rText()),
2929
},
30+
{
31+
Config: alertV2DowntimeWithWithUnreportedAlertNotificationsRetentionSec(rText()),
32+
},
3033
{
3134
Config: alertV2DowntimeWithGroupBy(rText()),
3235
},
@@ -60,6 +63,28 @@ resource "sysdig_monitor_alert_v2_downtime" "sample" {
6063
`, name)
6164
}
6265

66+
func alertV2DowntimeWithWithUnreportedAlertNotificationsRetentionSec(name string) string {
67+
return fmt.Sprintf(`
68+
resource "sysdig_monitor_alert_v2_downtime" "sample" {
69+
70+
name = "TERRAFORM TEST - DOWNTIMEV2 %s"
71+
metric = "sysdig_container_up"
72+
threshold = 75
73+
74+
scope {
75+
label = "kube_cluster_name"
76+
operator = "in"
77+
values = ["thom-cluster1", "demo-env-prom"]
78+
}
79+
80+
trigger_after_minutes = 15
81+
unreported_alert_notifications_retention_seconds = 60 * 60 * 24 * 30
82+
83+
}
84+
85+
`, name)
86+
}
87+
6388
func alertV2DowntimeWithGroupBy(name string) string {
6489
return fmt.Sprintf(`
6590
resource "sysdig_monitor_alert_v2_downtime" "sample" {

sysdig/resource_sysdig_monitor_alert_v2_form_based_prometheus.go

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ func resourceSysdigMonitorAlertV2FormBasedPrometheus() *schema.Resource {
5858
Default: "DO_NOTHING",
5959
ValidateFunc: validation.StringInSlice([]string{"DO_NOTHING", "TRIGGER"}, false),
6060
},
61+
"unreported_alert_notifications_retention_seconds": {
62+
Type: schema.TypeInt,
63+
Optional: true,
64+
ValidateFunc: validation.IntAtLeast(60),
65+
},
6166
})),
6267
}
6368
}
@@ -194,10 +199,17 @@ func buildAlertV2FormBasedPrometheusStruct(d *schema.ResourceData) (*v2.AlertV2F
194199

195200
config.NoDataBehaviour = d.Get("no_data_behaviour").(string)
196201

202+
var unreportedAlertNotificationsRetentionSec *int
203+
if unreportedAlertNotificationsRetentionSecInterface, ok := d.GetOk("unreported_alert_notifications_retention_seconds"); ok {
204+
u := unreportedAlertNotificationsRetentionSecInterface.(int)
205+
unreportedAlertNotificationsRetentionSec = &u
206+
}
207+
197208
alert := &v2.AlertV2FormBasedPrometheus{
198-
AlertV2Common: *alertV2Common,
199-
DurationSec: 0,
200-
Config: config,
209+
AlertV2Common: *alertV2Common,
210+
DurationSec: 0,
211+
Config: config,
212+
UnreportedAlertNotificationsRetentionSec: unreportedAlertNotificationsRetentionSec,
201213
}
202214
return alert, nil
203215
}
@@ -225,5 +237,11 @@ func updateAlertV2FormBasedPrometheusState(d *schema.ResourceData, alert *v2.Ale
225237

226238
_ = d.Set("no_data_behaviour", alert.Config.NoDataBehaviour)
227239

240+
if alert.UnreportedAlertNotificationsRetentionSec != nil {
241+
_ = d.Set("unreported_alert_notifications_retention_seconds", *alert.UnreportedAlertNotificationsRetentionSec)
242+
} else {
243+
_ = d.Set("unreported_alert_notifications_retention_seconds", nil)
244+
}
245+
228246
return nil
229247
}

sysdig/resource_sysdig_monitor_alert_v2_metric.go

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@ func resourceSysdigMonitorAlertV2Metric() *schema.Resource {
7272
Default: "DO_NOTHING",
7373
ValidateFunc: validation.StringInSlice([]string{"DO_NOTHING", "TRIGGER"}, false),
7474
},
75+
"unreported_alert_notifications_retention_seconds": {
76+
Type: schema.TypeInt,
77+
Optional: true,
78+
ValidateFunc: validation.IntAtLeast(60),
79+
},
7580
})),
7681
}
7782
}
@@ -215,10 +220,17 @@ func buildAlertV2MetricStruct(d *schema.ResourceData) (*v2.AlertV2Metric, error)
215220

216221
config.NoDataBehaviour = d.Get("no_data_behaviour").(string)
217222

223+
var unreportedAlertNotificationsRetentionSec *int
224+
if unreportedAlertNotificationsRetentionSecInterface, ok := d.GetOk("unreported_alert_notifications_retention_seconds"); ok {
225+
u := unreportedAlertNotificationsRetentionSecInterface.(int)
226+
unreportedAlertNotificationsRetentionSec = &u
227+
}
228+
218229
alert := &v2.AlertV2Metric{
219-
AlertV2Common: *alertV2Common,
220-
DurationSec: minutesToSeconds(d.Get("trigger_after_minutes").(int)),
221-
Config: config,
230+
AlertV2Common: *alertV2Common,
231+
DurationSec: minutesToSeconds(d.Get("trigger_after_minutes").(int)),
232+
Config: config,
233+
UnreportedAlertNotificationsRetentionSec: unreportedAlertNotificationsRetentionSec,
222234
}
223235
return alert, nil
224236
}
@@ -252,5 +264,11 @@ func updateAlertV2MetricState(d *schema.ResourceData, alert *v2.AlertV2Metric) e
252264

253265
_ = d.Set("no_data_behaviour", alert.Config.NoDataBehaviour)
254266

267+
if alert.UnreportedAlertNotificationsRetentionSec != nil {
268+
_ = d.Set("unreported_alert_notifications_retention_seconds", *alert.UnreportedAlertNotificationsRetentionSec)
269+
} else {
270+
_ = d.Set("unreported_alert_notifications_retention_seconds", nil)
271+
}
272+
255273
return nil
256274
}

sysdig/resource_sysdig_monitor_alert_v2_metric_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ func TestAccAlertV2Metric(t *testing.T) {
6060
{
6161
Config: alertV2MetricWithEnabled(rText()),
6262
},
63+
{
64+
Config: alertV2MetricWithUnreportedAlertNotificationsRetentionSec(rText()),
65+
},
6366
{
6467
Config: alertV2MetricWithWarningThreshold(rText()),
6568
},
@@ -344,6 +347,22 @@ resource "sysdig_monitor_alert_v2_metric" "sample" {
344347
`, name)
345348
}
346349

350+
func alertV2MetricWithUnreportedAlertNotificationsRetentionSec(name string) string {
351+
return fmt.Sprintf(`
352+
resource "sysdig_monitor_alert_v2_metric" "sample" {
353+
354+
name = "TERRAFORM TEST - METRICV2 %s"
355+
metric = "sysdig_container_cpu_used_percent"
356+
group_aggregation = "avg"
357+
time_aggregation = "avg"
358+
operator = ">="
359+
threshold = 50
360+
trigger_after_minutes = 15
361+
unreported_alert_notifications_retention_seconds = 60 * 60 * 24 * 30
362+
}
363+
`, name)
364+
}
365+
347366
func alertV2MetricWithWarningThreshold(name string) string {
348367
return fmt.Sprintf(`
349368
resource "sysdig_monitor_notification_channel_email" "nc_email1" {

website/docs/r/monitor_alert_v2_change.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ By defining this field, the user can add link to notifications.
104104
* `warning_threshold` - (Optional) Warning threshold used together with `op` to trigger the alert if crossed. Must be a number that triggers the alert before reaching the main `threshold`.
105105
* `shorter_time_range_seconds` - (Required) Time range for which data is compared to a longer, previous period. Can be one of `300` (5 minutes), `600` (10 minutes), `3600` (1 hour), `14400` (4 hours), `86400` (1 day).
106106
* `longer_time_range_seconds` - (Required) Time range for which data will be used as baseline for comparisons with data in the time range defined in `shorter_time_range_seconds`. Possible values depend on `shorter_time_range_seconds`: for a shorter time range of 5 minutes, longer time range can be 1, 2 or 3 hours, for a shorter time range or 10 minutes, it can be from 1 to 8 hours, for a shorter time range or one hour, it can be from 4 to 24 hours, for a shorter time range of 4 hours, it can be from 1 to 7 days, for a shorter time range of one day, it can only be 7 days.
107+
* `unreported_alert_notifications_retention_seconds` - (Optional) Period after which any alerts triggered for entities (such as containers or hosts) that are no longer reporting data will be automatically marked as 'deactivated'. By default there is no deactivation.
107108

108109
### `scope`
109110

website/docs/r/monitor_alert_v2_downtime.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ Enables the creation of a capture file of the syscalls during the event.
9696
* `group_by` - (Optional) List of segments to trigger a separate alert on. Example: `["kube_cluster_name", "kube_pod_name"]`.
9797
* `metric` - (Required) Metric the alert will act upon. Can be: `sysdig_container_up`, `sysdig_program_up`, `sysdig_host_up`.
9898
* `threshold` - (Required) Below of this percentage of downtime the alert will be triggered. Defaults to 100.
99+
* `unreported_alert_notifications_retention_seconds` - (Optional) Period after which any alerts triggered for entities (such as containers or hosts) that are no longer reporting data will be automatically marked as 'deactivated'. By default there is no deactivation.
99100

100101
### `scope`
101102

0 commit comments

Comments
 (0)