class StackifyRubyAPM::Spies::LoggingSpy

Constants

EXCEPTION
LVL_LABEL

Public Instance Methods

install() click to toggle source
# File lib/stackify_apm/spies/logging.rb, line 11
def install
  Logging::Logger.class_eval do
    alias_method 'log_event_without_apm', 'log_event'

    # Log message formatted to an event object
    def log_event(event)
      return log_event_without_apm(event) unless StackifyRubyAPM.current_transaction

      begin
        name = 'logging'
        type = 'ext.logging'
        log_message = ''
        exception = nil

        obj = event.data

        case obj
        when String;
          log_message = obj
        when Exception
          log_message = obj.message
          exception = "(#{ obj.class.name })\n#{ obj.backtrace.join("\n") if obj.backtrace }"
        when nil;
          log_message = "(#{obj.class.name}) nil"
        else
          log_message = obj.to_s
        end

        ctx = Span::Context.new(
          CATEGORY: 'Log',
          SUBCATEGORY: 'Logging',
          LEVEL: LVL_LABEL[event.level] || 'ANY',
          MESSAGE: log_message,
          PREFIX: 'TRUE'
        )

        if exception
          ctx.EXCEPTION = exception
        end
      rescue Exception => e
        StackifyRubyAPM.agent.error "[LoggingSpy] Error: creating span context."
        StackifyRubyAPM.agent.error "[LoggingSpy] #{e.inspect}"
        return log_event_without_apm(event)
      end

      StackifyRubyAPM.span name, type, context: ctx do
        log_event_without_apm(event)
      end
    end
  end
end
log_event(event) click to toggle source

Log message formatted to an event object

# File lib/stackify_apm/spies/logging.rb, line 16
def log_event(event)
  return log_event_without_apm(event) unless StackifyRubyAPM.current_transaction

  begin
    name = 'logging'
    type = 'ext.logging'
    log_message = ''
    exception = nil

    obj = event.data

    case obj
    when String;
      log_message = obj
    when Exception
      log_message = obj.message
      exception = "(#{ obj.class.name })\n#{ obj.backtrace.join("\n") if obj.backtrace }"
    when nil;
      log_message = "(#{obj.class.name}) nil"
    else
      log_message = obj.to_s
    end

    ctx = Span::Context.new(
      CATEGORY: 'Log',
      SUBCATEGORY: 'Logging',
      LEVEL: LVL_LABEL[event.level] || 'ANY',
      MESSAGE: log_message,
      PREFIX: 'TRUE'
    )

    if exception
      ctx.EXCEPTION = exception
    end
  rescue Exception => e
    StackifyRubyAPM.agent.error "[LoggingSpy] Error: creating span context."
    StackifyRubyAPM.agent.error "[LoggingSpy] #{e.inspect}"
    return log_event_without_apm(event)
  end

  StackifyRubyAPM.span name, type, context: ctx do
    log_event_without_apm(event)
  end
end