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

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

Constant Summary collapse

EXPLAINER =
lambda do |statement|
  connection = NewRelic::Agent::Database.get_connection(statement.config) do
    ::ActiveRecord::Base.send("#{statement.config[:adapter]}_connection",
      statement.config)
  end
  # the following line needs else branch coverage
  if connection && connection.respond_to?(:execute) # rubocop:disable Style/SafeNavigation
    return connection.execute("EXPLAIN #{statement.sql}")
  end
end

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(instrumented_class) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/new_relic/agent/instrumentation/active_record.rb', line 37

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

.insert_instrumentationObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/new_relic/agent/instrumentation/active_record.rb', line 22

def self.insert_instrumentation
  if defined?(::ActiveRecord::VERSION::MAJOR) && ::ActiveRecord::VERSION::MAJOR.to_i >= 3
    if ::NewRelic::Agent.config[:prepend_active_record_instrumentation]
      ::ActiveRecord::Base.prepend(::NewRelic::Agent::Instrumentation::ActiveRecordPrepend::BaseExtensions)
      ::ActiveRecord::Relation.prepend(::NewRelic::Agent::Instrumentation::ActiveRecordPrepend::RelationExtensions)
    else
      ::NewRelic::Agent::Instrumentation::ActiveRecordHelper.instrument_additional_methods
    end
  end

  ::ActiveRecord::ConnectionAdapters::AbstractAdapter.module_eval do
    include ::NewRelic::Agent::Instrumentation::ActiveRecord
  end
end

Instance Method Details

#log_with_newrelic_instrumentation(*args, **kwargs, &block) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/new_relic/agent/instrumentation/active_record.rb', line 48

def log_with_newrelic_instrumentation(*args, &block)
  state = NewRelic::Agent::Tracer.state

  if !state.is_execution_traced?
    return log_without_newrelic_instrumentation(*args, &block)
  end

  sql, name, _ = args

  product, operation, collection = ActiveRecordHelper.product_operation_collection_for(
    NewRelic::Helper.correctly_encoded(name),
    NewRelic::Helper.correctly_encoded(sql),
    @config && @config[:adapter]
  )

  host = nil
  port_path_or_id = nil
  database = nil

  if ActiveRecordHelper::InstanceIdentification.supported_adapter?(@config)
    host = ActiveRecordHelper::InstanceIdentification.host(@config)
    port_path_or_id = ActiveRecordHelper::InstanceIdentification.port_path_or_id(@config)
    database = @config && @config[:database]
  end

  segment = NewRelic::Agent::Tracer.start_datastore_segment(
    product: product,
    operation: operation,
    collection: collection,
    host: host,
    port_path_or_id: port_path_or_id,
    database_name: database
  )
  segment._notice_sql(sql, @config, EXPLAINER)

  begin
    NewRelic::Agent::Tracer.capture_segment_error(segment) do
      log_without_newrelic_instrumentation(*args, &block)
    end
  ensure
    ::NewRelic::Agent::Transaction::Segment.finish(segment)
  end
end