Class: StackifyRubyAPM::Spies::LoggingSpy Private

Inherits:
Object
  • Object
show all
Defined in:
lib/stackify_apm/spies/logging.rb

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Constant Summary collapse

LVL_LABEL =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

%w(DEBUG INFO WARN ERROR FATAL ANY).freeze

Instance Method Summary collapse

Instance Method Details

#installObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# 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