nt-fingerprint
Synopsis
nt-fingerprint is a Ruby gem for converting queries into fingerprints.
require 'nt/fingerprint'
class Sample
include Nt::Fingerprint
end
puts Sample.new.fingerprint("SELECT a, b, 'c' FROM t WEHERE a = 1 AND b IN (1, 2, 3)")
nt-fingerprint exposes a single method fingerprint
. What the method does is just calling a function query.Fingerprint
of percona/go-mysql via FFI. Thus, the behavior of fingerprint
completely follows that of query.Fingerprint
. We quote the description of query.Fingerprint
from the documentation of go-mysql.
func Fingerprint(q string) string
Fingerprint returns the canonical form of q. The primary transformations are:
- Replace values with ?
- Collapse whitespace
- Remove comments
- Lowercase everything
Additional trasnformations are performed which change the syntax of the original query without affecting its performance characteristics. For example, "ORDER BY col ASC" is the same as "ORDER BY col", so "ASC" in the fingerprint is removed.
Examples
A typical use case of nt-fingerprint is query logging. Using nt-fingerprint, one can exclude sensitive information from queries. This makes it possible to log production queries while maintaining security.
For example, one can capture arbitrary ActiveRecord queries by combining Arproxy and nt-fingerprint.
class QueryLogger < Arproxy::Base
include Nt::Fingerprint
def execute(sql, name=nil)
Rails.logger.info(fingerprint(sql))
super(sql, name)
end
end
Arproxy.configure do |config|
config.use QueryLogger
end