Class: Puppet::Util::Instrumentation::Instrumentable::Probe

Inherits:
Object
  • Object
show all
Defined in:
lib/vendor/puppet/util/instrumentation/instrumentable.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(method, klass, options = {}) ⇒ Probe

Returns a new instance of Probe.



25
26
27
28
29
30
31
# File 'lib/vendor/puppet/util/instrumentation/instrumentable.rb', line 25

def initialize(method, klass, options = {})
  @method = method
  @klass = klass

  @label = options[:label] || method
  @data = options[:data] || {}
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



23
24
25
# File 'lib/vendor/puppet/util/instrumentation/instrumentable.rb', line 23

def data
  @data
end

#klassObject (readonly)

Returns the value of attribute klass.



23
24
25
# File 'lib/vendor/puppet/util/instrumentation/instrumentable.rb', line 23

def klass
  @klass
end

#labelObject (readonly)

Returns the value of attribute label.



23
24
25
# File 'lib/vendor/puppet/util/instrumentation/instrumentable.rb', line 23

def label
  @label
end

#methodObject (readonly)

Returns the value of attribute method.



23
24
25
# File 'lib/vendor/puppet/util/instrumentation/instrumentable.rb', line 23

def method
  @method
end

Instance Method Details

#disableObject



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/vendor/puppet/util/instrumentation/instrumentable.rb', line 59

def disable
  raise "Probe is not enabled" unless enabled?

  # For the same reason as in #enable, we're forced to do a local
  # copy
  method = @method
  klass.class_eval do
    alias_method(method, "instrumented_#{method}")
    remove_method("instrumented_#{method}".to_sym)
  end
  @enabled = false
end

#enableObject



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
# File 'lib/vendor/puppet/util/instrumentation/instrumentable.rb', line 33

def enable
  raise "Probe already enabled" if enabled?

  # We're forced to perform this copy because in the class_eval'uated
  # block below @method would be evaluated in the class context. It's better
  # to close on locally-scoped variables than to resort to complex namespacing
  # to get access to the probe instance variables.
  method = @method; label = @label; data = @data
  klass.class_eval {
    alias_method("instrumented_#{method}", method)
    define_method(method) do |*args|
      id = nil
      instrumentation_data = nil
      begin
        instrumentation_label = label.respond_to?(:call) ? label.call(self, args) : label
        instrumentation_data = data.respond_to?(:call) ? data.call(self, args) : data
        id = Puppet::Util::Instrumentation.start(instrumentation_label, instrumentation_data)
        send("instrumented_#{method}".to_sym, *args)
      ensure
        Puppet::Util::Instrumentation.stop(instrumentation_label, id, instrumentation_data || {})
      end
    end
  }
  @enabled = true
end

#enabled?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/vendor/puppet/util/instrumentation/instrumentable.rb', line 72

def enabled?
  !!@enabled
end