Skip to content

Commit

Permalink
Chapter 3: Implement SQL query normalizer
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisBr committed Apr 4, 2020
1 parent b1f1847 commit 66a833a
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 3 deletions.
5 changes: 3 additions & 2 deletions lib/active_monitoring/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,13 @@ class Engine < ::Rails::Engine
end

ActiveMonitoring::Notifications.subscribe("sql.active_record") do |name, start, finish, _id, payload|
if ActiveMonitoring::SqlQuery.new(name: payload[:name], query: payload[:sql]).track?
query = ActiveMonitoring::SqlQuery.new(name: payload[:name].dup, query: payload[:sql].dup)
if query.track?
Metric.create(
name: name,
value: finish - start,
request_id: Current.request_id,
sql_query: payload[:sql],
sql_query: query.normalized_query,
location: Current.location,
created_at: finish
)
Expand Down
23 changes: 23 additions & 0 deletions lib/active_monitoring/sql_normalizer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module ActiveMonitoring
class SqlNormalizer
def initialize(query:)
@query = query
end

def to_s
query.squish!
query.gsub!(/(\s(=|>|<|>=|<=|<>|!=)\s)('[^']+'|[\$\+\-\w\.]+)/, '\1xxx')
query.gsub!(/(\sIN\s)\([^\(\)]+\)/i, '\1(xxx)')
regex = /(\sBETWEEN\s)('[^']+'|[\+\-\w\.]+)(\sAND\s)('[^']+'|[\+\-\w\.]+)/i
query.gsub!(regex, '\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

private

attr_reader :query
end
end
6 changes: 6 additions & 0 deletions lib/active_monitoring/sql_query.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
require_relative "sql_tracker"
require_relative "sql_normalizer"

module ActiveMonitoring
class SqlQuery
def initialize(query:, name:)
Expand All @@ -10,6 +12,10 @@ def track?
SqlTracker.new(query: query, name: name).track?
end

def normalized_query
SqlNormalizer.new(query: query).to_s
end

private

attr_reader :query, :name
Expand Down
2 changes: 1 addition & 1 deletion spec/requests/metrics_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
name: "sql.active_record",
value: 0,
created_at: Time.zone.local(2020, 1, 1),
sql_query: %(INSERT INTO "books" ("name", "created_at", "updated_at") VALUES (?, ?, ?)),
sql_query: %(INSERT INTO "books" ("name", "created_at", "updated_at") VALUES (xxx)),
request_id: response.headers["X-Request-Id"],
location: "BooksController#create"
)
Expand Down

0 comments on commit 66a833a

Please sign in to comment.