Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

filters/sql: apply more aggressive grouping #504

Merged
merged 1 commit into from
Oct 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ Airbrake Ruby Changelog

### master

* Improved grouping of some SQL queries
([#504](https://github.com/airbrake/airbrake-ruby/pull/504))

### [v4.6.0][v4.6.0] (August 5, 2019)

* Added the `query_stats` option that configures SQL performance
Expand Down
5 changes: 5 additions & 0 deletions lib/airbrake-ruby/filters/sql_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ class SqlFilter
# rubocop:enable Metrics/LineLength
}.freeze

# @return [Regexp] the regexp that is applied after the feature regexps
# were used
POST_FILTER = /(?<=[values|in ]\().+(?=\))/i

# @return [Hash{Symbol=>Array<Symbol>}] a set of features that corresponds
# to a certain dialect
DIALECT_FEATURES = {
Expand Down Expand Up @@ -96,6 +100,7 @@ def call(resource)
return unless resource.respond_to?(:query)

q = resource.query.gsub(@regexp, FILTERED)
q.gsub!(POST_FILTER, FILTERED) if q =~ POST_FILTER
q = ERROR_MSG if UNMATCHED_PAIR[@dialect] =~ q
resource.query = q
end
Expand Down
12 changes: 8 additions & 4 deletions spec/filters/sql_filter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,19 @@
dialects: %i[postgres]
}, {
input: "INSERT INTO `X` values(\"test\",0, 1 , 2, 'test')",
output: "INSERT INTO `X` values(?,?, ? , ?, ?)",
output: "INSERT INTO `X` values(?)",
dialects: %i[mysql]
}, {
input: "INSERT INTO `X` values(\"test\",0, 1 , 2, 'test')",
output: "INSERT INTO `X` values(?)",
dialects: %i[mysql]
}, {
input: "SELECT c11.col1, c22.col2 FROM table c11, table c22 WHERE value='nothing'",
output: "SELECT c11.col1, c22.col2 FROM table c11, table c22 WHERE value=?",
dialects: ALL_DIALECTS
}, {
input: "INSERT INTO X VALUES(1, 23456, 123.456, 99+100)",
output: "INSERT INTO X VALUES(?, ?, ?, ?+?)",
output: "INSERT INTO X VALUES(?)",
dialects: ALL_DIALECTS
}, {
input: "SELECT * FROM table WHERE name=\"foo\" AND value=\"don't\"",
Expand Down Expand Up @@ -117,7 +121,7 @@
dialects: ALL_DIALECTS
}, {
input: "INSERT INTO X values('', 'a''b c',0, 1 , 'd''e f''s h')",
output: "INSERT INTO X values(?, ?,?, ? , ?)",
output: "INSERT INTO X values(?)",
dialects: ALL_DIALECTS
}, {
input: "SELECT * FROM t WHERE -- '\n bar='baz' -- '",
Expand Down Expand Up @@ -153,7 +157,7 @@
dialects: %i[postgres]
}, {
input: "INSERT INTO \"foo\" (\"bar\", \"baz\", \"qux\") VALUES ($1, $2, $3) RETURNING \"id\"",
output: "INSERT INTO \"foo\" (\"bar\", \"baz\", \"qux\") VALUES ($?, $?, $?) RETURNING \"id\"",
output: "INSERT INTO \"foo\" (?) RETURNING \"id\"",
dialects: %i[postgres]
}, {
input: "select * from foo where bar = 'some\\tthing' and baz = 10",
Expand Down