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.



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

def initialize
  @logger = nil
  @statsd = nil
  @sample_rate = 1.0
  @timing_dimensions  = ->(env) { DEFAULT_DIMENSIONS }
  @gauge_dimensions   = ->(env) { DEFAULT_DIMENSIONS }
end

Instance Attribute Details

#gauge_dimensionsObject

Returns the value of attribute gauge_dimensions.



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

def gauge_dimensions
  @gauge_dimensions
end

#loggerObject

Returns the value of attribute logger.



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

def logger
  @logger
end

#sample_rateObject

Returns the value of attribute sample_rate.



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

def sample_rate
  @sample_rate
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.



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

def timing_dimensions
  @timing_dimensions
end

Instance Method Details

#random_sample?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/trashed/reporter.rb', line 24

def random_sample?
  @sample_rate == 1 or rand < @sample_rate
end

#report(env) ⇒ Object



28
29
30
31
# File 'lib/trashed/reporter.rb', line 28

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

#report_logger(env) ⇒ Object



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
73
74
75
76
77
78
79
80
# File 'lib/trashed/reporter.rb', line 33

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').map(&:to_i)
  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



82
83
84
85
86
87
# File 'lib/trashed/reporter.rb', line 82

def report_statsd(env)
  @statsd.batch do |statsd|
    send_to_statsd statsd, :timing, env[Trashed::Rack::TIMINGS], :'Rack.Request', @timing_dimensions.call(env)
    send_to_statsd statsd, :gauge,  env[Trashed::Rack::GAUGES],  :'Rack.Server',  @gauge_dimensions.call(env)
  end
end

#sample?(env = nil) ⇒ Boolean

Override in subclasses. Be sure to ‘super && …` if you want to rely on the sample_rate.

Returns:

  • (Boolean)


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

def sample?(env = nil)
  random_sample?
end

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



89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/trashed/reporter.rb', line 89

def send_to_statsd(statsd, method, measurements, namespace, dimensions)
  measurements.each do |metric, value|
    case value
    when Array
      value.each do |v|
        send_to_statsd statsd, method, { 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