class Puppet::Util::Instrumentation::Instrumentable::Probe

Attributes

data[R]
klass[R]
label[R]
method[R]

Public Class Methods

new(method, klass, options = {}) click to toggle source
# File lib/puppet/util/instrumentation/instrumentable.rb, line 25
def initialize(method, klass, options = {})
  @method = method
  @klass = klass

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

Public Instance Methods

disable() click to toggle source
# File lib/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
enable() click to toggle source
# File lib/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?() click to toggle source
# File lib/puppet/util/instrumentation/instrumentable.rb, line 72
def enabled?
  !!@enabled
end