Class: Trashed::Reporter

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

Constant Summary collapse

DEFAULT_DIMENSIONS =
[ :All ]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeReporter

Returns a new instance of Reporter.



11
12
13
14
15
16
17
18
# File 'lib/trashed/reporter.rb', line 11

def initialize
  @logger = nil
  @statsd = nil
  @timing_sample_rate = 0.1
  @gauge_sample_rate = 0.05
  @timing_dimensions  = ->(env) { DEFAULT_DIMENSIONS }
  @gauge_dimensions   = ->(env) { DEFAULT_DIMENSIONS }
end

Instance Attribute Details

#gauge_dimensionsObject

Returns the value of attribute gauge_dimensions.



7
8
9
# File 'lib/trashed/reporter.rb', line 7

def gauge_dimensions
  @gauge_dimensions
end

#gauge_sample_rateObject

Returns the value of attribute gauge_sample_rate.



6
7
8
# File 'lib/trashed/reporter.rb', line 6

def gauge_sample_rate
  @gauge_sample_rate
end

#loggerObject

Returns the value of attribute logger.



5
6
7
# File 'lib/trashed/reporter.rb', line 5

def logger
  @logger
end

#statsdObject

Returns the value of attribute statsd.



5
6
7
# File 'lib/trashed/reporter.rb', line 5

def statsd
  @statsd
end

#timing_dimensionsObject

Returns the value of attribute timing_dimensions.



7
8
9
# File 'lib/trashed/reporter.rb', line 7

def timing_dimensions
  @timing_dimensions
end

#timing_sample_rateObject

Returns the value of attribute timing_sample_rate.



6
7
8
# File 'lib/trashed/reporter.rb', line 6

def timing_sample_rate
  @timing_sample_rate
end

Instance Method Details

#report(env) ⇒ Object



20
21
22
23
# File 'lib/trashed/reporter.rb', line 20

def report(env)
  report_logger env if @logger
  report_statsd env if @statsd
end

#report_logger(env) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/trashed/reporter.rb', line 25

def report_logger(env)
  timings = env[Trashed::Rack::TIMINGS]
  parts = []

  elapsed = '%.2fms' % timings[:'Time.wall']
  if timings[:'Time.pct.cpu']
    elapsed << ' (%.1f%% cpu, %.1f%% idle)' % timings.values_at(:'Time.pct.cpu', :'Time.pct.idle')
  end
  parts << elapsed

  obj = timings[:'GC.allocated_objects'].to_i
  parts << '%d objects' % obj unless obj.zero?

  if gcs = timings[:'GC.count'].to_i
    gc = '%d GCs' % gcs
    unless gcs.zero?
      if timings.include?(:'GC.major_count')
        gc << ' (%d major, %d minor)' % timings.values_at(:'GC.major_count', :'GC.minor_count').map(&:to_i)
      end
      if timings.include?(:'GC.time')
        gc << ' took %.2fms' % timings[:'GC.time']
      end
    end
    parts << gc
  end

  oobgcs = timings[:'OOBGC.count'].to_i
  if !oobgcs.zero?
    oobgc = 'Avoided %d OOB GCs' % oobgcs
    if timings[:'OOBGC.major_count']
      oobgc << ' (%d major, %d minor, %d sweep)' % timings.values_at(:'OOBGC.major_count', :'OOBGC.minor_count', :'OOBGC.sweep_count').map(&:to_i)
    end
    if timings[:'OOBGC.time']
      oobgc << ' saving %.2fms' % timings[:'OOBGC.time']
    end
    parts << oobgc
  end

  message = "Rack handled in #{parts * '. '}."

  if @logger.respond_to?(:tagged) && env.include?('trashed.logger.tags')
    @logger.tagged env['trashed.logger.tags'] do
      @logger.info message
    end
  else
    @logger.info message
  end
end

#report_statsd(env) ⇒ Object



74
75
76
77
78
79
80
# File 'lib/trashed/reporter.rb', line 74

def report_statsd(env)
  method = @statsd.respond_to?(:easy) ? :easy : :batch
  @statsd.send(method) do |statsd|
    send_to_statsd statsd, :timing, @timing_sample_rate, env[Trashed::Rack::TIMINGS], :'Rack.Request', @timing_dimensions.call(env)
    send_to_statsd statsd, :timing, @gauge_sample_rate,  env[Trashed::Rack::GAUGES],  :'Rack.Server',  @gauge_dimensions.call(env)
  end
end

#send_to_statsd(statsd, method, sample_rate, measurements, namespace, dimensions) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/trashed/reporter.rb', line 82

def send_to_statsd(statsd, method, sample_rate, measurements, namespace, dimensions)
  measurements.each do |metric, value|
    case value
    when Array
      value.each do |v|
        send_to_statsd statsd, method, sample_rate, { metric => v }, namespace, dimensions
      end
    when Numeric
      Array(dimensions || :All).each do |dimension|
        statsd.send method, :"#{namespace}.#{dimension}.#{metric}", value, sample_rate
      end
    end
  end
end