Module: NewRelic::Agent::Instrumentation::Logger

Included in:
Prepend
Defined in:
lib/new_relic/agent/instrumentation/logger/chain.rb,
lib/new_relic/agent/instrumentation/logger/instrumentation.rb

Defined Under Namespace

Modules: Prepend

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.clear_skip_instrumenting(logger) ⇒ Object



22
23
24
25
26
# File 'lib/new_relic/agent/instrumentation/logger/instrumentation.rb', line 22

def self.clear_skip_instrumenting(logger)
  return if logger.frozen?

  logger.instance_variable_set(:@skip_instrumenting, false)
end

.enabled?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/new_relic/agent/instrumentation/logger/instrumentation.rb', line 40

def self.enabled?
  NewRelic::Agent.config[:'instrumentation.logger'] != 'disabled'
end

.instrument!Object



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/new_relic/agent/instrumentation/logger/chain.rb', line 7

def self.instrument!
  ::Logger.class_eval do
    include NewRelic::Agent::Instrumentation::Logger

    alias_method(:format_message_without_new_relic, :format_message)

    def format_message(severity, datetime, progname, msg)
      format_message_with_tracing(severity, datetime, progname, msg) do
        format_message_without_new_relic(severity, datetime, progname, msg)
      end
    end
  end
end

.mark_skip_instrumenting(logger) ⇒ Object

We support setting this on loggers which might not have instrumentation installed yet. This lets us disable in AgentLogger and AuditLogger without them having to know the inner details.



16
17
18
19
20
# File 'lib/new_relic/agent/instrumentation/logger/instrumentation.rb', line 16

def self.mark_skip_instrumenting(logger)
  return if logger.frozen?

  logger.instance_variable_set(:@skip_instrumenting, true)
end

Instance Method Details

#clear_skip_instrumentingObject



34
35
36
37
38
# File 'lib/new_relic/agent/instrumentation/logger/instrumentation.rb', line 34

def clear_skip_instrumenting
  return if frozen?

  @skip_instrumenting = false
end

#format_message_with_tracing(severity, datetime, progname, msg) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/new_relic/agent/instrumentation/logger/instrumentation.rb', line 44

def format_message_with_tracing(severity, datetime, progname, msg)
  formatted_message = yield
  return formatted_message if skip_instrumenting?

  begin
    # It's critical we don't instrument logging from metric recording
    # methods within NewRelic::Agent, or we'll stack overflow!!
    mark_skip_instrumenting

    unless ::NewRelic::Agent.agent.nil?
      ::NewRelic::Agent.agent.log_event_aggregator.record(formatted_message, severity)
      formatted_message = LocalLogDecorator.decorate(formatted_message)
    end

    formatted_message
  ensure
    clear_skip_instrumenting
  end
end

#mark_skip_instrumentingObject



28
29
30
31
32
# File 'lib/new_relic/agent/instrumentation/logger/instrumentation.rb', line 28

def mark_skip_instrumenting
  return if frozen?

  @skip_instrumenting = true
end

#skip_instrumenting?Boolean

Returns:

  • (Boolean)


9
10
11
# File 'lib/new_relic/agent/instrumentation/logger/instrumentation.rb', line 9

def skip_instrumenting?
  defined?(@skip_instrumenting) && @skip_instrumenting
end