Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(alerts): add keep_firing_for_minutes field #387

Merged
merged 1 commit into from
Jul 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion sysdig/internal/client/v2/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,8 @@ type AlertV2Common struct {
}

type AlertV2ConfigPrometheus struct {
Query string `json:"query"`
Query string `json:"query"`
KeepFiringForSec *int `json:"keepFiringForSec,omitempty"`
}

type AlertV2Prometheus struct {
Expand Down
18 changes: 17 additions & 1 deletion sysdig/resource_sysdig_monitor_alert_v2_prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package sysdig

import (
"context"
v2 "github.com/draios/terraform-provider-sysdig/sysdig/internal/client/v2"
"strconv"
"time"

v2 "github.com/draios/terraform-provider-sysdig/sysdig/internal/client/v2"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)

func resourceSysdigMonitorAlertV2Prometheus() *schema.Resource {
Expand All @@ -34,6 +36,11 @@ func resourceSysdigMonitorAlertV2Prometheus() *schema.Resource {
Type: schema.TypeString,
Required: true,
},
"keep_firing_for_minutes": {
Type: schema.TypeInt,
Optional: true,
ValidateFunc: validation.IntAtLeast(1),
},
}),
}
}
Expand Down Expand Up @@ -139,6 +146,10 @@ func buildAlertV2PrometheusStruct(d *schema.ResourceData) *v2.AlertV2Prometheus

config := v2.AlertV2ConfigPrometheus{}
config.Query = d.Get("query").(string)
if keepFiringForMinutes, ok := d.GetOk("keep_firing_for_minutes"); ok {
kff := keepFiringForMinutes.(int) * 60
config.KeepFiringForSec = &kff
}

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

_ = d.Set("query", alert.Config.Query)
if alert.Config.KeepFiringForSec != nil {
_ = d.Set("keep_firing_for_minutes", *alert.Config.KeepFiringForSec/60)
} else {
_ = d.Set("keep_firing_for_minutes", nil)
}

return
}
17 changes: 17 additions & 0 deletions sysdig/resource_sysdig_monitor_alert_v2_prometheus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ func TestAccAlertV2Prometheus(t *testing.T) {
{
Config: alertV2PrometheusWithGroup(rText()),
},
{
Config: alertV2PrometheusWithKeepFiringFor(rText()),
},
{
ResourceName: "sysdig_monitor_alert_v2_prometheus.sample",
ImportState: true,
Expand Down Expand Up @@ -70,3 +73,17 @@ resource "sysdig_monitor_alert_v2_prometheus" "sample" {
}
`, name, name)
}

func alertV2PrometheusWithKeepFiringFor(name string) string {
return fmt.Sprintf(`
resource "sysdig_monitor_alert_v2_prometheus" "sample" {
name = "TERRAFORM TEST - PROMQL %s"
description = "TERRAFORM TEST - PROMQL %s"
severity = "high"
query = "(elasticsearch_jvm_memory_used_bytes{area=\"heap\"} / elasticsearch_jvm_memory_max_bytes{area=\"heap\"}) * 100 > 80"
trigger_after_minutes = 10
enabled = false
keep_firing_for_minutes = 10
}
`, name, name)
}
1 change: 1 addition & 0 deletions website/docs/r/monitor_alert_v2_prometheus.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ By defining this field, the user can add link to notificatons.
### Prometheus alert arguments

* `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`.
* `keep_firing_for_minutes` - (Optional) Alert resolution delay before actually resolving an alert.

## Attributes Reference

Expand Down
Loading