Class: Barnes::Reporter

Inherits:
Object
  • Object
show all
Defined in:
lib/barnes/reporter.rb

Constant Summary collapse

MAX_RETRIES =
3
HTTP_TIMEOUT =
5
ServerError =
Class.new(StandardError)

Instance Method Summary collapse

Constructor Details

#initialize(url:, backoff_sleep: ->(n) { sleep(n) }) ⇒ Reporter

Returns a new instance of Reporter.



35
36
37
38
39
40
# File 'lib/barnes/reporter.rb', line 35

def initialize(url:, backoff_sleep: ->(n) { sleep(n) })
  @uri = URI.parse(url)
  @backoff_sleep = backoff_sleep
  @mutex = Mutex.new
  @http = nil
end

Instance Method Details

#report(env) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/barnes/reporter.rb', line 42

def report(env)
  counters = {}
  env[Barnes::COUNTERS].each { |k, v| counters["Rack.Server.All.#{k}"] = v }

  gauges = {}
  env[Barnes::GAUGES].each { |k, v| gauges["Rack.Server.All.#{k}"] = v }

  count = counters.size + gauges.size
  return if count == 0

  body = JSON.generate(counters: counters, gauges: gauges)
  @mutex.synchronize { post(body, count) }
end