class Mmtrix::Agent::Instrumentation::ActiveRecordSubscriber

Constants

CACHED_QUERY_NAME

Public Class Methods

new() click to toggle source
Calls superclass method
# File lib/mmtrix/agent/instrumentation/active_record_subscriber.rb, line 16
def initialize
  # We cache this in an instance variable to avoid re-calling method
  # on each query.
  @explainer = method(:get_explain_plan)
  super
end

Public Instance Methods

active_record_config_for_event(event) click to toggle source
# File lib/mmtrix/agent/instrumentation/active_record_subscriber.rb, line 85
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) click to toggle source
# File lib/mmtrix/agent/instrumentation/active_record_subscriber.rb, line 31
def finish(name, id, payload) #THREAD_LOCAL_ACCESS
  return if payload[:name] == CACHED_QUERY_NAME
  state = Mmtrix::Agent::TransactionState.tl_get
  return unless state.is_execution_traced?
  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 ) click to toggle source
# File lib/mmtrix/agent/instrumentation/active_record_subscriber.rb, line 43
def get_explain_plan( config, query )
  connection = Mmtrix::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) click to toggle source
# File lib/mmtrix/agent/instrumentation/active_record_subscriber.rb, line 53
def notice_sql(state, event, config, metric)
  stack  = state.traced_method_stack

  # enter transaction trace node
  frame = stack.push_frame(state, :active_record, event.time)

  Mmtrix::Agent.instance.transaction_sampler \
    .notice_sql(event.payload[:sql], config,
                Helper.milliseconds_to_seconds(event.duration),
                state, @explainer)

  Mmtrix::Agent.instance.sql_sampler \
    .notice_sql(event.payload[:sql], metric, config,
                Helper.milliseconds_to_seconds(event.duration),
                state, @explainer)

  # exit transaction trace node
  stack.pop_frame(state, frame, metric, event.end)
end
record_metrics(event, config) click to toggle source
# File lib/mmtrix/agent/instrumentation/active_record_subscriber.rb, line 73
def record_metrics(event, config) #THREAD_LOCAL_ACCESS
  base, *other_metrics = ActiveRecordHelper.metrics_for(event.payload[:name],
                                                       Mmtrix::Helper.correctly_encoded(event.payload[:sql]),
                                                       config && config[:adapter])

  Mmtrix::Agent.instance.stats_engine.tl_record_scoped_and_unscoped_metrics(
    base, other_metrics,
    Helper.milliseconds_to_seconds(event.duration))

  base
end
start(name, id, payload) click to toggle source
Calls superclass method
# File lib/mmtrix/agent/instrumentation/active_record_subscriber.rb, line 23
def start(name, id, payload) #THREAD_LOCAL_ACCESS
  return if payload[:name] == CACHED_QUERY_NAME
  return unless Mmtrix::Agent.tl_is_execution_traced?
  super
rescue => e
  log_notification_error(e, name, 'start')
end