Class: NewRelic::Agent::Database::Obfuscator
- Inherits:
-
Object
- Object
- NewRelic::Agent::Database::Obfuscator
- Includes:
- Singleton
- Defined in:
- lib/new_relic/agent/database.rb
Instance Attribute Summary collapse
-
#obfuscator ⇒ Object
readonly
Returns the value of attribute obfuscator.
Instance Method Summary collapse
- #default_sql_obfuscator(sql) ⇒ Object
-
#initialize ⇒ Obfuscator
constructor
A new instance of Obfuscator.
- #obfuscate_double_quote_literals(sql) ⇒ Object
- #obfuscate_numeric_literals(sql) ⇒ Object
- #obfuscate_single_quote_literals(sql) ⇒ Object
- #remove_escaped_quotes(sql) ⇒ Object
- #reset ⇒ Object
-
#set_sql_obfuscator(type, &block) ⇒ Object
Sets the sql obfuscator used to clean up sql when sending it to the server.
Constructor Details
#initialize ⇒ Obfuscator
Returns a new instance of Obfuscator.
173 174 175 |
# File 'lib/new_relic/agent/database.rb', line 173 def initialize reset end |
Instance Attribute Details
#obfuscator ⇒ Object (readonly)
Returns the value of attribute obfuscator.
171 172 173 |
# File 'lib/new_relic/agent/database.rb', line 171 def obfuscator @obfuscator end |
Instance Method Details
#default_sql_obfuscator(sql) ⇒ Object
204 205 206 207 208 209 210 211 212 213 214 |
# File 'lib/new_relic/agent/database.rb', line 204 def default_sql_obfuscator(sql) stmt = sql.kind_of?(Statement) ? sql : Statement.new(sql) adapter = stmt.adapter = remove_escaped_quotes(stmt) = obfuscate_single_quote_literals() if !(adapter.to_s =~ /postgres/ || adapter.to_s =~ /sqlite/) = obfuscate_double_quote_literals() end = obfuscate_numeric_literals() .to_s # return back to a regular String end |
#obfuscate_double_quote_literals(sql) ⇒ Object
224 225 226 |
# File 'lib/new_relic/agent/database.rb', line 224 def obfuscate_double_quote_literals(sql) sql.gsub(/"(?:[^"]|"")*"/, '?') end |
#obfuscate_numeric_literals(sql) ⇒ Object
228 229 230 |
# File 'lib/new_relic/agent/database.rb', line 228 def obfuscate_numeric_literals(sql) sql.gsub(/\b\d+\b/, "?") end |
#obfuscate_single_quote_literals(sql) ⇒ Object
220 221 222 |
# File 'lib/new_relic/agent/database.rb', line 220 def obfuscate_single_quote_literals(sql) sql.gsub(/'(?:[^']|'')*'/, '?') end |
#remove_escaped_quotes(sql) ⇒ Object
216 217 218 |
# File 'lib/new_relic/agent/database.rb', line 216 def remove_escaped_quotes(sql) sql.gsub(/\\"/, '').gsub(/\\'/, '') end |
#reset ⇒ Object
177 178 179 |
# File 'lib/new_relic/agent/database.rb', line 177 def reset @obfuscator = method(:default_sql_obfuscator) end |
#set_sql_obfuscator(type, &block) ⇒ Object
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
192 193 194 195 196 197 198 199 200 201 202 |
# File 'lib/new_relic/agent/database.rb', line 192 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 |