Module: Remnant::ClassMethods

Included in:
Remnant
Defined in:
lib/remnant/base.rb

Instance Method Summary collapse

Instance Method Details

#collectObject

Collect data, report statsd timings, run hook



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/remnant/base.rb', line 88

def collect
  @sample_counter ||= 0

  extra_remnant_key = Remnant::Discover.results.delete(:extra_remnant_key)

  # don't send timings in development or test environments
  unless ::Rails.env.development? || ::Rails.env.test?
    # only log if above sample rate
    if @sample_counter > configuration.sample_rate
      key_prefix = [
                    Remnant.configuration.tag,
                    Remnant.configuration.env,
                    extra_remnant_key
                   ].compact.join('.')

      Remnant::Discover.results.map do |remnant_key, ms|
        Remnant.handler.timing("#{key_prefix}.#{remnant_key}", ms.to_i)
      end

      Remnant.handler.timing("#{key_prefix}.gc", Remnant::GC.time.to_i)
      Remnant.handler.timing("#{key_prefix}.db", Remnant::Database.total_time.to_i)
      Remnant.handler.timing("#{key_prefix}.filters", Remnant::Filters.total_time.to_i)

      @sample_counter = 0
    else
      @sample_counter += 1
    end
  end

  # run hook if given for all environments
  unless Remnant.configuration.custom_hook.nil?
    begin
      Remnant.configuration.custom_hook.call(Remnant::Discover.results)
    rescue => e
      ::Rails.logger.error "Failed to run hook: #{e.message}"
    end
  end

  Remnant::Database.reset
  Remnant::Template.reset
  Remnant::Filters.reset
  Remnant::Discover.results.clear
end

#color(default = false, heading = false) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/remnant/base.rb', line 15

def color(default = false, heading = false)
  return "\033[0m" if default
  return "\033[0;01;33m" if heading

  @current_color ||= "\033[0;01;36m"
  @next_color ||= "\033[0;01;35m"

  @current_color, @next_color = @next_color, @current_color
  @current_color
end

#configurationObject



30
31
32
# File 'lib/remnant/base.rb', line 30

def configuration
  @configuration ||= Remnant::Configuration.new.defaults!
end

#configure(&block) ⇒ Object



26
27
28
# File 'lib/remnant/base.rb', line 26

def configure(&block)
  configuration.instance_eval(&block)
end

#disable!Object



3
4
5
# File 'lib/remnant/base.rb', line 3

def disable!
  @enabled = false
end

#enable!Object



7
8
9
# File 'lib/remnant/base.rb', line 7

def enable!
  @enabled = true
end

#enabled?Boolean

Returns:

  • (Boolean)


11
12
13
# File 'lib/remnant/base.rb', line 11

def enabled?
  @enabled
end

#handlerObject



34
35
36
# File 'lib/remnant/base.rb', line 34

def handler
  @handler ||= Statsd.new(Remnant.configuration.hostname, Remnant.configuration.port_number)
end

#outputObject

Log everything to Rails logger for output



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
81
82
83
# File 'lib/remnant/base.rb', line 41

def output
  # always log in development mode
  Rails.logger.info "#{color(false, true)}--------------Remnants Discovered--------------#{color(true)}"

  Remnant::Discover.results.map do |remnant_key, ms|
    key = [
      extra_remnant_key,
      remnant_key
    ].compact.join('.')

    Rails.logger.info "#{Remnant.color}#{ms.to_i}ms#{Remnant.color(true)}\t#{key}"
  end
  Rails.logger.info "#{Remnant.color}#{Remnant::GC.time.to_i}ms (#{Remnant::GC.collections} collections)#{Remnant.color(true)}\tGC"

  # filters
  Rails.logger.info ""
  Rails.logger.info("#{color(false, true)}----- Filters (%.2fms) -----#{color(true)}" % Remnant::Filters.total_time)
  Remnant::Filters.filters.map do |filter|
    Rails.logger.info("#{color}%.2fms#{color(true)}\t#{filter[:name]} (#{filter[:type]})" % filter[:ms])
  end

  # template captures
  if Remnant::Template.enabled?
    Rails.logger.info ""
    Rails.logger.info "#{color(false, true)}----- Templates -----#{color(true)}"
    Remnant::Template.trace.root.children.map do |rendering|
      Remnant::Template.trace.log(Rails.logger, rendering)
    end
  end

  # sql captures
  Rails.logger.info ""
  Rails.logger.info("#{color(false, true)}---- Database (%.2fms) -----#{color(true)}" % Remnant::Database.total_time)
  if Remnant::Database.suppress?
    Rails.logger.info "queries suppressed in development mode"
  else
    Remnant::Database.queries.map do |query|
      Rails.logger.info("#{color}%.2fms#{color(true)}\t#{query.sql}" % (query.time * 1000))
    end
  end

  Rails.logger.info "#{color(false, true)}-----------------------------------------------#{color(true)}"
end