Class: Callcounter::Capturer

Inherits:
Object
  • Object
show all
Defined in:
lib/callcounter/capturer.rb

Overview

class that captures the rack requests and sends them to Callcounter

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#bufferObject

Returns the value of attribute buffer.



8
9
10
# File 'lib/callcounter/capturer.rb', line 8

def buffer
  @buffer
end

Instance Method Details

#app_tokenObject



41
42
43
# File 'lib/callcounter/capturer.rb', line 41

def app_token
  Callcounter.configuration.app_token || project_token
end

#async?Boolean

Returns:

  • (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

Returns:

  • (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_tokenObject



37
38
39
# File 'lib/callcounter/capturer.rb', line 37

def project_token
  Callcounter.configuration.project_token
end

#send_bufferObject



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

Returns:

  • (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

Returns:

  • (Boolean)


33
34
35
# File 'lib/callcounter/capturer.rb', line 33

def track?(request)
  Callcounter.configuration.track.call(request)
end