Class: Frequent::Probe

Inherits:
Object
  • Object
show all
Defined in:
lib/frequent.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Probe

Returns a new instance of Probe.



40
41
42
43
44
45
46
# File 'lib/frequent.rb', line 40

def initialize(name)
  @calls = 0
  @name = name
  @enabled = false
  parse_name(name)
  enable! if ready?
end

Instance Attribute Details

#callsObject (readonly)

Returns the value of attribute calls.



37
38
39
# File 'lib/frequent.rb', line 37

def calls
  @calls
end

#class_nameObject (readonly)

Returns the value of attribute class_name.



37
38
39
# File 'lib/frequent.rb', line 37

def class_name
  @class_name
end

#method_nameObject (readonly)

Returns the value of attribute method_name.



37
38
39
# File 'lib/frequent.rb', line 37

def method_name
  @method_name
end

#nameObject (readonly) Also known as: to_s

Returns the value of attribute name.



37
38
39
# File 'lib/frequent.rb', line 37

def name
  @name
end

#original_implementationObject (readonly)

Returns the value of attribute original_implementation.



37
38
39
# File 'lib/frequent.rb', line 37

def original_implementation
  @original_implementation
end

Instance Method Details

#aliased_nameObject



102
103
104
# File 'lib/frequent.rb', line 102

def aliased_name
  "__frequent_original_#{method_name}".to_sym
end

#disable!Object



92
93
94
95
96
97
98
99
100
# File 'lib/frequent.rb', line 92

def disable!
  if enabled?
    probe = self
    method_owner.class_eval do
      define_method(probe.method_name, probe.original_implementation)
      remove_method(probe.aliased_name)
    end
  end
end

#enable!Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/frequent.rb', line 74

def enable!
  unless @enabling
    @enabling = true
    @original_implementation = method_owner.instance_method(method_name)
    probe = self
    aliased_name = self.aliased_name
    method_owner.class_eval do
      alias_method aliased_name, probe.method_name
      define_method(probe.method_name) do |*args, &blk|
        probe.increment
        send(aliased_name, *args, &blk)
      end
    end
    @enabled = true
    @enabling = false
  end
end

#enabled?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/frequent.rb', line 52

def enabled?
  @enabled
end

#incrementObject



48
49
50
# File 'lib/frequent.rb', line 48

def increment
  @calls += 1
end

#method_ownerObject



60
61
62
63
64
# File 'lib/frequent.rb', line 60

def method_owner
  owner = Frequent.constantize(class_name)
  return unless owner
  @type == :instance ? owner : owner.singleton_class
end

#parse_name(name) ⇒ Object

Raises:



106
107
108
109
110
111
112
113
# File 'lib/frequent.rb', line 106

def parse_name(name)
  md = name.match(/(.*)(\.|\#)(.*)/)
  raise ProbeNameError.new("Failed to parse probe name '#{name}'") unless md
  class_name, sep, method_name = md[1..3]
  @class_name = class_name
  @type = (sep == '#') ? :instance : :class
  @method_name = method_name.to_sym
end

#ready?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/frequent.rb', line 56

def ready?
  !enabled? && target_defined?
end

#target_defined?Boolean

Returns:

  • (Boolean)


66
67
68
69
70
71
72
# File 'lib/frequent.rb', line 66

def target_defined?
  owner = method_owner
  owner && (
    owner.method_defined?(method_name) ||
    owner.private_instance_methods.include?(method_name)
  )
end