Class: NeetoMonitorRuby::Monitor
- Inherits:
-
Object
- Object
- NeetoMonitorRuby::Monitor
- Includes:
- MonitorUtils
- Defined in:
- lib/neeto_monitor_ruby/monitor.rb
Constant Summary collapse
- REQUEST_HEADERS =
{ 'Content-Type': "application/json", 'Accept': "application/json", 'User-Agent': "neeto-monitor-ruby" }.freeze
- MAX_RETRY_COUNT =
3
- BULK_CREATE_PATH =
"/api/v1/clients/bulk_checks".freeze
Constants included from MonitorUtils
NeetoMonitorRuby::MonitorUtils::JOB_STATES, NeetoMonitorRuby::MonitorUtils::MONITOR_TYPES
Instance Attribute Summary collapse
-
#api_key ⇒ Object
readonly
Returns the value of attribute api_key.
-
#environment ⇒ Object
readonly
Returns the value of attribute environment.
-
#monitor_key ⇒ Object
readonly
Returns the value of attribute monitor_key.
Class Method Summary collapse
- .delete_old_heartbeat_jobs! ⇒ Object
- .load_heartbeats! ⇒ Object
- .load_monitors! ⇒ Object
- .make_bulk_create_request(check_params) ⇒ Object
Instance Method Summary collapse
-
#initialize(monitor_key, api_key: nil, environment: nil) ⇒ Monitor
constructor
A new instance of Monitor.
- #job_ping(params) ⇒ Object
- #ping(params = {}) ⇒ Object
Methods included from MonitorUtils
#generate_series, #generate_stamp, included, #random_string
Constructor Details
#initialize(monitor_key, api_key: nil, environment: nil) ⇒ Monitor
Returns a new instance of Monitor.
90 91 92 93 94 |
# File 'lib/neeto_monitor_ruby/monitor.rb', line 90 def initialize(monitor_key, api_key: nil, environment: nil) @monitor_key = monitor_key @api_key = api_key || NeetoMonitorRuby.config.api_key @environment = environment || NeetoMonitorRuby.config.environment end |
Instance Attribute Details
#api_key ⇒ Object (readonly)
Returns the value of attribute api_key.
88 89 90 |
# File 'lib/neeto_monitor_ruby/monitor.rb', line 88 def api_key @api_key end |
#environment ⇒ Object (readonly)
Returns the value of attribute environment.
88 89 90 |
# File 'lib/neeto_monitor_ruby/monitor.rb', line 88 def environment @environment end |
#monitor_key ⇒ Object (readonly)
Returns the value of attribute monitor_key.
88 89 90 |
# File 'lib/neeto_monitor_ruby/monitor.rb', line 88 def monitor_key @monitor_key end |
Class Method Details
.delete_old_heartbeat_jobs! ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/neeto_monitor_ruby/monitor.rb', line 75 def delete_old_heartbeat_jobs! if ActiveJob::Base.queue_adapter.respond_to?(:enqueued_jobs) ActiveJob::Base.queue_adapter.enqueued_jobs.reject! do |job| job["job_class"] == "NeetoMonitorRuby::HeartbeatRunnerJob" end elsif ActiveJob::Base.queue_adapter.class == ActiveJob::QueueAdapters::SidekiqAdapter Sidekiq::ScheduledSet.new.scan("NeetoMonitorRuby::HeartbeatRunnerJob").each do |job| job.delete if job.display_class == "NeetoMonitorRuby::HeartbeatRunnerJob" end end end |
.load_heartbeats! ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/neeto_monitor_ruby/monitor.rb', line 60 def load_heartbeats! NeetoMonitor.logger.debug "Loading heartbeats!!!" NeetoMonitor.logger.debug "NeetoMonitor.config.monitors #{NeetoMonitor.config.monitors}" heartbeats = NeetoMonitor.config.monitors[:heartbeats] prefix = NeetoMonitor.config.key_prefix_enabled ? NeetoMonitor.config.key_prefix : nil heartbeats.each do |heartbeat| heartbeat_name = prefix ? [prefix, heartbeat[:name]].join("::") : heartbeat[:name] delete_old_heartbeat_jobs! HeartbeatRunnerJob.perform_later(heartbeat_name, heartbeat[:schedule]) end end |
.load_monitors! ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/neeto_monitor_ruby/monitor.rb', line 21 def load_monitors! return unless NeetoMonitorRuby.config.api_key config_monitors = NeetoMonitorRuby.config.monitors&.except(:heartbeats) if !config_monitors.blank? environment = NeetoMonitorRuby.config.environment all_monitors = config_monitors.each_with_object([]) do |(type, monitors), result| monitors.each { |m| result << m.merge(kind: type.to_s.chop, environment:) } end make_bulk_create_request({ checks: all_monitors, api_key: NeetoMonitorRuby.config.api_key }) else NeetoMonitorRuby.logger.debug "No monitors found in config" end end |
.make_bulk_create_request(check_params) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/neeto_monitor_ruby/monitor.rb', line 39 def make_bulk_create_request(check_params) bulk_monitors_url = "#{NeetoMonitorRuby.config.base_url}#{BULK_CREATE_PATH}" response = HTTParty.post( bulk_monitors_url, body: check_params.to_json, headers: REQUEST_HEADERS ) result = response.code >= 200 && response.code < 300 NeetoMonitor.logger.error "Bulk create response code: #{response.code}" unless result load_heartbeats! if defined?(ActiveJob::Base) result rescue StandardError => exception NeetoMonitor.logger.error exception. NeetoMonitor.logger.error exception.backtrace.join("\n") end |
Instance Method Details
#job_ping(params) ⇒ Object
111 112 113 |
# File 'lib/neeto_monitor_ruby/monitor.rb', line 111 def job_ping(params) ping(params.merge(kind: MONITOR_TYPES[:job])) end |
#ping(params = {}) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/neeto_monitor_ruby/monitor.rb', line 96 def ping(params = {}) if api_key.nil? log_error "No API key is provided, please pass the api_key or configure." return false end retry_count = params[:retry_count] || 0 response = make_ping_request(params) response.code >= 200 && response.code < 300 rescue StandardError => exception return false if retry_count >= MAX_RETRY_COUNT retry_ping(params, retry_count) end |