Module: TingYun::Instrumentation::ActiveRecord

Defined in:
lib/ting_yun/instrumentation/active_record.rb

Constant Summary collapse

EXPLAINER =
lambda do |config, query|
  connection = TingYun::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



24
25
26
27
28
29
30
31
32
# File 'lib/ting_yun/instrumentation/active_record.rb', line 24

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

.instrumentObject



34
35
36
37
38
39
40
41
42
# File 'lib/ting_yun/instrumentation/active_record.rb', line 34

def self.instrument
  if defined?(::ActiveRecord::VERSION::MAJOR) && ::ActiveRecord::VERSION::MAJOR.to_i >= 3
    ::TingYun::Instrumentation::Support::ActiveRecordHelper.instrument_additional_methods
  end

  ::ActiveRecord::ConnectionAdapters::AbstractAdapter.module_eval do
    include ::TingYun::Instrumentation::ActiveRecord
  end
end

Instance Method Details

#log_with_tingyun_instrumentation(*args, &block) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/ting_yun/instrumentation/active_record.rb', line 44

def log_with_tingyun_instrumentation(*args, &block)

  state = TingYun::Agent::TransactionState.tl_get
  sql, name, _ = args
  metrics = ::TingYun::Instrumentation::Support::ActiveRecordHelper.metrics_for(
      TingYun::Helper.correctly_encoded(name),
      TingYun::Helper.correctly_encoded(sql),
      @config && @config[:adapter])

  scoped_metric = metrics.first

  TingYun::Agent::MethodTracer.trace_execution_scoped(metrics) do
    t0 = Time.now
    begin
      log_without_tingyun_instrumentation(*args, &block)
    ensure
      elapsed_time = (Time.now - t0).to_f
      state.sql_duration = elapsed_time * 1000

      TingYun::Agent.instance.transaction_sampler.notice_sql(sql, @config, elapsed_time,
                                                             state, EXPLAINER)
      TingYun::Agent.instance.sql_sampler.notice_sql(sql, scoped_metric,
                                                     @config, elapsed_time,
                                                     state, EXPLAINER)
    end

  end
end