Class: Kennel::Models::Monitor
- Includes:
- TagsValidation
- Defined in:
- lib/kennel/models/monitor.rb
Constant Summary collapse
- OPTIONAL_SERVICE_CHECK_THRESHOLDS =
[:ok, :warning].freeze
- READONLY_ATTRIBUTES =
superclass::READONLY_ATTRIBUTES + [ :multi, :matching_downtimes, :overall_state_modified, :overall_state, :restricted_roles ]
- TRACKING_FIELD =
:message
- MONITOR_DEFAULTS =
{ priority: nil }.freeze
- MONITOR_OPTION_DEFAULTS =
defaults that datadog uses when options are not sent, so safe to leave out if our values match their defaults
{ evaluation_delay: nil, new_host_delay: 300, timeout_h: 0, renotify_interval: 0, notify_audit: false, no_data_timeframe: nil, # this works out ok since if notify_no_data is on, it would never be nil groupby_simple_monitor: false, variables: nil, on_missing_data: "default", # "default" is "evaluate as zero" notification_preset_name: nil, notify_by: nil }.freeze
- DEFAULT_ESCALATION_MESSAGE =
["", nil].freeze
- ALLOWED_PRIORITY_CLASSES =
[NilClass, Integer].freeze
- SKIP_NOTIFY_NO_DATA_TYPES =
["event alert", "event-v2 alert", "log alert"].freeze
Constants inherited from Record
Record::ALLOWED_KENNEL_ID_CHARS, Record::ALLOWED_KENNEL_ID_FULL, Record::ALLOWED_KENNEL_ID_REGEX, Record::ALLOWED_KENNEL_ID_SEGMENT, Record::LOCK, Record::MARKER_TEXT, Record::TITLE_FIELDS, Record::TRACKING_FIELDS
Constants included from OptionalValidations
OptionalValidations::UNIGNORABLE, OptionalValidations::UNUSED_IGNORES
Constants inherited from Base
Base::SETTING_OVERRIDABLE_METHODS
Constants included from SettingsAsMethods
SettingsAsMethods::AS_PROCS, SettingsAsMethods::SETTING_OVERRIDABLE_METHODS
Instance Attribute Summary
Attributes inherited from Record
Class Method Summary collapse
- .api_resource ⇒ Object
- .normalize(expected, actual) ⇒ Object
- .parse_url(url) ⇒ Object
- .url(id) ⇒ Object
Instance Method Summary collapse
- #build_json ⇒ Object
- #resolve_linked_tracking_ids!(id_map, **args) ⇒ Object
- #validate_update!(diffs) ⇒ Object
Methods inherited from Record
#add_tracking_id, api_resource_map, #build, #diff, #initialize, #invalid_update!, parse_any_url, parse_tracking_id, #remove_tracking_id, remove_tracking_id, #safe_tracking_id, #tracking_id
Methods included from OptionalValidations
filter_validation_errors, included, #initialize, #invalid!, valid?
Methods inherited from Base
Methods included from SubclassTracking
#recursive_subclasses, #subclasses
Methods included from SettingsAsMethods
included, #initialize, #raise_with_location
Constructor Details
This class inherits a constructor from Kennel::Models::Record
Class Method Details
.api_resource ⇒ Object
185 186 187 |
# File 'lib/kennel/models/monitor.rb', line 185 def self.api_resource "monitor" end |
.normalize(expected, actual) ⇒ Object
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 |
# File 'lib/kennel/models/monitor.rb', line 205 def self.normalize(expected, actual) super ignore_default(expected, actual, MONITOR_DEFAULTS) = actual.fetch(:options) .delete(:silenced) # we do not manage silenced, so ignore it when diffing # fields are not returned when set to true if ["service check", "event alert"].include?(actual[:type]) [:include_tags] = true unless .key?(:include_tags) [:require_full_window] = false unless .key?(:require_full_window) end case actual[:type] when "event alert" # setting nothing results in thresholds not getting returned from the api [:thresholds] ||= { critical: 0 } when "service check" # fields are not returned when created with default values via UI OPTIONAL_SERVICE_CHECK_THRESHOLDS.each do |t| [:thresholds][t] ||= 1 end else # do nothing end # nil / "" / 0 are not returned from the api when set via the UI [:evaluation_delay] ||= nil = expected[:options] || {} ignore_default(, , MONITOR_OPTION_DEFAULTS) if DEFAULT_ESCALATION_MESSAGE.include?([:escalation_message]) .delete(:escalation_message) .delete(:escalation_message) end # locked is deprecated: ignored when diffing .delete(:locked) .delete(:locked) end |
.parse_url(url) ⇒ Object
193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/kennel/models/monitor.rb', line 193 def self.parse_url(url) # datadog uses / for show and # for edit as separator in it's links id = url[/\/monitors[\/#](\d+)/, 1] # slo alert url id ||= url[/\/slo\/edit\/[a-z\d]{10,}\/alerts\/(\d+)/, 1] return unless id Integer(id) end |
.url(id) ⇒ Object
189 190 191 |
# File 'lib/kennel/models/monitor.rb', line 189 def self.url(id) Utils.path_to_url "/monitors/#{id}/edit" end |
Instance Method Details
#build_json ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/kennel/models/monitor.rb', line 70 def build_json data = super.merge( name: "#{name}#{LOCK}", type: type, query: query.strip, message: .strip, tags: , priority: priority, options: { timeout_h: timeout_h, notify_no_data: notify_no_data, no_data_timeframe: notify_no_data ? no_data_timeframe : nil, notify_audit: notify_audit, require_full_window: require_full_window, new_host_delay: new_host_delay, new_group_delay: new_group_delay, include_tags: true, escalation_message: Utils.presence(.strip), evaluation_delay: evaluation_delay, locked: false, # deprecated: setting this to true will likely fail renotify_interval: renotify_interval || 0, variables: variables } ) = data[:options] if data.fetch(:type) != "composite" thresholds = ([:thresholds] = { critical: critical }) # warning, ok, critical_recovery, and warning_recovery are optional [:warning, :ok, :critical_recovery, :warning_recovery].each do |key| if (value = send(key)) thresholds[key] = value end end thresholds[:critical] = critical unless case data.fetch(:type) when "service check" # avoid diff for default values of 1 OPTIONAL_SERVICE_CHECK_THRESHOLDS.each { |t| thresholds[t] ||= 1 } when "query alert" # metric and query values are stored as float by datadog thresholds.each { |k, v| thresholds[k] = Float(v) } end end # set without causing lots of nulls to be stored if (notify_by_value = notify_by) [:notify_by] = notify_by_value end # setting this via the api breaks the UI with # "The no_data_timeframe option is not allowed for log alert monitors" if data.fetch(:type) == "log alert" .delete :no_data_timeframe end if (windows = threshold_windows) [:threshold_windows] = windows end if (schedule = ) [:scheduling_options] = schedule end # Datadog requires only either new_group_delay or new_host_delay, never both .delete([:new_group_delay] ? :new_host_delay : :new_group_delay) # Add in statuses where we would re notify on. Possible values: alert, no data, warn if [:renotify_interval] != 0 statuses = ["alert"] statuses << "no data" if [:notify_no_data] statuses << "warn" if .dig(:thresholds, :warning) [:renotify_statuses] = statuses end # on_missing_data cannot be used with notify_no_data or no_data_timeframe # TODO migrate everything to only use on_missing_data if data.fetch(:type) == "event-v2 alert" || on_missing_data != "default" [:on_missing_data] = on_missing_data [:notify_no_data] = false # cannot set nil or it's an endless update loop .delete :no_data_timeframe end # only set when needed to avoid big diff if (notification_preset_name = notification_preset_name()) [:notification_preset_name] = notification_preset_name end # locked is deprecated, will fail if used .delete :locked data end |
#resolve_linked_tracking_ids!(id_map, **args) ⇒ Object
166 167 168 169 170 171 172 173 174 175 |
# File 'lib/kennel/models/monitor.rb', line 166 def resolve_linked_tracking_ids!(id_map, **args) case as_json[:type] when "composite", "slo alert" type = (as_json[:type] == "composite" ? :monitor : :slo) as_json[:query] = as_json[:query].gsub(/%{(.*?)}/) do resolve($1, type, id_map, **args) || $& end else # do nothing end end |
#validate_update!(diffs) ⇒ Object
177 178 179 180 181 182 183 |
# File 'lib/kennel/models/monitor.rb', line 177 def validate_update!(diffs) # ensure type does not change, but not if it's metric->query which is supported and used by importer.rb _, path, from, to = diffs.detect { |_, path, _, _| path == "type" } if path && !(from == "metric alert" && to == "query alert") invalid_update!(path, from, to) end end |