Class: SqlTracker::Handler

Inherits:
Object
  • Object
show all
Defined in:
lib/sql_tracker/handler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Handler

Returns a new instance of Handler.



9
10
11
12
13
# File 'lib/sql_tracker/handler.rb', line 9

def initialize(config)
  @config = config
  @started_at = Time.now.to_s
  @data = {} # {key: {sql:, count:, duration, source: []}, ...}
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



7
8
9
# File 'lib/sql_tracker/handler.rb', line 7

def data
  @data
end

Instance Method Details

#add_data(key, sql, trace, duration) ⇒ Object



77
78
79
80
81
82
83
84
# File 'lib/sql_tracker/handler.rb', line 77

def add_data(key, sql, trace, duration)
  @data[key] = {}
  @data[key][:sql] = sql
  @data[key][:count] = 1
  @data[key][:duration] = duration
  @data[key][:source] = [trace.first]
  @data
end

#call(_name, started, finished, _id, payload) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/sql_tracker/handler.rb', line 15

def call(_name, started, finished, _id, payload)
  return unless @config.enabled

  sql = payload[:sql]
  return unless track?(sql)

  cleaned_trace = clean_trace(caller)
  return if cleaned_trace.empty?

  sql = clean_sql_query(sql)
  duration = 1000.0 * (finished - started) # in milliseconds
  sql_key = Digest::MD5.hexdigest(sql.downcase)

  if @data.key?(sql_key)
    update_data(sql_key, cleaned_trace, duration)
  else
    add_data(sql_key, sql, cleaned_trace, duration)
  end
end

#clean_sql_query(query) ⇒ Object



48
49
50
51
52
53
54
55
56
57
# File 'lib/sql_tracker/handler.rb', line 48

def clean_sql_query(query)
  query.squish!
  query.gsub!(/(\s(=|>|<|>=|<=|<>|!=)\s)('[^']+'|[\$\+\-\w\.]+)/, '\1xxx')
  query.gsub!(/(\sIN\s)\([^\(\)]+\)/i, '\1(xxx)')
  query.gsub!(/(\sBETWEEN\s)('[^']+'|[\+\-\w\.]+)(\sAND\s)('[^']+'|[\+\-\w\.]+)/i, '\1xxx\3xxx')
  query.gsub!(/(\sVALUES\s)\(.+\)/i, '\1(xxx)')
  query.gsub!(/(\s(LIKE|ILIKE|SIMILAR TO|NOT SIMILAR TO)\s)('[^']+')/i, '\1xxx')
  query.gsub!(/(\s(LIMIT|OFFSET)\s)(\d+)/i, '\1xxx')
  query
end

#clean_trace(trace) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/sql_tracker/handler.rb', line 59

def clean_trace(trace)
  return trace unless defined?(::Rails)

  if Rails.backtrace_cleaner.instance_variable_get(:@root) == '/'
    Rails.backtrace_cleaner.instance_variable_set :@root, Rails.root.to_s
  end

  Rails.backtrace_cleaner.remove_silencers!

  if @config.tracked_paths.respond_to?(:join)
    Rails.backtrace_cleaner.add_silencer do |line|
      line !~ trace_path_matcher
    end
  end

  Rails.backtrace_cleaner.clean(trace)
end

#saveObject

save the data to file



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/sql_tracker/handler.rb', line 94

def save
  return if @data.empty?
  output = {}
  output[:data] = @data
  output[:generated_at] = Time.now.to_s
  output[:started_at] = @started_at
  output[:format_version] = '1.0'
  output[:rails_version] = Rails.version
  output[:rails_path] = Rails.root.to_s

  FileUtils.mkdir_p(@config.output_path)
  filename = "sql_tracker-#{Process.pid}-#{Time.now.to_i}.json"

  File.open(File.join(@config.output_path, filename), 'w') do |f|
    f.write JSON.dump(output)
  end
end

#trace_path_matcherObject



44
45
46
# File 'lib/sql_tracker/handler.rb', line 44

def trace_path_matcher
  @trace_path_matcher ||= %r{^(#{@config.tracked_paths.join('|')})\/}
end

#track?(sql) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
38
# File 'lib/sql_tracker/handler.rb', line 35

def track?(sql)
  return true unless @config.tracked_sql_command.respond_to?(:join)
  tracked_sql_matcher =~ sql
end

#tracked_sql_matcherObject



40
41
42
# File 'lib/sql_tracker/handler.rb', line 40

def tracked_sql_matcher
  @tracked_sql_matcher ||= /\A#{@config.tracked_sql_command.join('|')}/i
end

#update_data(key, trace, duration) ⇒ Object



86
87
88
89
90
91
# File 'lib/sql_tracker/handler.rb', line 86

def update_data(key, trace, duration)
  @data[key][:count] += 1
  @data[key][:duration] += duration
  @data[key][:source] << trace.first
  @data
end