Module: NewRelic::Agent::Instrumentation::ActiveRecord

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

Constant Summary collapse

EXPLAINER =
lambda do |config, query|
  connection = NewRelic::Agent::Database.get_connection(config) do
    ::ActiveRecord::Base.send("#{config[:adapter]}_connection",
                              config)
  end
  if connection && connection.respond_to?(:execute)
    return connection.execute("EXPLAIN #{query}")
  end
end

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(instrumented_class) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/new_relic/agent/instrumentation/active_record.rb', line 19

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

Instance Method Details

#log_with_newrelic_instrumentation(*args, &block) ⇒ Object



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
# File 'lib/new_relic/agent/instrumentation/active_record.rb', line 29

def log_with_newrelic_instrumentation(*args, &block)
  if !NewRelic::Agent.is_execution_traced?
    return log_without_newrelic_instrumentation(*args, &block)
  end

  sql, name, _ = args
  metric = ActiveRecordHelper.metric_for_name(NewRelic::Helper.correctly_encoded(name)) ||
    ActiveRecordHelper.metric_for_sql(NewRelic::Helper.correctly_encoded(sql))

  if !metric
    log_without_newrelic_instrumentation(*args, &block)
  else
    metrics = [metric, remote_service_metric].compact
    metrics += ActiveRecordHelper.rollup_metrics_for(metric)
    self.class.trace_execution_scoped(metrics) do
      t0 = Time.now
      begin
        log_without_newrelic_instrumentation(*args, &block)
      ensure
        elapsed_time = (Time.now - t0).to_f

        NewRelic::Agent.instance.transaction_sampler.notice_sql(sql,
                                              @config, elapsed_time,
                                                          &EXPLAINER)
        NewRelic::Agent.instance.sql_sampler.notice_sql(sql, metric,
                                              @config, elapsed_time,
                                                          &EXPLAINER)
      end
    end
  end
end

#remote_service_metricObject



61
62
63
64
65
# File 'lib/new_relic/agent/instrumentation/active_record.rb', line 61

def remote_service_metric
  if @config && @config[:adapter]
    ActiveRecordHelper.remote_service_metric(@config[:adapter], @config[:host])
  end
end