Module: OpenTrace::SqlNormalizer
- Defined in:
- lib/opentrace/sql_normalizer.rb
Class Method Summary collapse
-
.fingerprint(sql) ⇒ Object
Compute a fingerprint for a normalized query.
-
.normalize(sql) ⇒ Object
Replace literal values with ? placeholders for grouping.
Class Method Details
.fingerprint(sql) ⇒ Object
Compute a fingerprint for a normalized query. Two queries with the same fingerprint are “the same query with different params.”
24 25 26 27 |
# File 'lib/opentrace/sql_normalizer.rb', line 24 def fingerprint(sql) normalized = normalize(sql) Digest::MD5.hexdigest(normalized)[0, 12] end |
.normalize(sql) ⇒ Object
Replace literal values with ? placeholders for grouping. Handles: integers, floats, single-quoted strings, double-quoted strings, hex literals, boolean literals, NULL.
normalize("SELECT * FROM users WHERE id = 42 AND name = 'Alice'")
# => "SELECT * FROM users WHERE id = ? AND name = ?"
16 17 18 19 20 |
# File 'lib/opentrace/sql_normalizer.rb', line 16 def normalize(sql) return sql if sql.nil? || sql.empty? sql.gsub(LITERAL_PATTERN, "?") end |