This listener stores the last 20 event durations for a given label. It is not terribly useful except giving an idea of the api.
Signed-off-by: Brice Figureau <[email protected]> --- lib/puppet/util/instrumentation/listeners/log.rb | 24 +++++++++++++ .../util/instrumentation/listeners/log_spec.rb | 35 ++++++++++++++++++++ 2 files changed, 59 insertions(+), 0 deletions(-) create mode 100644 lib/puppet/util/instrumentation/listeners/log.rb create mode 100644 spec/unit/util/instrumentation/listeners/log_spec.rb diff --git a/lib/puppet/util/instrumentation/listeners/log.rb b/lib/puppet/util/instrumentation/listeners/log.rb new file mode 100644 index 0000000..34d6564 --- /dev/null +++ b/lib/puppet/util/instrumentation/listeners/log.rb @@ -0,0 +1,24 @@ +require 'monitor' + +# This is an example instrumentation listener that stores the last +# 20 instrumentated probe run time. +Puppet::Util::Instrumentation.new_listener(:log) do + + SIZE = 20 + + attr_accessor :last_logs + + def notify(label, event, data) + return if event == :start + @last_logs ||= {}.extend(MonitorMixin) + 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 + 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 100644 index 0000000..6b97e0c --- /dev/null +++ b/spec/unit/util/instrumentation/listeners/log_spec.rb @@ -0,0 +1,35 @@ +Dir.chdir(File.dirname(__FILE__)) { (s = lambda { |f| File.exist?(f) ? require(f) : Dir.chdir("..") { s.call(f) } }).call("spec/spec_helper.rb") } + +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_nil + 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 -- 1.7.2.1 -- You received this message because you are subscribed to the Google Groups "Puppet Developers" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/puppet-dev?hl=en.
