module TingYun::Agent::Database

sql explain plan

Constants

MAX_QUERY_LENGTH
RECORD_FOR

Public Instance Methods

capture_query(query) click to toggle source
# File lib/ting_yun/agent/database.rb, line 46
def capture_query(query)
  TingYun::Helper.correctly_encoded(truncate_query(query))
end
close_connections() click to toggle source
# File lib/ting_yun/agent/database.rb, line 77
def close_connections
  TingYun::Agent::Database::ConnectionManager.instance.close_connections
end
explain_plan(statement) click to toggle source
# File lib/ting_yun/agent/database.rb, line 24
def explain_plan(statement)
  connection = get_connection(statement.config) do
    ::ActiveRecord::Base.send("#{statement.config[:adapter]}_connection",
                              statement.config)
  end
  if connection
    if connection.respond_to?(:exec_query)
      return connection.exec_query("EXPLAIN #{statement.sql}",
                                   "Explain #{statement.name}",
                                   statement.binds)
    elsif connection.respond_to?(:execute)
      return connection.execute("EXPLAIN #{statement.sql}")
    end
  end
end
explain_sql(statement) click to toggle source
# File lib/ting_yun/agent/database.rb, line 18
def explain_sql(statement)
  return nil unless statement.sql && statement.explainer && statement.config
  statement.sql = statement.sql.split(";\n")[0] # only explain the first
  return statement.explain || {"dialect"=> nil, "keys"=>[], "values"=>[]}
end
get_connection(config, &connector) click to toggle source
# File lib/ting_yun/agent/database.rb, line 73
def get_connection(config, &connector)
  TingYun::Agent::Database::ConnectionManager.instance.get_connection(config, &connector)
end
obfuscate_sql(sql) click to toggle source
# File lib/ting_yun/agent/database.rb, line 41
def obfuscate_sql(sql)
  TingYun::Agent::Database::Obfuscator.instance.obfuscator.call(sql)
end
record_sql_method(key) click to toggle source
# File lib/ting_yun/agent/database.rb, line 60
def record_sql_method(key)

  case Agent.config[key].to_s
    when 'off'
      :off
    when 'raw'
      :raw
    else
      :obfuscated
  end
end
should_action_collect_explain_plans?() click to toggle source
# File lib/ting_yun/agent/database.rb, line 95
def should_action_collect_explain_plans?
  should_record_sql?("nbs.action_tracer.record_sql") &&
      Agent.config["nbs.action_tracer.explain_enabled".to_sym]
end
should_record_sql?(key) click to toggle source
# File lib/ting_yun/agent/database.rb, line 85
def should_record_sql?(key)
  RECORD_FOR.include?(record_sql_method(key.to_sym))
end
sql_sampler_enabled?() click to toggle source
# File lib/ting_yun/agent/database.rb, line 89
def sql_sampler_enabled?
  Agent.config[:'action_tracer.enabled'] &&
      Agent.config[:'nbs.action_tracer.slow_sql'] &&
      should_record_sql?('nbs.action_tracer.record_sql')
end
truncate_query(query) click to toggle source
# File lib/ting_yun/agent/database.rb, line 50
def truncate_query(query)
  if query.length > (MAX_QUERY_LENGTH - 4)
    query[0..MAX_QUERY_LENGTH - 4] + '...'
  else
    query
  end
end