class Callcounter::Capturer
class that captures the rack requests and sends them to callcounter.eu
Attributes
buffer[RW]
Public Class Methods
new(app)
click to toggle source
# File lib/callcounter/capturer.rb, line 10 def initialize(app) @app = app @buffer = [] end
Public Instance Methods
async?()
click to toggle source
# File lib/callcounter/capturer.rb, line 41 def async? Callcounter.configuration.async end
background_work(start, finish, env, result)
click to toggle source
# File lib/callcounter/capturer.rb, line 83 def background_work(start, finish, env, result) request = Rack::Request.new(env) return unless track?(request) @buffer << event_attributes(start, finish, request, result) return unless should_send_buffer? send_buffer @buffer = [] end
call(env)
click to toggle source
# File lib/callcounter/capturer.rb, line 15 def call(env) start = Time.now result = @app.call(env) finish = Time.now return result if project_token.nil? threading do background_work(start, finish, env, result) end result end
debug?()
click to toggle source
# File lib/callcounter/capturer.rb, line 29 def debug? Callcounter.configuration.debug end
event_attributes(start, finish, request, result)
click to toggle source
# File lib/callcounter/capturer.rb, line 72 def event_attributes(start, finish, request, result) { created_at: start, elapsed_time: ((finish - start) * 1000).round, method: request.request_method, path: request.path, user_agent: request.user_agent, status: result.first } end
project_token()
click to toggle source
# File lib/callcounter/capturer.rb, line 37 def project_token Callcounter.configuration.project_token end
send_buffer()
click to toggle source
# File lib/callcounter/capturer.rb, line 53 def send_buffer http = Net::HTTP.new('api.callcounter.eu', 443) http.use_ssl = true track = Net::HTTP::Post.new('/api/v1/events/batch') track['content-type'] = 'application/json' track['user-agent'] = "callcounter-gem (#{Callcounter::VERSION})" track.body = { batch: { project_token: project_token, events: @buffer } }.to_json http.request(track) puts 'sent request' if debug? rescue StandardError puts 'failed to send request' if debug? end
should_send_buffer?()
click to toggle source
# File lib/callcounter/capturer.rb, line 68 def should_send_buffer? @buffer.first[:created_at] < Time.now - Random.rand(300..359) || @buffer.size > 25 end
threading() { || ... }
click to toggle source
# File lib/callcounter/capturer.rb, line 45 def threading(&block) if async? Thread.new(&block) else yield end end
track?(request)
click to toggle source
# File lib/callcounter/capturer.rb, line 33 def track?(request) Callcounter.configuration.track.call(request) end