Module: ScoutApm::Instruments::ActiveRecordInstruments

Defined in:
lib/scout_apm/instruments/active_record_instruments.rb

Overview

Contains ActiveRecord instrument, aliasing ActiveRecord::ConnectionAdapters::AbstractAdapter#log calls to trace calls to the database.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(instrumented_class) ⇒ Object



8
9
10
11
12
13
14
15
16
17
# File 'lib/scout_apm/instruments/active_record_instruments.rb', line 8

def self.included(instrumented_class)
  ScoutApm::Agent.instance.logger.debug "Instrumenting #{instrumented_class.inspect}"
  instrumented_class.class_eval do
    unless instrumented_class.method_defined?(:log_without_scout_instruments)
      alias_method :log_without_scout_instruments, :log
      alias_method :log, :log_with_scout_instruments
      protected :log
    end
  end
end

Instance Method Details

#log_with_scout_instruments(*args, &block) ⇒ Object

self.included



19
20
21
22
23
24
# File 'lib/scout_apm/instruments/active_record_instruments.rb', line 19

def log_with_scout_instruments(*args, &block)
  sql, name = args
  self.class.instrument(scout_ar_metric_name(sql,name), :desc => Utils::SqlSanitizer.new(sql).to_s) do
    log_without_scout_instruments(sql, name, &block)
  end
end

#scout_ar_metric_name(sql, name) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/scout_apm/instruments/active_record_instruments.rb', line 26

def scout_ar_metric_name(sql,name)
  # sql: SELECT "places".* FROM "places"  ORDER BY "places"."position" ASC
  # name: Place Load
  if name && (parts = name.split " ") && parts.size == 2
    model = parts.first
    operation = parts.last.downcase
    metric_name = case operation
                  when 'load' then 'find'
                  when 'indexes', 'columns' then nil # not under developer control
                  when 'destroy', 'find', 'save', 'create', 'exists' then operation
                  when 'update' then 'save'
                  else
                    if model == 'Join'
                      operation
                    end
                  end
    metric = "ActiveRecord/#{model}/#{metric_name}" if metric_name
    metric = "ActiveRecord/SQL/other" if metric.nil?
  else
    metric = "ActiveRecord/SQL/Unknown"
  end
  metric
end