diff --git a/lib/puppet/util/instrumentation/listeners/log.rb b/lib/puppet/util/instrumentation/listeners/log.rb new file mode 100644 index 000000000..59e9daff9 --- /dev/null +++ b/lib/puppet/util/instrumentation/listeners/log.rb @@ -0,0 +1,29 @@ +require 'monitor' + +# This is an example instrumentation listener that stores the last +# 20 instrumented probe run time. +Puppet::Util::Instrumentation.new_listener(:log) do + + SIZE = 20 + + attr_accessor :last_logs + + def initialize + @last_logs = {}.extend(MonitorMixin) + end + + def notify(label, event, data) + return if event == :start + log_line = "#{label} took #{data[:finished] - data[:started]}" + @last_logs.synchronize { + (@last_logs[label] ||= []) << log_line + @last_logs[label].shift if @last_logs[label].length > SIZE + } + end + + def data + @last_logs.synchronize { + @last_logs.dup + } + end +end \ No newline at end of file diff --git a/spec/unit/util/instrumentation/listeners/log_spec.rb b/spec/unit/util/instrumentation/listeners/log_spec.rb new file mode 100755 index 000000000..8359625a1 --- /dev/null +++ b/spec/unit/util/instrumentation/listeners/log_spec.rb @@ -0,0 +1,34 @@ +require 'spec_helper' +require 'puppet/util/instrumentation' + +Puppet::Util::Instrumentation.init +log = Puppet::Util::Instrumentation.listener(:log) + +describe log do + before(:each) do + @log = log.new + end + + it "should have a notify method" do + @log.should respond_to(:notify) + end + + it "should have a data method" do + @log.should respond_to(:data) + end + + it "should keep data for stop event" do + @log.notify(:test, :stop, { :started => Time.at(123456789), :finished => Time.at(123456790)}) + @log.data.should == {:test=>["test took 1.0"]} + end + + it "should not keep data for start event" do + @log.notify(:test, :start, { :started => Time.at(123456789)}) + @log.data.should be_empty + end + + it "should not keep more than 20 events per label" do + 25.times { @log.notify(:test, :stop, { :started => Time.at(123456789), :finished => Time.at(123456790)}) } + @log.data[:test].size.should == 20 + end +end \ No newline at end of file