Class: Barnes::Reporter

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

Overview

The reporter is used to send stats to the server.

Example:

statsd   = Statsd.new('127.0.0.1', "8125")
reporter = Reporter.new(statsd: , sample_rate: 10)
reporter.report_statsd('barnes.counters' => {"hello" => 2})

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(statsd:, sample_rate:) ⇒ Reporter

Returns a new instance of Reporter.



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

def initialize(statsd: , sample_rate:)
  @statsd      = statsd
  @sample_rate = sample_rate.to_f

  if @statsd.respond_to?(:easy)
    @statsd_method = statsd.method(:easy)
  else
    @statsd_method = statsd.method(:batch)
  end
end

Instance Attribute Details

#sample_rateObject

Returns the value of attribute sample_rate.



33
34
35
# File 'lib/barnes/reporter.rb', line 33

def sample_rate
  @sample_rate
end

#statsdObject

Returns the value of attribute statsd.



33
34
35
# File 'lib/barnes/reporter.rb', line 33

def statsd
  @statsd
end

Instance Method Details

#report(env) ⇒ Object



46
47
48
# File 'lib/barnes/reporter.rb', line 46

def report(env)
  report_statsd env if @statsd
end

#report_statsd(env) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/barnes/reporter.rb', line 50

def report_statsd(env)
  @statsd_method.call do |statsd|
    env[Barnes::COUNTERS].each do |metric, value|
      statsd.count(:"Rack.Server.All.#{metric}", value, @sample_rate)
    end

    # for :gauge, use sample rate of 1, since gauges in statsd have no sampling characteristics.
    env[Barnes::GAUGES].each do |metric, value|
      statsd.gauge(:"Rack.Server.All.#{metric}", value, 1.0)
    end
  end
end