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

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

Instance Method Summary collapse

Instance Method Details

#log(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.



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/new_relic/agent/instrumentation/data_mapper.rb', line 194

def log(msg)
  return unless NewRelic::Agent.is_execution_traced?
  return unless operation = case NewRelic::Helper.correctly_encoded(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.
  metric = "ActiveRecord/#{operation}"
  rollup_metrics = ActiveRecordHelper.rollup_metrics_for(metric)
  metrics = [metric] + rollup_metrics
  NewRelic::Agent.instance.stats_engine.record_metrics(metrics, duration)
ensure
  super
end