Class: TingYun::Instrumentation::Rails::ActiveRecordSubscriber

Inherits:
Support::EventedSubscriber show all
Defined in:
lib/ting_yun/instrumentation/support/active_record_subscriber.rb

Constant Summary collapse

CACHED_QUERY_NAME =
'CACHE'.freeze

Instance Method Summary collapse

Methods inherited from Support::EventedSubscriber

#event_stack, #log_notification_error, #pop_event, #push_event, subscribe, subscribed?

Constructor Details

#initializeActiveRecordSubscriber

Returns a new instance of ActiveRecordSubscriber.



17
18
19
20
21
22
# File 'lib/ting_yun/instrumentation/support/active_record_subscriber.rb', line 17

def initialize
  # We cache this in an instance variable to avoid re-calling method
  # on each query.
  @explainer = method(:explain_plan)
  super
end

Instance Method Details

#active_record_config_for_event(event) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/ting_yun/instrumentation/support/active_record_subscriber.rb', line 75

def active_record_config_for_event(event)
  return unless event.payload[:connection_id]

  connection = nil
  connection_id = event.payload[:connection_id]

  ::ActiveRecord::Base.connection_handler.connection_pool_list.each do |handler|
    connection = handler.connections.detect do |conn|
      conn.object_id == connection_id
    end

    break if connection
  end

  connection.instance_variable_get(:@config) if connection
end

#explain_plan(statement) ⇒ Object



24
25
26
# File 'lib/ting_yun/instrumentation/support/active_record_subscriber.rb', line 24

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

#finish(name, id, payload) ⇒ Object

THREAD_LOCAL_ACCESS



36
37
38
39
40
41
42
43
44
45
# File 'lib/ting_yun/instrumentation/support/active_record_subscriber.rb', line 36

def finish(name, id, payload) #THREAD_LOCAL_ACCESS
  return if payload[:name] == CACHED_QUERY_NAME
  state = TingYun::Agent::TransactionState.tl_get
  event = pop_event(id)
  config = active_record_config_for_event(event)
  base, metric = record_metrics(event, config)
  notice_sql(state, event, config, base, metric)
rescue Exception => e
  log_notification_error(e, name, 'finish')
end

#notice_sql(state, event, config, base, metric) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/ting_yun/instrumentation/support/active_record_subscriber.rb', line 48

def notice_sql(state, event, config, base, metric)
  stack  = state.traced_method_stack
  state.timings.sql_duration = state.timings.sql_duration + event.duration
  # enter transaction trace node
  frame = stack.push_frame(state, :active_record, event.time.to_f)

  sql_sampler.notice_sql(event.payload[:sql], base, config,
                         TingYun::Helper.milliseconds_to_seconds(event.duration),
                         state, @explainer, event.payload[:binds], event.payload[:name])

  transaction_sampler.notice_sql(event.payload[:sql], config, event.duration,
                                 state, @explainer, event.payload[:binds], event.payload[:name])
  # exit transaction trace node
  stack.pop_frame(state, frame, base, event.end.to_f, true, metric)
end

#record_metrics(event, config) ⇒ Object



64
65
66
67
68
69
70
71
72
# File 'lib/ting_yun/instrumentation/support/active_record_subscriber.rb', line 64

def record_metrics(event, config)
  metric, base, *other_metrics = TingYun::Instrumentation::Support::ActiveRecordHelper.metrics_for(event.payload[:name],
                                                                                           TingYun::Helper.correctly_encoded(event.payload[:sql]),
                                                                                           config)

  TingYun::Agent.agent.stats_engine.tl_record_scoped_and_unscoped_metrics(base, other_metrics, event.duration)

  return base, metric
end

#sql_samplerObject



96
97
98
# File 'lib/ting_yun/instrumentation/support/active_record_subscriber.rb', line 96

def sql_sampler
  ::TingYun::Agent::Collector::SqlSampler
end

#start(name, id, payload) ⇒ Object

THREAD_LOCAL_ACCESS



28
29
30
31
32
33
34
# File 'lib/ting_yun/instrumentation/support/active_record_subscriber.rb', line 28

def start(name, id, payload) #THREAD_LOCAL_ACCESS

  return if payload[:name] == CACHED_QUERY_NAME
  super
rescue => e
  log_notification_error(e, name, 'start')
end

#transaction_samplerObject



92
93
94
# File 'lib/ting_yun/instrumentation/support/active_record_subscriber.rb', line 92

def transaction_sampler
  ::TingYun::Agent::Collector::TransactionSampler
end