Class: AppMonit::Rails::Worker
- Inherits:
-
Object
- Object
- AppMonit::Rails::Worker
- Defined in:
- lib/app_monit/rails/worker.rb
Constant Summary collapse
- MUTEX =
Mutex.new
- MAX_MULTIPLIER =
5
Instance Attribute Summary collapse
-
#errors ⇒ Object
Returns the value of attribute errors.
-
#requests ⇒ Object
Returns the value of attribute requests.
Class Method Summary collapse
Instance Method Summary collapse
- #convert_errors_to_events ⇒ Object
- #convert_requests_to_events ⇒ Object
- #current_minute ⇒ Object
- #events ⇒ Object
-
#initialize ⇒ Worker
constructor
A new instance of Worker.
- #push(event) ⇒ Object
- #reset ⇒ Object
- #send_to_collector ⇒ Object
- #start ⇒ Object
- #start_flusher ⇒ Object
- #update_error(event) ⇒ Object
- #update_event(event) ⇒ Object
Constructor Details
#initialize ⇒ Worker
Returns a new instance of Worker.
19 20 21 22 23 24 |
# File 'lib/app_monit/rails/worker.rb', line 19 def initialize @queue = Queue.new @multiplier = 1 @flush_rate = 60 reset end |
Instance Attribute Details
#errors ⇒ Object
Returns the value of attribute errors.
7 8 9 |
# File 'lib/app_monit/rails/worker.rb', line 7 def errors @errors end |
#requests ⇒ Object
Returns the value of attribute requests.
7 8 9 |
# File 'lib/app_monit/rails/worker.rb', line 7 def requests @requests end |
Class Method Details
.instance ⇒ Object
10 11 12 13 14 15 16 |
# File 'lib/app_monit/rails/worker.rb', line 10 def instance return @instance if @instance MUTEX.synchronize do return @instance if @instance @instance = new.start end end |
Instance Method Details
#convert_errors_to_events ⇒ Object
107 108 109 110 111 112 113 114 |
# File 'lib/app_monit/rails/worker.rb', line 107 def convert_errors_to_events @errors.flat_map do |minute, endpoints| endpoints.collect do |endpoint, duration| payload = duration.merge(application: Config.name, endpoint: endpoint) { created_at: Time.at(minute), name: 'page_error', payload: payload } end end end |
#convert_requests_to_events ⇒ Object
98 99 100 101 102 103 104 105 |
# File 'lib/app_monit/rails/worker.rb', line 98 def convert_requests_to_events @requests.flat_map do |minute, endpoints| endpoints.collect do |endpoint, durations| payload = durations.merge(application: Config.name, endpoint: endpoint) { application: Config.name, created_at: Time.at(minute), name: 'page_load', payload: payload } end end end |
#current_minute ⇒ Object
41 42 43 44 |
# File 'lib/app_monit/rails/worker.rb', line 41 def current_minute time = Time.now.to_i time - (time % 60) end |
#events ⇒ Object
116 117 118 |
# File 'lib/app_monit/rails/worker.rb', line 116 def events convert_requests_to_events + convert_errors_to_events end |
#push(event) ⇒ Object
94 95 96 |
# File 'lib/app_monit/rails/worker.rb', line 94 def push(event) @queue << event end |
#reset ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/app_monit/rails/worker.rb', line 26 def reset @requests = Hash.new do |requests, minutes| requests[minutes] = Hash.new do |minute, endpoints| minute[endpoints] = Hash.new do |endpoint, duration| endpoint[duration] = { sum: 0, count: 0, average: 0 } end end end @errors = Hash.new do |errors, minutes| errors[minutes] = Hash.new do |minute, endpoint| minute[endpoint] = { sum: 0, count: 0, average: 0 } end end end |
#send_to_collector ⇒ Object
120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/app_monit/rails/worker.rb', line 120 def send_to_collector AppMonit::Rails.logger.debug "Sending to collector" if events.any? AppMonit::Http.post('/v1/events', event: events) @requests.clear @errors.clear end reset end |
#start ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/app_monit/rails/worker.rb', line 46 def start Thread.new do while (event = @queue.pop) begin case event when Error update_error(event) when Event update_event(event) when :flush send_to_collector end rescue Exception => e AppMonit::Rails.logger.debug ["Event error:", event.inspect, e.] end end end start_flusher self end |
#start_flusher ⇒ Object
85 86 87 88 89 90 91 92 |
# File 'lib/app_monit/rails/worker.rb', line 85 def start_flusher Thread.new do loop do sleep @multiplier * @flush_rate push(:flush) end end end |
#update_error(event) ⇒ Object
68 69 70 71 72 73 |
# File 'lib/app_monit/rails/worker.rb', line 68 def update_error(event) bucket = @errors[event.minute][event.endpoint] bucket[:sum] += event.duration bucket[:count] += 1 bucket[:average] = (bucket[:sum] / bucket[:count].to_f) end |
#update_event(event) ⇒ Object
76 77 78 79 80 81 82 83 |
# File 'lib/app_monit/rails/worker.rb', line 76 def update_event(event) event.durations.each do |duration, value| bucket = @requests[event.minute][event.endpoint][duration] bucket[:sum] += value bucket[:count] += 1 bucket[:average] = (bucket[:sum] / bucket[:count].to_f) end end |