Class: NewRelic::Agent::Database::Obfuscator

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/new_relic/agent/database.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeObfuscator

Returns a new instance of Obfuscator.



185
186
187
# File 'lib/new_relic/agent/database.rb', line 185

def initialize
  reset
end

Instance Attribute Details

#obfuscatorObject (readonly)

Returns the value of attribute obfuscator.



183
184
185
# File 'lib/new_relic/agent/database.rb', line 183

def obfuscator
  @obfuscator
end

Instance Method Details

#default_sql_obfuscator(sql) ⇒ Object



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/new_relic/agent/database.rb', line 216

def default_sql_obfuscator(sql)
  if sql[-3,3] == '...'
    return "Query too large (over 16k characters) to safely obfuscate"
  end

  stmt = sql.kind_of?(Statement) ? sql : Statement.new(sql)
  adapter = stmt.adapter
  obfuscated = remove_escaped_quotes(stmt)
  obfuscated = obfuscate_single_quote_literals(obfuscated)
  if !(adapter.to_s =~ /postgres/ || adapter.to_s =~ /sqlite/)
    obfuscated = obfuscate_double_quote_literals(obfuscated)
  end
  obfuscated = obfuscate_numeric_literals(obfuscated)
  obfuscated.to_s # return back to a regular String
end

#obfuscate_double_quote_literals(sql) ⇒ Object



240
241
242
# File 'lib/new_relic/agent/database.rb', line 240

def obfuscate_double_quote_literals(sql)
  sql.gsub(/"(?:[^"]|"")*"/, '?')
end

#obfuscate_numeric_literals(sql) ⇒ Object



244
245
246
# File 'lib/new_relic/agent/database.rb', line 244

def obfuscate_numeric_literals(sql)
  sql.gsub(/\b\d+\b/, "?")
end

#obfuscate_single_quote_literals(sql) ⇒ Object



236
237
238
# File 'lib/new_relic/agent/database.rb', line 236

def obfuscate_single_quote_literals(sql)
  sql.gsub(/'(?:[^']|'')*'/, '?')
end

#remove_escaped_quotes(sql) ⇒ Object



232
233
234
# File 'lib/new_relic/agent/database.rb', line 232

def remove_escaped_quotes(sql)
  sql.gsub(/\\"/, '').gsub(/\\'/, '')
end

#resetObject



189
190
191
# File 'lib/new_relic/agent/database.rb', line 189

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



204
205
206
207
208
209
210
211
212
213
214
# File 'lib/new_relic/agent/database.rb', line 204

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