Module: Rack::MiniProfiler::ActiveRecordInstrumentation

Defined in:
lib/patches/db/activerecord.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(instrumented_class) ⇒ Object



6
7
8
9
10
11
12
13
14
# File 'lib/patches/db/activerecord.rb', line 6

def self.included(instrumented_class)
  instrumented_class.class_eval do
    unless instrumented_class.method_defined?(:log_without_miniprofiler)
      alias_method :log_without_miniprofiler, :log
      alias_method :log, :log_with_miniprofiler
      protected :log
    end
  end
end

Instance Method Details

#binds_to_params(binds) ⇒ Object



16
17
18
19
20
21
22
23
24
25
# File 'lib/patches/db/activerecord.rb', line 16

def binds_to_params(binds)
  return if binds.nil? || Rack::MiniProfiler.config.max_sql_param_length == 0
  # map ActiveRecord::Relation::QueryAttribute to [name, value]
  params = binds.map { |c| c.kind_of?(Array) ? [c.first, c.last] : [c.name, c.value] }
  if (skip = Rack::MiniProfiler.config.skip_sql_param_names)
    params.map { |(n,v)| n =~ skip ? [n, nil] : [n, v] }
  else
    params
  end
end

#log_with_miniprofiler(*args, &block) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/patches/db/activerecord.rb', line 27

def log_with_miniprofiler(*args, &block)
  return log_without_miniprofiler(*args, &block) unless SqlPatches.should_measure?

  sql, name, binds = args
  start            = Time.now
  rval             = log_without_miniprofiler(*args, &block)

  # Don't log schema queries if the option is set
  return rval if Rack::MiniProfiler.config.skip_schema_queries and name =~ /SCHEMA/

  elapsed_time = SqlPatches.elapsed_time(start)
  Rack::MiniProfiler.record_sql(sql, elapsed_time, binds_to_params(binds))
  rval
end