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

Add support for enable_logs_sample option in log monitors #151

Merged
merged 1 commit into from
Apr 17, 2019
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
16 changes: 16 additions & 0 deletions datadog/resource_datadog_monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
},
}
}
Expand Down Expand Up @@ -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{}) {
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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

Expand Down
70 changes: 70 additions & 0 deletions datadog/resource_datadog_monitor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions website/docs/r/monitor.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down