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
more...

Defined Under Namespace

Modules: Prepend

Constant Summary collapse

INSTRUMENTATION_NAME =
'Logger'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.clear_skip_instrumenting(logger) ⇒ Object

[View source]

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

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

  logger.instance_variable_set(:@skip_instrumenting, false)
end

.enabled?Boolean

Returns:

  • (Boolean)
[View source]

42
43
44
# File 'lib/new_relic/agent/instrumentation/logger/instrumentation.rb', line 42

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

.instrument!Object

[View source]

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.

[View source]

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

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

  logger.instance_variable_set(:@skip_instrumenting, true)
end

Instance Method Details

#clear_skip_instrumentingObject

[View source]

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

def clear_skip_instrumenting
  return if frozen?

  @skip_instrumenting = false
end

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

[View source]

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

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.record_instrumentation_invocation(INSTRUMENTATION_NAME)
      ::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

[View source]

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

def mark_skip_instrumenting
  return if frozen?

  @skip_instrumenting = true
end

#skip_instrumenting?Boolean

Returns:

  • (Boolean)
[View source]

11
12
13
# File 'lib/new_relic/agent/instrumentation/logger/instrumentation.rb', line 11

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