Class: NewRelic::Agent::AuditLogger

Inherits:
Object
  • Object
show all
Defined in:
lib/new_relic/agent/audit_logger.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAuditLogger

Returns a new instance of AuditLogger.



13
14
15
16
17
18
# File 'lib/new_relic/agent/audit_logger.rb', line 13

def initialize
  @enabled = NewRelic::Agent.config[:'audit_log.enabled']
  @endpoints = NewRelic::Agent.config[:'audit_log.endpoints']
  @encoder = NewRelic::Agent::NewRelicService::Encoders::Identity
  @log = nil
end

Instance Attribute Details

#enabled=(value) ⇒ Object (writeonly)



20
21
22
# File 'lib/new_relic/agent/audit_logger.rb', line 20

def enabled=(value)
  @enabled = value
end

Instance Method Details

#allowed_endpoint?(uri) ⇒ Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/new_relic/agent/audit_logger.rb', line 59

def allowed_endpoint?(uri)
  @endpoints.any? { |endpoint| uri =~ endpoint }
end

#create_log_formatterObject



101
102
103
104
105
106
107
# File 'lib/new_relic/agent/audit_logger.rb', line 101

def create_log_formatter
  @hostname = NewRelic::Agent::Hostname.get
  @prefix = wants_stdout? ? '** [NewRelic]' : ''
  proc do |severity, time, progname, msg|
    "#{@prefix}[#{time} #{@hostname} (#{$$})] : #{msg}\n"
  end
end

#enabled?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/new_relic/agent/audit_logger.rb', line 22

def enabled?
  @enabled
end

#ensure_log_pathObject



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/new_relic/agent/audit_logger.rb', line 81

def ensure_log_path
  path = File.expand_path(NewRelic::Agent.config[:'audit_log.path'])
  log_dir = File.dirname(path)

  begin
    FileUtils.mkdir_p(log_dir)
    FileUtils.touch(path)
  rescue SystemCallError => e
    msg = "Audit log disabled, failed opening log at '#{path}': #{e}"
    ::NewRelic::Agent.logger.warn(msg)
    path = nil
  end

  path
end

#log_request(uri, data, marshaller) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/new_relic/agent/audit_logger.rb', line 41

def log_request(uri, data, marshaller)
  return unless enabled? && allowed_endpoint?(uri)

  setup_logger unless setup?
  request_body = if marshaller.class.human_readable?
    marshaller.dump(data, :encoder => @encoder)
  else
    marshaller.prepare(data, :encoder => @encoder).inspect
  end
  @log.info("REQUEST: #{uri}")
  @log.info("REQUEST BODY: #{request_body}")
rescue StandardError, SystemStackError, SystemCallError => e
  ::NewRelic::Agent.logger.warn('Failed writing to audit log', e)
rescue Exception => e
  ::NewRelic::Agent.logger.warn('Failed writing to audit log with exception. Re-raising in case of interrupt.', e)
  raise
end

#log_request_headers(uri, headers) ⇒ Object



30
31
32
33
34
35
36
37
38
39
# File 'lib/new_relic/agent/audit_logger.rb', line 30

def log_request_headers(uri, headers)
  return unless enabled? && allowed_endpoint?(uri)

  @log.info("REQUEST HEADERS: #{headers}")
rescue StandardError, SystemStackError, SystemCallError => e
  ::NewRelic::Agent.logger.warn('Failed writing request headers to audit log', e)
rescue Exception => e
  ::NewRelic::Agent.logger.warn('Failed writing request headers to audit log with exception. Re-raising in case of interrupt.', e)
  raise
end

#setup?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/new_relic/agent/audit_logger.rb', line 26

def setup?
  !@log.nil?
end

#setup_loggerObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/new_relic/agent/audit_logger.rb', line 63

def setup_logger
  if wants_stdout?
    # Using $stdout global for easier reassignment in testing
    @log = ::Logger.new($stdout)
    ::NewRelic::Agent.logger.info('Audit log enabled to STDOUT')
  elsif path = ensure_log_path
    @log = ::Logger.new(path)
    ::NewRelic::Agent.logger.info("Audit log enabled at '#{path}'")
  else
    @log = NewRelic::Agent::NullLogger.new
  end

  # Never have agent log forwarding capture audits
  NewRelic::Agent::Instrumentation::Logger.mark_skip_instrumenting(@log)

  @log.formatter = create_log_formatter
end

#wants_stdout?Boolean

Returns:

  • (Boolean)


97
98
99
# File 'lib/new_relic/agent/audit_logger.rb', line 97

def wants_stdout?
  ::NewRelic::Agent.config[:'audit_log.path'].casecmp(NewRelic::STANDARD_OUT) == 0
end