Class: Kennel::Models::Monitor

Inherits:
Record show all
Includes:
OptionalValidations
Defined in:
lib/kennel/models/monitor.rb

Constant Summary collapse

RENOTIFY_INTERVALS =

minutes

[0, 10, 20, 30, 40, 50, 60, 90, 120, 180, 240, 300, 360, 720, 1440].freeze
OPTIONAL_SERVICE_CHECK_THRESHOLDS =
[:ok, :warning].freeze
READONLY_ATTRIBUTES =
superclass::READONLY_ATTRIBUTES + [
  :multi, :matching_downtimes, :overall_state_modified, :overall_state, :restricted_roles
]
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
}.freeze
DEFAULT_ESCALATION_MESSAGE =
["", nil].freeze

Constants inherited from Record

Record::LOCK

Constants inherited from Base

Base::SETTING_OVERRIDABLE_METHODS

Constants included from SettingsAsMethods

SettingsAsMethods::SETTING_OVERRIDABLE_METHODS

Instance Attribute Summary

Attributes inherited from Record

#project

Class Method Summary collapse

Instance Method Summary collapse

Methods included from OptionalValidations

included

Methods inherited from Record

api_resource_map, #diff, #initialize, parse_any_url, #tracking_id

Methods inherited from Base

#kennel_id, #name, #to_json

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_resourceObject



122
123
124
# File 'lib/kennel/models/monitor.rb', line 122

def self.api_resource
  "monitor"
end

.normalize(expected, actual) ⇒ Object



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
165
166
167
168
169
170
171
# File 'lib/kennel/models/monitor.rb', line 136

def self.normalize(expected, actual)
  super

  ignore_default(expected, actual, MONITOR_DEFAULTS)

  options = actual.fetch(:options)
  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])
    options[:include_tags] = true unless options.key?(:include_tags)
    options[:require_full_window] = true unless options.key?(:require_full_window)
  end

  case actual[:type]
  when "event alert"
    # setting nothing results in thresholds not getting returned from the api
    options[:thresholds] ||= { critical: 0 }

  when "service check"
    # fields are not returned when created with default values via UI
    OPTIONAL_SERVICE_CHECK_THRESHOLDS.each do |t|
      options[:thresholds][t] ||= 1
    end
  end

  # nil / "" / 0 are not returned from the api when set via the UI
  options[:evaluation_delay] ||= nil

  expected_options = expected[:options] || {}
  ignore_default(expected_options, options, MONITOR_OPTION_DEFAULTS)
  if DEFAULT_ESCALATION_MESSAGE.include?(options[:escalation_message])
    options.delete(:escalation_message)
    expected_options.delete(:escalation_message)
  end
end

.parse_url(url) ⇒ Object

datadog uses / for show and # for edit as separator in it’s links



131
132
133
134
# File 'lib/kennel/models/monitor.rb', line 131

def self.parse_url(url)
  return unless id = url[/\/monitors[\/#](\d+)/, 1]
  Integer(id)
end

.url(id) ⇒ Object



126
127
128
# File 'lib/kennel/models/monitor.rb', line 126

def self.url(id)
  Utils.path_to_url "/monitors##{id}/edit"
end

Instance Method Details

#as_jsonObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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
# File 'lib/kennel/models/monitor.rb', line 55

def as_json
  return @as_json if @as_json
  data = {
    name: "#{name}#{LOCK}",
    type: type,
    query: query.strip,
    message: message.strip,
    tags: tags.uniq,
    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,
      include_tags: true,
      escalation_message: Utils.presence(escalation_message.strip),
      evaluation_delay: evaluation_delay,
      locked: false, # setting this to true prevents any edit and breaks updates when using replace workflow
      renotify_interval: renotify_interval || 0
    }
  }

  data[:id] = id if id

  options = data[:options]
  if data.fetch(:type) != "composite"
    thresholds = (options[: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

  if windows = threshold_windows
    options[:threshold_windows] = windows
  end

  validate_json(data) if validate

  @as_json = data
end

#resolve_linked_tracking_ids!(id_map, **args) ⇒ Object



112
113
114
115
116
117
118
119
120
# File 'lib/kennel/models/monitor.rb', line 112

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_link($1, type, id_map, **args) || $&
    end
  end
end