module NewRelic::Agent::Instrumentation::Logger

Constants

INSTRUMENTATION_NAME

Public Class Methods

clear_skip_instrumenting(logger) click to toggle source
# 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?() click to toggle source
# File lib/new_relic/agent/instrumentation/logger/instrumentation.rb, line 42
def self.enabled?
  NewRelic::Agent.config[:'instrumentation.logger'] != 'disabled'
end
instrument!() click to toggle source
# 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) click to toggle source

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.

# 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

Public Instance Methods

clear_skip_instrumenting() click to toggle source
# File lib/new_relic/agent/instrumentation/logger/instrumentation.rb, line 36
def clear_skip_instrumenting
  return if frozen?

  @skip_instrumenting = false
end
format_message(severity, datetime, progname, msg) click to toggle source
# File lib/new_relic/agent/instrumentation/logger/chain.rb, line 13
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
format_message_with_tracing(severity, datetime, progname, msg) { || ... } click to toggle source
# 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_instrumenting() click to toggle source
# File lib/new_relic/agent/instrumentation/logger/instrumentation.rb, line 30
def mark_skip_instrumenting
  return if frozen?

  @skip_instrumenting = true
end
skip_instrumenting?() click to toggle source
# File lib/new_relic/agent/instrumentation/logger/instrumentation.rb, line 11
def skip_instrumenting?
  defined?(@skip_instrumenting) && @skip_instrumenting
end