-
Notifications
You must be signed in to change notification settings - Fork 597
/
obfuscator.rb
62 lines (52 loc) · 1.76 KB
/
obfuscator.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# encoding: utf-8
# This file is distributed under New Relic's license terms.
# See https://github.com/newrelic/newrelic-ruby-agent/blob/main/LICENSE for complete details.
require 'new_relic/agent/database/obfuscation_helpers'
module NewRelic
module Agent
module Database
class Obfuscator
include Singleton
include ObfuscationHelpers
attr_reader :obfuscator
QUERY_TOO_LARGE_MESSAGE = "Query too large (over 16k characters) to safely obfuscate".freeze
ELLIPSIS = "...".freeze
def initialize
reset
end
def reset
@obfuscator = method(:default_sql_obfuscator)
end
# Sets the sql obfuscator used to clean up sql when sending it
# to the server. Possible types are:
#
# :before => sets the block to run before the existing
# obfuscators
#
# :after => sets the block to run after the existing
# obfuscator(s)
#
# :replace => removes the current obfuscator and replaces it
# with the provided block
def set_sql_obfuscator(type, &block)
if type == :before
@obfuscator = NewRelic::ChainedCall.new(block, @obfuscator)
elsif type == :after
@obfuscator = NewRelic::ChainedCall.new(@obfuscator, block)
elsif type == :replace
@obfuscator = block
else
fail "unknown sql_obfuscator type #{type}"
end
end
def default_sql_obfuscator(sql)
stmt = sql.kind_of?(Statement) ? sql : Statement.new(sql)
if stmt.sql.end_with? ELLIPSIS
return QUERY_TOO_LARGE_MESSAGE
end
obfuscate(stmt.sql, stmt.adapter).to_s
end
end
end
end
end