Module: NewRelic::Agent::Instrumentation::ActiveRecordInstrumentation

Defined in:
lib/new_relic/agent/instrumentation/active_record_instrumentation.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(instrumented_class) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/new_relic/agent/instrumentation/active_record_instrumentation.rb', line 10

def self.included(instrumented_class)
  instrumented_class.class_eval do
    alias_method :log_without_newrelic_instrumentation, :log
    alias_method :log, :log_with_newrelic_instrumentation
    protected :log
  end
end

Instance Method Details

#log_with_newrelic_instrumentation(sql, name, &block) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
# File 'lib/new_relic/agent/instrumentation/active_record_instrumentation.rb', line 18

def log_with_newrelic_instrumentation(sql, name, &block)

  return log_without_newrelic_instrumentation(sql, name, &block) unless NewRelic::Agent.is_execution_traced?

  # Capture db config if we are going to try to get the explain plans
  if (defined?(ActiveRecord::ConnectionAdapters::MysqlAdapter) && self.is_a?(ActiveRecord::ConnectionAdapters::MysqlAdapter)) ||
      (defined?(ActiveRecord::ConnectionAdapters::PostgreSQLAdapter) && self.is_a?(ActiveRecord::ConnectionAdapters::PostgreSQLAdapter))
    supported_config = @config
  end
  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 # fall back to DirectSQL
                  when 'destroy', 'find', 'save', 'create' then operation
                  when 'update' then 'save'
                  else
                    if model == 'Join'
                      operation
                    end
                  end
    metric = "ActiveRecord/#{model}/#{metric_name}" if metric_name
  end

  if metric.nil?
    metric = NewRelic::Agent::Instrumentation::MetricFrame.database_metric_name
    if metric.nil?
      if sql =~ /^(select|update|insert|delete|show)/i
        # Could not determine the model/operation so let's find a better
        # metric.  If it doesn't match the regex, it's probably a show
        # command or some DDL which we'll ignore.
        metric = "Database/SQL/#{$1.downcase}"
      else
        metric = "Database/SQL/other"
      end
    end
  end

  if !metric
    log_without_newrelic_instrumentation(sql, name, &block)
  else
    metrics = [metric, "ActiveRecord/all"]
    metrics << "ActiveRecord/#{metric_name}" if metric_name
    self.class.trace_execution_scoped(metrics) do
      t0 = Time.now
      begin
        log_without_newrelic_instrumentation(sql, name, &block)
      ensure
        NewRelic::Agent.instance.transaction_sampler.notice_sql(sql, supported_config, (Time.now - t0).to_f)
      end
    end
  end
end