Class: Callcounter::Capturer
- Inherits:
-
Object
- Object
- Callcounter::Capturer
- Defined in:
- lib/callcounter/capturer.rb
Overview
class that captures the rack requests and sends them to Callcounter
Instance Attribute Summary collapse
-
#buffer ⇒ Object
Returns the value of attribute buffer.
Instance Method Summary collapse
- #app_token ⇒ Object
- #async? ⇒ Boolean
- #background_work(start, finish, env, result) ⇒ Object
- #call(env) ⇒ Object
- #debug? ⇒ Boolean
- #event_attributes(start, finish, request, result) ⇒ Object
-
#initialize(app) ⇒ Capturer
constructor
A new instance of Capturer.
- #project_token ⇒ Object
- #send_buffer ⇒ Object
- #should_send_buffer? ⇒ Boolean
- #threading(&block) ⇒ Object
- #track?(request) ⇒ Boolean
Constructor Details
#initialize(app) ⇒ Capturer
Returns a new instance of Capturer.
10 11 12 13 |
# File 'lib/callcounter/capturer.rb', line 10 def initialize(app) @app = app @buffer = [] end |
Instance Attribute Details
#buffer ⇒ Object
Returns the value of attribute buffer.
8 9 10 |
# File 'lib/callcounter/capturer.rb', line 8 def buffer @buffer end |
Instance Method Details
#app_token ⇒ Object
41 42 43 |
# File 'lib/callcounter/capturer.rb', line 41 def app_token Callcounter.configuration.app_token || project_token end |
#async? ⇒ Boolean
45 46 47 |
# File 'lib/callcounter/capturer.rb', line 45 def async? Callcounter.configuration.async end |
#background_work(start, finish, env, result) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/callcounter/capturer.rb', line 87 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) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/callcounter/capturer.rb', line 15 def call(env) start = Time.now result = @app.call(env) finish = Time.now return result if app_token.nil? threading do background_work(start, finish, env, result) end result end |
#debug? ⇒ Boolean
29 30 31 |
# File 'lib/callcounter/capturer.rb', line 29 def debug? Callcounter.configuration.debug end |
#event_attributes(start, finish, request, result) ⇒ Object
76 77 78 79 80 81 82 83 84 85 |
# File 'lib/callcounter/capturer.rb', line 76 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 ⇒ Object
37 38 39 |
# File 'lib/callcounter/capturer.rb', line 37 def project_token Callcounter.configuration.project_token end |
#send_buffer ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/callcounter/capturer.rb', line 57 def send_buffer http = Net::HTTP.new('api.callcounter.io', 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: app_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? ⇒ Boolean
72 73 74 |
# File 'lib/callcounter/capturer.rb', line 72 def should_send_buffer? @buffer.first[:created_at] < Time.now - Random.rand(300..359) || @buffer.size > 25 end |
#threading(&block) ⇒ Object
49 50 51 52 53 54 55 |
# File 'lib/callcounter/capturer.rb', line 49 def threading(&block) if async? Thread.new(&block) else yield end end |
#track?(request) ⇒ Boolean
33 34 35 |
# File 'lib/callcounter/capturer.rb', line 33 def track?(request) Callcounter.configuration.track.call(request) end |