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

[datadog_monitor] add notify_by option #1599

Merged
merged 3 commits into from
Oct 19, 2022
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
7 changes: 7 additions & 0 deletions datadog/data_source_datadog_monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,12 @@ func dataSourceDatadogMonitor() *schema.Resource {
Type: schema.TypeBool,
Computed: true,
},
"notify_by": {
Description: "Controls what granularity a monitor alerts on. Only available for monitors with groupings. For instance, a monitor grouped by `cluster`, `namespace`, and `pod` can be configured to only notify on each new `cluster` violating the alert conditions by setting `notify_by` to `['cluster']`. Tags mentioned in `notify_by` must be a subset of the grouping tags in the query. For example, a query grouped by `cluster` and `namespace` cannot notify on `region`. Setting `notify_by` to `[*]` configures the monitor to notify as a simple-alert.",
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
}
}
Expand Down Expand Up @@ -328,6 +334,7 @@ func dataSourceDatadogMonitorRead(ctx context.Context, d *schema.ResourceData, m
d.Set("locked", m.Options.GetLocked())
d.Set("restricted_roles", restricted_roles)
d.Set("groupby_simple_monitor", m.Options.GetGroupbySimpleMonitor())
d.Set("notify_by", m.Options.GetNotifyBy())

if m.GetType() == datadogV1.MONITORTYPE_LOG_ALERT {
d.Set("enable_logs_sample", m.Options.GetEnableLogsSample())
Expand Down
19 changes: 19 additions & 0 deletions datadog/resource_datadog_monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,12 @@ func resourceDatadogMonitor() *schema.Resource {
Type: schema.TypeBool,
Optional: true,
},
"notify_by": {
Description: "Controls what granularity a monitor alerts on. Only available for monitors with groupings. For instance, a monitor grouped by `cluster`, `namespace`, and `pod` can be configured to only notify on each new `cluster` violating the alert conditions by setting `notify_by` to `['cluster']`. Tags mentioned in `notify_by` must be a subset of the grouping tags in the query. For example, a query grouped by `cluster` and `namespace` cannot notify on `region`. Setting `notify_by` to `[*]` configures the monitor to notify as a simple-alert.",
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
// since this is only useful for "log alert" type, we don't set a default value
// if we did set it, it would be used for all types; we have to handle this manually
// throughout the code
Expand Down Expand Up @@ -591,6 +597,15 @@ func buildMonitorStruct(d builtResource) (*datadogV1.Monitor, *datadogV1.Monitor
}
}

if attr, ok := d.GetOk("notify_by"); ok {
notifyBy := make([]string, 0)
for _, s := range attr.(*schema.Set).List() {
notifyBy = append(notifyBy, s.(string))
}
sort.Strings(notifyBy)
o.SetNotifyBy(notifyBy)
}

m := datadogV1.NewMonitor(d.Get("query").(string), monitorType)
m.SetName(d.Get("name").(string))
m.SetMessage(d.Get("message").(string))
Expand Down Expand Up @@ -908,6 +923,10 @@ func updateMonitorState(d *schema.ResourceData, meta interface{}, m *datadogV1.M
}
}

if err := d.Set("notify_by", m.Options.GetNotifyBy()); err != nil {
return diag.FromErr(err)
}

return nil
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2022-08-19T13:02:12.022524-07:00
2022-10-13T16:16:16.660353-07:00
60 changes: 35 additions & 25 deletions datadog/tests/cassettes/TestAccDatadogMonitor_LogMultiAlert.yaml

Large diffs are not rendered by default.

9 changes: 7 additions & 2 deletions datadog/tests/resource_datadog_monitor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -653,7 +653,7 @@ func TestAccDatadogMonitor_LogMultiAlert(t *testing.T) {
resource.TestCheckResourceAttr(
"datadog_monitor.foo", "type", "log alert"),
resource.TestCheckResourceAttr(
"datadog_monitor.foo", "query", "logs(\"service:foo AND type:error\").index(\"main\").rollup(\"count\").by(\"source\").last(\"5m\") > 2"),
"datadog_monitor.foo", "query", "logs(\"service:foo AND type:error\").index(\"main\").rollup(\"count\").by(\"source,status\").last(\"5m\") > 2"),
resource.TestCheckResourceAttr(
"datadog_monitor.foo", "notify_no_data", "false"),
resource.TestCheckResourceAttr(
Expand All @@ -678,6 +678,10 @@ func TestAccDatadogMonitor_LogMultiAlert(t *testing.T) {
"datadog_monitor.foo", "enable_logs_sample", "true"),
resource.TestCheckResourceAttr(
"datadog_monitor.foo", "groupby_simple_monitor", "false"),
resource.TestCheckResourceAttr(
"datadog_monitor.foo", "notify_by.#", "1"),
resource.TestCheckResourceAttr(
"datadog_monitor.foo", "notify_by.0", "status"),
),
},
},
Expand Down Expand Up @@ -1227,7 +1231,7 @@ resource "datadog_monitor" "foo" {
message = "some message Notify: @hipchat-channel"
escalation_message = "the situation has escalated @pagerduty"

query = "logs(\"service:foo AND type:error\").index(\"main\").rollup(\"count\").by(\"source\").last(\"5m\") > 2"
query = "logs(\"service:foo AND type:error\").index(\"main\").rollup(\"count\").by(\"source,status\").last(\"5m\") > 2"

monitor_thresholds {
warning = "1.0"
Expand All @@ -1249,6 +1253,7 @@ resource "datadog_monitor" "foo" {
locked = false
tags = ["foo:bar", "baz"]
enable_logs_sample = true
notify_by = ["status"]
}`, uniq)
}

Expand Down
1 change: 1 addition & 0 deletions docs/data-sources/monitor.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ data "datadog_monitor" "test" {
- `new_host_delay` (Number) Time (in seconds) allowing a host to boot and applications to fully start before starting the evaluation of monitor results.
- `no_data_timeframe` (Number) The number of minutes before the monitor notifies when data stops reporting.
- `notify_audit` (Boolean) Whether or not tagged users are notified on changes to the monitor.
- `notify_by` (Set of String) Controls what granularity a monitor alerts on. Only available for monitors with groupings. For instance, a monitor grouped by `cluster`, `namespace`, and `pod` can be configured to only notify on each new `cluster` violating the alert conditions by setting `notify_by` to `['cluster']`. Tags mentioned in `notify_by` must be a subset of the grouping tags in the query. For example, a query grouped by `cluster` and `namespace` cannot notify on `region`. Setting `notify_by` to `[*]` configures the monitor to notify as a simple-alert.
- `notify_no_data` (Boolean) Whether or not this monitor notifies when data stops reporting.
- `on_missing_data` (String) Controls how groups or monitors are treated if an evaluation does not return any data points. The default option results in different behavior depending on the monitor query type. For monitors using `Count` queries, an empty monitor evaluation is treated as 0 and is compared to the threshold conditions. For monitors using any query type other than `Count`, for example `Gauge`, `Measure`, or `Rate`, the monitor shows the last known status. This option is only available for APM Trace Analytics, Audit Trail, CI, Error Tracking, Event, Logs, and RUM monitors. Valid values are: `show_no_data`, `show_and_notify_no_data`, `resolve`, and `default`.
- `query` (String) Query of the monitor.
Expand Down
1 change: 1 addition & 0 deletions docs/resources/monitor.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ For example, if the value is set to `300` (5min), the `timeframe` is set to `las

We recommend at least 2x the monitor timeframe for metric alerts or 2 minutes for service checks.
- `notify_audit` (Boolean) A boolean indicating whether tagged users will be notified on changes to this monitor. Defaults to `false`.
- `notify_by` (Set of String) Controls what granularity a monitor alerts on. Only available for monitors with groupings. For instance, a monitor grouped by `cluster`, `namespace`, and `pod` can be configured to only notify on each new `cluster` violating the alert conditions by setting `notify_by` to `['cluster']`. Tags mentioned in `notify_by` must be a subset of the grouping tags in the query. For example, a query grouped by `cluster` and `namespace` cannot notify on `region`. Setting `notify_by` to `[*]` configures the monitor to notify as a simple-alert.
- `notify_no_data` (Boolean) A boolean indicating whether this monitor will notify when data stops reporting. Defaults to `false`.
- `on_missing_data` (String) Controls how groups or monitors are treated if an evaluation does not return any data points. The default option results in different behavior depending on the monitor query type. For monitors using `Count` queries, an empty monitor evaluation is treated as 0 and is compared to the threshold conditions. For monitors using any query type other than `Count`, for example `Gauge`, `Measure`, or `Rate`, the monitor shows the last known status. This option is only available for APM Trace Analytics, Audit Trail, CI, Error Tracking, Event, Logs, and RUM monitors. Valid values are: `show_no_data`, `show_and_notify_no_data`, `resolve`, and `default`.
- `priority` (Number) Integer from 1 (high) to 5 (low) indicating alert severity.
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module github.com/terraform-providers/terraform-provider-datadog

require (
github.com/DataDog/datadog-api-client-go/v2 v2.3.2-0.20221007190223-cae36d7feeed
github.com/DataDog/datadog-api-client-go/v2 v2.3.2-0.20221013130632-7ce5b4f69ecd
github.com/DataDog/dd-sdk-go-testing v0.0.0-20211116174033-1cd082e322ad
github.com/dnaeon/go-vcr v1.0.1
github.com/hashicorp/go-cleanhttp v0.5.2
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/DataDog/datadog-api-client-go/v2 v2.3.2-0.20221007190223-cae36d7feeed h1:iGgwV2cXBVsDK0HK83hkycaXjET/daCtmXW8CscYmkw=
github.com/DataDog/datadog-api-client-go/v2 v2.3.2-0.20221007190223-cae36d7feeed/go.mod h1:98b/MtTwSAr/yhTfhCR1oxAqQ/4tMkdrgKH7fYiDA0g=
github.com/DataDog/datadog-api-client-go/v2 v2.3.2-0.20221013130632-7ce5b4f69ecd h1:dQYHabZBaVwABfGEs0brELsctqjKpfhwnWIDHb6jETg=
github.com/DataDog/datadog-api-client-go/v2 v2.3.2-0.20221013130632-7ce5b4f69ecd/go.mod h1:98b/MtTwSAr/yhTfhCR1oxAqQ/4tMkdrgKH7fYiDA0g=
github.com/DataDog/datadog-go v4.4.0+incompatible/go.mod h1:LButxg5PwREeZtORoXG3tL4fMGNddJ+vMq1mwgfaqoQ=
github.com/DataDog/datadog-go v4.8.3+incompatible h1:fNGaYSuObuQb5nzeTQqowRAd9bpDIRRV4/gUtIBjh8Q=
github.com/DataDog/datadog-go v4.8.3+incompatible/go.mod h1:LButxg5PwREeZtORoXG3tL4fMGNddJ+vMq1mwgfaqoQ=
Expand Down