class BaselineRedAgent::Dispatcher
Public Class Methods
new()
click to toggle source
# File lib/baseline_red_agent/dispatcher.rb, line 7 def initialize @start_time = Time.now @queue = Queue.new end
Public Instance Methods
add_event(event)
click to toggle source
# File lib/baseline_red_agent/dispatcher.rb, line 12 def add_event(event) @queue << event end
dispatch()
click to toggle source
# File lib/baseline_red_agent/dispatcher.rb, line 33 def dispatch begin events = drain(@queue) dispatch_events(events.dup) rescue => ex ::BaselineRedAgent.logger.error "#{ex.inspect}" ::BaselineRedAgent.logger.error "#{ex.backtrace.inspect}" ensure reset end end
dispatch_interval()
click to toggle source
# File lib/baseline_red_agent/dispatcher.rb, line 20 def dispatch_interval 30 end
queue_empty?()
click to toggle source
# File lib/baseline_red_agent/dispatcher.rb, line 16 def queue_empty? @queue.size.to_i == 0 end
ready?()
click to toggle source
# File lib/baseline_red_agent/dispatcher.rb, line 24 def ready? Time.now > @start_time + dispatch_interval.to_f && !queue_empty? end
reset()
click to toggle source
# File lib/baseline_red_agent/dispatcher.rb, line 28 def reset @queue.clear @start_time = Time.now end
Private Instance Methods
compress_body(data)
click to toggle source
# File lib/baseline_red_agent/dispatcher.rb, line 66 def compress_body(data) body = MessagePack.pack({ "host" => BaselineRedAgent.hostname, "data" => data }) compressed_body = Zlib::Deflate.deflate(body, Zlib::DEFAULT_COMPRESSION) Base64.encode64(compressed_body) end
dispatch_events(data)
click to toggle source
# File lib/baseline_red_agent/dispatcher.rb, line 47 def dispatch_events(data) if data && data.length > 0 uri = URI(url) sock = Net::HTTP.new(uri.host, uri.port) sock.use_ssl = BaselineRedAgent.options[:ssl] req = Net::HTTP::Post.new(uri.path, { "Content-Type" => "application/json", "Accept-Encoding" => "gzip", "User-Agent" => "gzip" }) req.body = compress_body(data) req.content_type = "application/octet-stream" res = sock.start do |http| http.read_timeout = 30 http.request(req) end data.clear end end
drain(queue)
click to toggle source
# File lib/baseline_red_agent/dispatcher.rb, line 76 def drain(queue) Array.new(queue.size) { queue.pop } end
protocol()
click to toggle source
# File lib/baseline_red_agent/dispatcher.rb, line 80 def protocol @protocol ||= BaselineRedAgent.options[:ssl] ? "https" : "http" end
url()
click to toggle source
# File lib/baseline_red_agent/dispatcher.rb, line 84 def url @url ||= "#{protocol}://#{BaselineRedAgent.options[:host] || "baseline.red"}/api/listener/2/#{BaselineRedAgent.options[:license_key]}" end