Skip to content

Commit

Permalink
default require_full_window to false to match dd behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
grosser committed Oct 3, 2023
1 parent 3e0bc2a commit 4687cc8
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 33 deletions.
13 changes: 5 additions & 8 deletions lib/kennel/models/monitor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ class Monitor < Record
variables: -> { MONITOR_OPTION_DEFAULTS.fetch(:variables) },
on_missing_data: -> { MONITOR_OPTION_DEFAULTS.fetch(:on_missing_data) },
notification_preset_name: -> { MONITOR_OPTION_DEFAULTS.fetch(:notification_preset_name) },
notify_by: -> { MONITOR_OPTION_DEFAULTS.fetch(:notify_by) }
notify_by: -> { MONITOR_OPTION_DEFAULTS.fetch(:notify_by) },
require_full_window: -> { false }
)

def build_json
Expand Down Expand Up @@ -205,7 +206,7 @@ def self.normalize(expected, actual)
# fields are not returned when set to true
if ["service check", "event alert"].include?(actual[:type])
options[:include_tags] = true unless options.key?(:include_tags)
options[:require_full_window] = true unless options.key?(:require_full_window)
options[:require_full_window] = false unless options.key?(:require_full_window)
end

case actual[:type]
Expand All @@ -218,6 +219,7 @@ def self.normalize(expected, actual)
OPTIONAL_SERVICE_CHECK_THRESHOLDS.each do |t|
options[:thresholds][t] ||= 1
end
else # do nothing
end

# nil / "" / 0 are not returned from the api when set via the UI
Expand All @@ -233,12 +235,6 @@ def self.normalize(expected, actual)

private

def require_full_window
# default 'on_average', 'at_all_times', 'in_total' aggregations to true, otherwise false
# https://docs.datadoghq.com/ap/#create-a-monitor
type != "query alert" || query.start_with?("avg", "min", "sum")
end

def validate_json(data)
super

Expand Down Expand Up @@ -358,6 +354,7 @@ def validate_thresholds(data)
"Warning threshold (#{warning}) must be less than the alert threshold (#{critical}) with > comparison"
)
end
else # do nothing
end
end
end
Expand Down
27 changes: 2 additions & 25 deletions test/kennel/models/monitor_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def monitor(options = {})
notify_no_data: true,
no_data_timeframe: 60,
notify_audit: false,
require_full_window: true,
require_full_window: false,
new_host_delay: 300,
include_tags: true,
escalation_message: nil,
Expand Down Expand Up @@ -546,7 +546,7 @@ def diff_resource(e, a)
diff_resource({}, {}).must_equal []
end

it "ignores include_tags/require_full_window for service alerts" do
it "ignores missing include_tags and require_full_window for service alerts" do
expected_basic_json[:query] = "foo.by(x)"
expected_basic_json[:options].delete(:include_tags)
expected_basic_json[:options].delete(:require_full_window)
Expand All @@ -567,7 +567,6 @@ def diff_resource(e, a)
it "ignores missing critical from event alert" do
assert expected_basic_json[:query].sub!("123.0", "0")
expected_basic_json[:options].delete(:thresholds)
expected_basic_json[:options][:require_full_window] = true
diff_resource(
{
type: -> { "event alert" },
Expand Down Expand Up @@ -672,26 +671,4 @@ def diff_resource(e, a)
actual.must_equal(options: {})
end
end

describe "#require_full_window" do
it "is true for on_average query" do
assert monitor.build_json.dig(:options, :require_full_window)
end

it "is true for at_all_times query" do
assert monitor(query: -> { "min(last_5m) > #{critical}" }).build_json.dig(:options, :require_full_window)
end

it "is true for in_total query" do
assert monitor(query: -> { "sum(last_5m) > #{critical}" }).build_json.dig(:options, :require_full_window)
end

it "is false for at_least_once query" do
refute monitor(query: -> { "max(last_5m) > #{critical}" }).build_json.dig(:options, :require_full_window)
end

it "is true for non-query" do
assert monitor(type: -> { "foo bar" }).build_json.dig(:options, :require_full_window)
end
end
end

0 comments on commit 4687cc8

Please sign in to comment.