Module: TingYun::Instrumentation::ActiveRecord

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

Constant Summary collapse

EXPLAINER =
method(:explain_plan)

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.explain_plan(statement) ⇒ Object



17
18
19
# File 'lib/ting_yun/instrumentation/active_record.rb', line 17

def self.explain_plan(statement)
  TingYun::Agent::Database.explain_plan(statement)
end

.included(instrumented_class) ⇒ Object



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

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



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

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



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

def log_with_tingyun_instrumentation(*args, &block)

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

  scoped_metric = metrics.first

  TingYun::Agent::MethodTracerHelpers.trace_execution_scoped(metrics, {}, nil, klass_name) do
    t0 = Time.now
    begin
      log_without_tingyun_instrumentation(*args, &block)
    ensure
      elapsed_time = (Time.now - t0).to_f
      state.timings.sql_duration = state.timings.sql_duration  + elapsed_time * 1000

      ::TingYun::Agent::Collector::TransactionSampler.notice_sql(sql, @config, elapsed_time, state, EXPLAINER)
      ::TingYun::Agent::Collector::SqlSampler.notice_sql(sql, scoped_metric, @config, elapsed_time, state, EXPLAINER)
    end

  end
end