lib/remnant/base.rb in remnant-0.4.10 vs lib/remnant/base.rb in remnant-0.9.0

- old
+ new

@@ -1,7 +1,19 @@ class Remnant module ClassMethods + def disable! + @enabled = false + end + + def enable! + @enabled = true + end + + def enabled? + @enabled + end + def color(default = false, heading = false) return "\033[0m" if default return "\033[0;01;33m" if heading @current_color ||= "\033[0;01;36m" @@ -26,14 +38,32 @@ def collect @sample_counter ||= 0 extra_remnant_key = Remnant::Discover.results.delete(:extra_remnant_key) - # process special queue times - ::Remnant::Queue.process! + if ::Rails.env.production? || ::Rails.env.staging? || ::Rails.env.demo? + # 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('.') - if ::Rails.env.development? || ::Rails.env.test? + 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 + else # always log in development mode Rails.logger.info "#{color(false, true)}--------------Remnants Discovered--------------#{color(true)}" Remnant::Discover.results.map do |remnant_key, ms| key = [ @@ -71,30 +101,9 @@ Rails.logger.info("#{color}%.2fms#{color(true)}\t#{query.sql}" % (query.time * 1000)) end end Rails.logger.info "#{color(false, true)}-----------------------------------------------#{color(true)}" - else - # 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 unless Remnant.configuration.custom_hook.nil? Remnant.configuration.custom_hook.call(Remnant::Discover.results)