From b54543cdb676cb752a53ead3d94a898b3175b44d Mon Sep 17 00:00:00 2001 From: Alexandra Bueno Date: Wed, 13 Feb 2019 10:44:36 -0700 Subject: [PATCH] add support for enable_logs_sample option in log alert monitors --- datadog/resource_datadog_monitor.go | 16 ++++++ datadog/resource_datadog_monitor_test.go | 70 ++++++++++++++++++++++++ website/docs/r/monitor.html.markdown | 1 + 3 files changed, 87 insertions(+) diff --git a/datadog/resource_datadog_monitor.go b/datadog/resource_datadog_monitor.go index 33c272c107..1a425188b7 100644 --- a/datadog/resource_datadog_monitor.go +++ b/datadog/resource_datadog_monitor.go @@ -150,6 +150,11 @@ func resourceDatadogMonitor() *schema.Resource { Optional: true, Elem: &schema.Schema{Type: schema.TypeString}, }, + "enable_logs_sample": { + Type: schema.TypeBool, + Optional: true, + Default: false, + }, }, } } @@ -227,6 +232,10 @@ func buildMonitorStruct(d *schema.ResourceData) *datadog.Monitor { Options: &o, } + if attr, ok := d.GetOk("enable_logs_sample"); ok && m.GetType() == "log alert" { + o.SetEnableLogsSample(attr.(bool)) + } + if attr, ok := d.GetOk("tags"); ok { tags := []string{} for _, s := range attr.([]interface{}) { @@ -327,6 +336,10 @@ func resourceDatadogMonitorRead(d *schema.ResourceData, meta interface{}) error d.Set("require_full_window", m.Options.GetRequireFullWindow()) // TODO Is this one of those options that we neeed to check? d.Set("locked", m.Options.GetLocked()) + if m.GetType() == "log alert" { + d.Set("enable_logs_sample", m.Options.GetEnableLogsSample()) + } + return nil } @@ -421,6 +434,9 @@ func resourceDatadogMonitorUpdate(d *schema.ResourceData, meta interface{}) erro if attr, ok := d.GetOk("locked"); ok { o.SetLocked(attr.(bool)) } + if attr, ok := d.GetOk("enable_logs_sample"); ok && m.GetType() == "log alert" { + o.SetEnableLogsSample(attr.(bool)) + } m.Options = &o diff --git a/datadog/resource_datadog_monitor_test.go b/datadog/resource_datadog_monitor_test.go index 40b3a42cdc..67df67e6c0 100644 --- a/datadog/resource_datadog_monitor_test.go +++ b/datadog/resource_datadog_monitor_test.go @@ -468,6 +468,46 @@ func TestAccDatadogMonitor_Basic_float_int(t *testing.T) { }) } +func TestAccDatadogMonitor_Log(t *testing.T) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckDatadogMonitorDestroy, + Steps: []resource.TestStep{ + { + Config: testAccCheckDatadogMonitorConfigLogAlert, + Check: resource.ComposeTestCheckFunc( + testAccCheckDatadogMonitorExists("datadog_monitor.foo"), + resource.TestCheckResourceAttr( + "datadog_monitor.foo", "name", "name for monitor foo"), + resource.TestCheckResourceAttr( + "datadog_monitor.foo", "message", "some message Notify: @hipchat-channel"), + resource.TestCheckResourceAttr( + "datadog_monitor.foo", "type", "log alert"), + resource.TestCheckResourceAttr( + "datadog_monitor.foo", "query", "logs(\"service:foo AND type:error\").index(\"main\").rollup(\"count\").last(\"5m\") > 100"), + resource.TestCheckResourceAttr( + "datadog_monitor.foo", "notify_no_data", "false"), + resource.TestCheckResourceAttr( + "datadog_monitor.foo", "renotify_interval", "60"), + resource.TestCheckResourceAttr( + "datadog_monitor.foo", "thresholds.ok", "0.0"), + resource.TestCheckResourceAttr( + "datadog_monitor.foo", "thresholds.warning", "1.0"), + resource.TestCheckResourceAttr( + "datadog_monitor.foo", "thresholds.warning_recovery", "0.5"), + resource.TestCheckResourceAttr( + "datadog_monitor.foo", "thresholds.critical", "2.0"), + resource.TestCheckResourceAttr( + "datadog_monitor.foo", "thresholds.critical_recovery", "1.5"), + resource.TestCheckResourceAttr( + "datadog_monitor.foo", "enable_logs_sample", "true"), + ), + }, + }, + }) +} + func testAccCheckDatadogMonitorDestroy(s *terraform.State) error { client := testAccProvider.Meta().(*datadog.Client) @@ -742,6 +782,36 @@ EOF } ` +const testAccCheckDatadogMonitorConfigLogAlert = ` +resource "datadog_monitor" "foo" { + name = "name for monitor foo" + type = "log alert" + message = "some message Notify: @hipchat-channel" + escalation_message = "the situation has escalated @pagerduty" + + query = "logs(\"service:foo AND type:error\").index(\"main\").rollup(\"count\").last(\"5m\") > 2" + + thresholds { + warning = "1.0" + critical = "2.0" + warning_recovery = "0.5" + critical_recovery = "1.5" + } + + renotify_interval = 60 + + notify_audit = false + timeout_h = 60 + new_host_delay = 600 + evaluation_delay = 700 + include_tags = true + require_full_window = true + locked = false + tags = ["foo:bar", "baz"] + enable_logs_sample = true +} +` + func destroyHelper(s *terraform.State, client *datadog.Client) error { for _, r := range s.RootModule().Resources { i, _ := strconv.Atoi(r.Primary.ID) diff --git a/website/docs/r/monitor.html.markdown b/website/docs/r/monitor.html.markdown index e093f98921..29ec97c87e 100644 --- a/website/docs/r/monitor.html.markdown +++ b/website/docs/r/monitor.html.markdown @@ -108,6 +108,7 @@ The following arguments are supported: * `timeout_h` (Optional) The number of hours of the monitor not reporting data before it will automatically resolve from a triggered state. Defaults to false. * `include_tags` (Optional) A boolean indicating whether notifications from this monitor will automatically insert its +* `enable_logs_sample` (Optional) A boolean indicating whether or not to include a list of log values which triggered the alert. Defaults to false. This is only used by log monitors. triggering tags into the title. Defaults to true. * `require_full_window` (Optional) A boolean indicating whether this monitor needs a full window of data before it's evaluated. We highly recommend you set this to False for sparse metrics, otherwise some evaluations will be skipped.