Class: SqlMonitor::Handler

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Handler

Returns a new instance of Handler.



12
13
14
15
16
17
18
19
# File 'lib/sql_monitor/handler.rb', line 12

def initialize(config)
  @config = config
  @started_at = Time.now.to_s
  @data = {} # {key: {sql:, count:, duration, source: []}, ...}
  @redis = Redis.new(host: @config.redis_host, db: @config.redis_db)
  @cachedVerKey = @config.release_version
  set_version
end

Instance Attribute Details

#cachedVerKeyObject (readonly)

Returns the value of attribute cachedVerKey.



10
11
12
# File 'lib/sql_monitor/handler.rb', line 10

def cachedVerKey
  @cachedVerKey
end

#configObject (readonly)

Returns the value of attribute config.



10
11
12
# File 'lib/sql_monitor/handler.rb', line 10

def config
  @config
end

#dataObject (readonly)

Returns the value of attribute data.



10
11
12
# File 'lib/sql_monitor/handler.rb', line 10

def data
  @data
end

#redisObject (readonly)

Returns the value of attribute redis.



10
11
12
# File 'lib/sql_monitor/handler.rb', line 10

def redis
  @redis
end

Instance Method Details

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



115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/sql_monitor/handler.rb', line 115

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]

  # store new data
  @redis.set(@cachedVerKey + ':' + key, JSON.dump(@data[key]))

  @data
end

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



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/sql_monitor/handler.rb', line 53

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



86
87
88
89
90
91
92
93
94
95
# File 'lib/sql_monitor/handler.rb', line 86

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



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/sql_monitor/handler.rb', line 97

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



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/sql_monitor/handler.rb', line 143

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
  output[:cached_ver_key] = @cachedVerKey

  FileUtils.mkdir_p(@config.output_path)
  filename = "sql_monitor-#{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

#set_versionObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/sql_monitor/handler.rb', line 21

def set_version
  versions = @redis.get('all_versions')
  if versions.nil? || versions.empty?
    versions = [{
      released_at: Time.now.to_s,
      version: @cachedVerKey
    }]
  else
    versions = JSON.parse(@redis.get('all_versions'), {:symbolize_names => true})
  end
  versions.push({
    released_at: Time.now.to_s,
    version: @cachedVerKey
  }) unless versions.map{|x|x[:version]}.include?(@cachedVerKey)
  @redis.set('all_versions', JSON.dump(versions))
end

#subscribeObject



38
39
40
41
42
43
# File 'lib/sql_monitor/handler.rb', line 38

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

#trace_path_matcherObject



82
83
84
# File 'lib/sql_monitor/handler.rb', line 82

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

#track?(sql) ⇒ Boolean

Returns:

  • (Boolean)


73
74
75
76
# File 'lib/sql_monitor/handler.rb', line 73

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

#tracked_sql_matcherObject



78
79
80
# File 'lib/sql_monitor/handler.rb', line 78

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

#unsubscribeObject



45
46
47
48
49
50
51
# File 'lib/sql_monitor/handler.rb', line 45

def unsubscribe
  return unless @subscription

  ActiveSupport::Notifications.unsubscribe(@subscription)

  @subscription = nil
end

#update_data(key, trace, duration) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/sql_monitor/handler.rb', line 128

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

  cachedData = JSON.parse(@redis.get(@cachedVerKey + ':' + key), {:symbolize_names => true})
  cachedData[:count] += 1
  cachedData[:duration] += duration
  cachedData[:source] << trace.first
  @redis.set(@cachedVerKey + ':' + key, JSON.dump(cachedData))

  @data
end