Class: TingYun::Instrumentation::Rails4::ActiveRecordSubscriber
- Inherits:
-
Support::EventedSubscriber
- Object
- Support::EventedSubscriber
- TingYun::Instrumentation::Rails4::ActiveRecordSubscriber
- Defined in:
- lib/ting_yun/instrumentation/rails4/active_record_subscriber.rb
Constant Summary collapse
- CACHED_QUERY_NAME =
'CACHE'.freeze
Instance Method Summary collapse
- #active_record_config_for_event(event) ⇒ Object
-
#finish(name, id, payload) ⇒ Object
THREAD_LOCAL_ACCESS.
- #get_explain_plan(config, query) ⇒ Object
-
#initialize ⇒ ActiveRecordSubscriber
constructor
A new instance of ActiveRecordSubscriber.
- #notice_sql(state, event, config, metric) ⇒ Object
- #record_metrics(event, config) ⇒ Object
-
#start(name, id, payload) ⇒ Object
THREAD_LOCAL_ACCESS.
Methods inherited from Support::EventedSubscriber
#event_stack, #log_notification_error, #pop_event, #push_event, subscribe, subscribed?
Constructor Details
#initialize ⇒ ActiveRecordSubscriber
Returns a new instance of ActiveRecordSubscriber.
14 15 16 17 18 19 |
# File 'lib/ting_yun/instrumentation/rails4/active_record_subscriber.rb', line 14 def initialize # We cache this in an instance variable to avoid re-calling method # on each query. @explainer = method(:get_explain_plan) super end |
Instance Method Details
#active_record_config_for_event(event) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/ting_yun/instrumentation/rails4/active_record_subscriber.rb', line 80 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 |
#finish(name, id, payload) ⇒ Object
THREAD_LOCAL_ACCESS
29 30 31 32 33 34 35 36 37 38 |
# File 'lib/ting_yun/instrumentation/rails4/active_record_subscriber.rb', line 29 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 => e log_notification_error(e, name, 'finish') end |
#get_explain_plan(config, query) ⇒ Object
40 41 42 43 44 45 46 47 48 |
# File 'lib/ting_yun/instrumentation/rails4/active_record_subscriber.rb', line 40 def get_explain_plan( 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 |
#notice_sql(state, event, config, metric) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/ting_yun/instrumentation/rails4/active_record_subscriber.rb', line 50 def notice_sql(state, event, config, metric) stack = state.traced_method_stack state.sql_duration = event.duration # enter transaction trace node frame = stack.push_frame(state, :active_record, event.time) TingYun::Agent.instance.sql_sampler \ .notice_sql(event.payload[:sql], metric, config, TingYun::Helper.milliseconds_to_seconds(event.duration), state, @explainer) TingYun::Agent.instance.transaction_sampler \ .notice_sql(event.payload[:sql], config, event.duration, state, @explainer) # exit transaction trace node stack.pop_frame(state, frame, metric, event.end) end |
#record_metrics(event, config) ⇒ Object
69 70 71 72 73 74 75 76 77 |
# File 'lib/ting_yun/instrumentation/rails4/active_record_subscriber.rb', line 69 def record_metrics(event, config) base, *other_metrics = TingYun::Instrumentation::Support::ActiveRecordHelper.metrics_for(event.payload[:name], TingYun::Helper.correctly_encoded(event.payload[:sql]), config && config[:adapter]) TingYun::Agent.agent.stats_engine.tl_record_scoped_and_unscoped_metrics(base, other_metrics, event.duration) base end |
#start(name, id, payload) ⇒ Object
THREAD_LOCAL_ACCESS
21 22 23 24 25 26 27 |
# File 'lib/ting_yun/instrumentation/rails4/active_record_subscriber.rb', line 21 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 |