Module: ScoutApm::Instruments::ActiveRecordQueryingInstruments

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

Overview

Entry-point of instruments.

We instrument both ActiveRecord::Querying#find_by_sql and ActiveRecord::FinderMethods#find_with_associations. These are early in the chain of calls when you’re using ActiveRecord.

Later on, they will call into #log, which we also instrument, at which point, we can fill in additional data gathered at that point (name, sql)

Caveats:

* We don't have a name for the query yet.
* The query hasn't hit the cache yet. In the case of a cache hit, we
  won't hit #log, so won't get a name, leaving the misleading default.
* One call here can result in several calls to #log, especially in the
  case where Rails needs to load the schema details for the table being
  queried.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(instrumented_class) ⇒ Object



159
160
161
162
163
164
165
166
167
# File 'lib/scout_apm/instruments/active_record.rb', line 159

def self.included(instrumented_class)
  ScoutApm::Agent.instance.logger.info "Instrumenting ActiveRecord::Querying - #{instrumented_class.inspect}"
  instrumented_class.class_eval do
    unless instrumented_class.method_defined?(:find_by_sql_without_scout_instruments)
      alias_method :find_by_sql_without_scout_instruments, :find_by_sql
      alias_method :find_by_sql, :find_by_sql_with_scout_instruments
    end
  end
end

Instance Method Details

#find_by_sql_with_scout_instruments(*args, &block) ⇒ Object



169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/scout_apm/instruments/active_record.rb', line 169

def find_by_sql_with_scout_instruments(*args, &block)
  req = ScoutApm::RequestManager.lookup
  layer = ScoutApm::Layer.new("ActiveRecord", Utils::ActiveRecordMetricName::DEFAULT_METRIC)
  layer.annotate_layer(:ignorable => true)
  req.start_layer(layer)
  req.ignore_children!
  begin
    find_by_sql_without_scout_instruments(*args, &block)
  ensure
    req.acknowledge_children!
    req.stop_layer
  end
end