Module: NewRelic::Agent::Instrumentation::DataMapperInstrumentation

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



187
188
189
190
191
192
# File 'lib/new_relic/agent/instrumentation/data_mapper.rb', line 187

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

Instance Method Details

#log_with_newrelic_instrumentation(msg) ⇒ Object

Unlike in AR, log is called in DM after the query actually ran, complete with metrics. Since DO has already calculated the duration, there’s nothing more to measure, so just record and log.

We rely on the assumption that all possible entry points have been hooked with tracers, ensuring that notice_sql attaches this SQL to the proper call scope.



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/new_relic/agent/instrumentation/data_mapper.rb', line 201

def log_with_newrelic_instrumentation(msg)
  return unless NewRelic::Agent.is_execution_traced?
  return unless operation = case msg.query
                            when /^\s*select/i          then 'find'
                            when /^\s*(update|insert)/i then 'save'
                            when /^\s*delete/i          then 'destroy'
                            else nil
                            end

  # FYI: self.to_s will yield connection URI string.
  duration = msg.duration / 1000000.0

  # Attach SQL to current segment/scope.
  NewRelic::Agent.instance.transaction_sampler.notice_sql(msg.query, nil, duration)

  # Record query duration associated with each of the desired metrics.
  metrics = [ "ActiveRecord/#{operation}", 'ActiveRecord/all' ]
  metrics.each do |metric|
    NewRelic::Agent.instance.stats_engine.get_stats_no_scope(metric).trace_call(duration)
  end
ensure
  log_without_newrelic_instrumentation(msg)
end