Class: RightScale::Tracer

Inherits:
Object show all
Defined in:
lib/right_agent/tracer.rb

Constant Summary collapse

NON_TRACEABLE_CLASSES =
[ 'Kernel', 'Module', 'Object' , 'SyslogLogger', 'RightSupport::Log::SystemLogger' ] +
[ 'RightScale::Tracer', 'RightScale::Multiplexer' ] +
[ 'RightScale::Log', 'RightScale::Log::Formatter' ]
NON_TRACEABLE_METHODS =
[ :metaclass, :method_missing, :method_added, :blank_slate_method_added, :[], :[]= ]
NON_TRACEABLE_CLASS_METHODS =
[ :initialize, :initialize_copy, :inherited, :new, :allocate, :superclass ]

Class Method Summary collapse

Class Method Details

.add_tracing_to_class(klass) ⇒ Object

Add logs when entering and exiting instance and class methods defined on given class

Parameters

klass(Class)

Class whose methods should be traced

Return

true

Always return true



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/right_agent/tracer.rb', line 52

def self.add_tracing_to_class(klass)
  return true if NON_TRACEABLE_CLASSES.include?(klass.to_s)
  (klass.public_instance_methods(all=false) + klass.private_instance_methods(all=false) +
          klass.protected_instance_methods(all=false)).each do |m|
    if traceable(m)
      old_m = klass.instance_method(m)
      klass.module_eval <<-EOM
        alias :o_l_d_#{m} :#{m}
        def #{m}(*args, &blk)
          Log.debug("<<< #{klass}##{m}(" + args.map(&:inspect).join(',') + ")")
          res = o_l_d_#{m}(*args, &blk)
          Log.debug(">>> #{klass}##{m}")
          res
        end
      EOM
    end
  end
  (klass.public_methods(all=false) + klass.private_methods(all=false) +
          klass.protected_methods(all=false)).each do |m|
    if traceable(m, static=true)
      old_m = klass.method(m)
      klass.module_eval <<-EOM
        class << self
          alias :o_l_d_#{m} :#{m}
          def #{m}(*args, &blk)
            Log.debug("<<< #{klass}.#{m}(" + args.map(&:inspect).join(',') + ")")
            res = o_l_d_#{m}(*args, &blk)
            Log.debug(">>> #{klass}.#{m}")
            res
          end
        end
      EOM
    end
  end
  true
end

.add_tracing_to_namespaces(namespaces) ⇒ Object

Add tracing to all classes in given namespaces

Parameters

namespaces(Array|String)

Namespace(s) of classes whose methods should be traced

Return

true

Always return true



110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/right_agent/tracer.rb', line 110

def self.add_tracing_to_namespaces(namespaces)
  namespaces = [ namespaces ] unless namespaces.respond_to?(:inject)
  regexps = namespaces.inject([]) { |reg, n| reg << "^#{n}::" }
  unless regexps.empty?
    ObjectSpace.each_object(Class) do |c|
      if c.to_s =~ /#{regexps.join('|')}/
        add_tracing_to_class(c)
      end
    end
  end 
  true
end

.traceable(m, static = false) ⇒ Object

Can method be traced?

Parameters

m(String)

Method name

static(Boolean)

Whether method is a class method

Return

traceable(Boolean)

true if method can be traced, false otherwise



97
98
99
100
101
# File 'lib/right_agent/tracer.rb', line 97

def self.traceable(m, static=false)
  traceable = !NON_TRACEABLE_METHODS.include?(m.to_sym) && m =~ /[a-zA-Z0-9]$/
  traceable &&= !NON_TRACEABLE_CLASS_METHODS.include?(m.to_sym) if static
  traceable
end