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

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

Constant Summary collapse

QUERY_TOO_LARGE_MESSAGE =
'Query too large (over 16k characters) to safely obfuscate'.freeze
ELLIPSIS =
'...'.freeze

Constants included from ObfuscationHelpers

NewRelic::Agent::Database::ObfuscationHelpers::CASSANDRA_COMPONENTS_REGEX, NewRelic::Agent::Database::ObfuscationHelpers::CLEANUP_REGEX, NewRelic::Agent::Database::ObfuscationHelpers::COMPONENTS_REGEX_MAP, NewRelic::Agent::Database::ObfuscationHelpers::DIALECT_COMPONENTS, NewRelic::Agent::Database::ObfuscationHelpers::FAILED_TO_OBFUSCATE_MESSAGE, NewRelic::Agent::Database::ObfuscationHelpers::FALLBACK_REGEX, NewRelic::Agent::Database::ObfuscationHelpers::MYSQL_COMPONENTS_REGEX, NewRelic::Agent::Database::ObfuscationHelpers::ORACLE_COMPONENTS_REGEX, NewRelic::Agent::Database::ObfuscationHelpers::PLACEHOLDER, NewRelic::Agent::Database::ObfuscationHelpers::POSTGRES_COMPONENTS_REGEX, NewRelic::Agent::Database::ObfuscationHelpers::SQLITE_COMPONENTS_REGEX

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ObfuscationHelpers

#detect_unmatched_pairs, generate_regex, #obfuscate, #obfuscate_single_quote_literals

Constructor Details

#initializeObfuscator

Returns a new instance of Obfuscator.



19
20
21
# File 'lib/new_relic/agent/database/obfuscator.rb', line 19

def initialize
  reset
end

Instance Attribute Details

#obfuscatorObject (readonly)



14
15
16
# File 'lib/new_relic/agent/database/obfuscator.rb', line 14

def obfuscator
  @obfuscator
end

Instance Method Details

#default_sql_obfuscator(sql) ⇒ Object



50
51
52
53
54
55
56
57
58
# File 'lib/new_relic/agent/database/obfuscator.rb', line 50

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

#resetObject



23
24
25
# File 'lib/new_relic/agent/database/obfuscator.rb', line 23

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



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/new_relic/agent/database/obfuscator.rb', line 38

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