Class: Fluent::MysqlExplainFilter
- Inherits:
-
Filter
- Object
- Filter
- Fluent::MysqlExplainFilter
- Defined in:
- lib/fluent/plugin/filter_mysql_explain.rb
Instance Method Summary collapse
- #client ⇒ Object
- #configure(conf) ⇒ Object
- #explain(sql) ⇒ Object
- #explainable?(sql) ⇒ Boolean
- #filter(tag, time, record) ⇒ Object
- #hash_get(hash, key) ⇒ Object
-
#initialize ⇒ MysqlExplainFilter
constructor
A new instance of MysqlExplainFilter.
- #shutdown ⇒ Object
- #start ⇒ Object
Constructor Details
#initialize ⇒ MysqlExplainFilter
Returns a new instance of MysqlExplainFilter.
5 6 7 8 |
# File 'lib/fluent/plugin/filter_mysql_explain.rb', line 5 def initialize super require "mysql2" end |
Instance Method Details
#client ⇒ Object
70 71 72 73 74 75 76 |
# File 'lib/fluent/plugin/filter_mysql_explain.rb', line 70 def client Mysql2::Client.new({ :host => @host, :port => @port, :username => @username, :password => @password, :database => @database, :flags => Mysql2::Client::MULTI_STATEMENTS, }) end |
#configure(conf) ⇒ Object
19 20 21 |
# File 'lib/fluent/plugin/filter_mysql_explain.rb', line 19 def configure(conf) super end |
#explain(sql) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/fluent/plugin/filter_mysql_explain.rb', line 39 def explain(sql) if sql.empty? || !explainable?(sql) return '' end res = StringIO.new handler = self.client handler.query("EXPLAIN #{sql}").each_with_index do |row, i| res.puts "*************************** #{i+1}. row ***************************" row.each do |key, value| value ||= "NULL" res.puts key.rjust(13, ' ') + ": #{value}" end end handler.close res.rewind res.read end |
#explainable?(sql) ⇒ Boolean
60 61 62 |
# File 'lib/fluent/plugin/filter_mysql_explain.rb', line 60 def explainable?(sql) sql =~/^\s*(SELECT|DELETE|INSERT|REPLACE|UPDATE)/i end |
#filter(tag, time, record) ⇒ Object
31 32 33 34 35 36 37 |
# File 'lib/fluent/plugin/filter_mysql_explain.rb', line 31 def filter(tag, time, record) sql = hash_get(record, @sql_key) if !sql.nil? && !sql.empty? record[@added_key] = explain(sql) end record end |
#hash_get(hash, key) ⇒ Object
64 65 66 67 68 |
# File 'lib/fluent/plugin/filter_mysql_explain.rb', line 64 def hash_get(hash, key) return hash[key.to_sym] if hash.key?(key.to_sym) return hash[key] if hash.key?(key) nil end |
#shutdown ⇒ Object
27 28 29 |
# File 'lib/fluent/plugin/filter_mysql_explain.rb', line 27 def shutdown super end |
#start ⇒ Object
23 24 25 |
# File 'lib/fluent/plugin/filter_mysql_explain.rb', line 23 def start super end |