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



92
93
94
95
96
97
98
99
# File 'lib/sql_tracker/handler.rb', line 92

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



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/sql_tracker/handler.rb', line 30

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

  sql = payload[:sql].dup
  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



63
64
65
66
67
68
69
70
71
72
# File 'lib/sql_tracker/handler.rb', line 63

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



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/sql_tracker/handler.rb', line 74

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



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/sql_tracker/handler.rb', line 109

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

#subscribeObject



15
16
17
18
19
20
# File 'lib/sql_tracker/handler.rb', line 15

def subscribe
  @subscription ||= ActiveSupport::Notifications.subscribe(
    'sql.active_record',
    self
  )
end

#trace_path_matcherObject



59
60
61
# File 'lib/sql_tracker/handler.rb', line 59

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

#track?(sql) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
53
# File 'lib/sql_tracker/handler.rb', line 50

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

#tracked_sql_matcherObject



55
56
57
# File 'lib/sql_tracker/handler.rb', line 55

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

#unsubscribeObject



22
23
24
25
26
27
28
# File 'lib/sql_tracker/handler.rb', line 22

def unsubscribe
  return unless @subscription

  ActiveSupport::Notifications.unsubscribe(@subscription)

  @subscription = nil
end

#update_data(key, trace, duration) ⇒ Object



101
102
103
104
105
106
# File 'lib/sql_tracker/handler.rb', line 101

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