Module: TingYun::Agent::Database

Extended by:
Database
Included in:
Database
Defined in:
lib/ting_yun/agent/database.rb,
lib/ting_yun/agent/database/statement.rb,
lib/ting_yun/agent/database/obfuscator.rb,
lib/ting_yun/agent/database/connection_manager.rb,
lib/ting_yun/agent/database/explain_plan_helpers.rb

Overview

sql explain plan

Defined Under Namespace

Modules: ExplainPlanHelpers Classes: ConnectionManager, Obfuscator, Statement

Constant Summary collapse

MAX_QUERY_LENGTH =
16384
RECORD_FOR =
[:raw, :obfuscated].freeze

Instance Method Summary collapse

Instance Method Details

#capture_query(query) ⇒ Object



46
47
48
# File 'lib/ting_yun/agent/database.rb', line 46

def capture_query(query)
  TingYun::Helper.correctly_encoded(truncate_query(query))
end

#close_connectionsObject



77
78
79
# File 'lib/ting_yun/agent/database.rb', line 77

def close_connections
  TingYun::Agent::Database::ConnectionManager.instance.close_connections
end

#explain_plan(statement) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# 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) ⇒ Object



18
19
20
21
22
# 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) ⇒ Object



73
74
75
# 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) ⇒ Object



41
42
43
# 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) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
# 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?Boolean

Returns:

  • (Boolean)


95
96
97
98
# 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) ⇒ Boolean

Returns:

  • (Boolean)


85
86
87
# 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?Boolean

Returns:

  • (Boolean)


89
90
91
92
93
# File 'lib/ting_yun/agent/database.rb', line 89

def sql_sampler_enabled?
  Agent.config[:'nbs.action_tracer.enabled'] &&
      Agent.config[:'nbs.action_tracer.slow_sql'] &&
      should_record_sql?('nbs.action_tracer.record_sql')
end

#truncate_query(query) ⇒ Object



50
51
52
53
54
55
56
# 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