eprothro
2/13/2017 - 4:39 PM

Ruby instrumentation glue example

Ruby instrumentation glue example

module Instrumentation

  def action(*args)
    super.tap do |r|
      puts "#=> instrumented action: succes=#{r} args=#{args}"
    end
  end
end

class Base
  prepend Instrumentation

  def self.inherited(subclass)
    subclass.prepend Instrumentation
  end

  def action(arg)
    false
  end
end

class SubClass < Base

  def action(arg)
    puts "#=> some specific algorithm with #{arg}"
    true
  end
end

SubClass.new.action(:foo)
Base.new.action(:foo)
#=> instrumented action: succes=true args=[:foo]
#=> false

SubClass.new.action(:foo)
#=> some specific algorithm with foo
#=> instrumented action: succes=true args=[:foo]
=> true