From 4168abcf1ed6cc23649853f1438f61ad6333414f Mon Sep 17 00:00:00 2001 From: Justin George Date: Thu, 13 May 2010 14:07:40 -0700 Subject: [PATCH] add start event api to allow instrumentation of start events framework-wide [#4594 state:resolved] Useful for places where we care about when things start more than how long they take --- activesupport/lib/active_support/notifications.rb | 2 +- .../active_support/notifications/instrumenter.rb | 11 +++++ activesupport/test/notifications_test.rb | 41 +++++++++++++++++++- 3 files changed, 52 insertions(+), 2 deletions(-) diff --git a/activesupport/lib/active_support/notifications.rb b/activesupport/lib/active_support/notifications.rb index 3f1fe64..5448b9f 100644 --- a/activesupport/lib/active_support/notifications.rb +++ b/activesupport/lib/active_support/notifications.rb @@ -45,7 +45,7 @@ module ActiveSupport class << self attr_writer :notifier delegate :publish, :subscribe, :unsubscribe, :to => :notifier - delegate :instrument, :to => :instrumenter + delegate :instrument, :instrument_with_start, :to => :instrumenter def notifier @notifier ||= Notifier.new diff --git a/activesupport/lib/active_support/notifications/instrumenter.rb b/activesupport/lib/active_support/notifications/instrumenter.rb index 7e89402..3d40852 100644 --- a/activesupport/lib/active_support/notifications/instrumenter.rb +++ b/activesupport/lib/active_support/notifications/instrumenter.rb @@ -26,6 +26,17 @@ module ActiveSupport end end + def instrument_with_start(name, payload={}) + if block_given? + instrument("#{name}.start", payload.dup) + instrument(name, payload) do + yield(payload) + end + else + instrument(name, payload) + end + end + private def unique_id SecureRandom.hex(10) diff --git a/activesupport/test/notifications_test.rb b/activesupport/test/notifications_test.rb index e11de5f..277a49e 100644 --- a/activesupport/test/notifications_test.rb +++ b/activesupport/test/notifications_test.rb @@ -127,6 +127,45 @@ module Notifications end end + class InstrumentWithStartTest < TestCase + delegate :instrument_with_start, :to => ActiveSupport::Notifications + + def test_instrument_with_start_returns_block_result + assert_equal 4, instrument_with_start(:even_better) { 2 + 2 } + end + + def test_instrument_with_start_creates_one_event_with_no_block + instrument_with_start(:do_stuff_man) + drain + assert_equal 1, @events.size + assert_equal :do_stuff_man, @events.first.name + end + + def test_instrument_with_start_creates_two_events_when_given_block + instrument_with_start(:do_stuff_man) { nil } + drain + assert_equal 2, @events.size + end + + def test_instrument_with_start_uses_correct_start_event_name + instrument_with_start(:do_stuff_man) { nil } + drain + assert_equal 2, @events.size + assert_equal 'do_stuff_man.start', @events.first.name + end + + def test_instrument_with_start_yields_payload_to_second_notification + assert_equal 4, instrument_with_start(:even_better) { |p| p[:result] = 2 + 2 } + drain + + assert_equal 2, @events.size + assert_equal :even_better, @events.last.name + assert_equal Hash[:result => 4], @events.last.payload + end + + end + + class InstrumentationTest < TestCase delegate :instrument, :to => ActiveSupport::Notifications @@ -135,7 +174,7 @@ module Notifications drain end - def test_instrument_yields_the_paylod_for_further_modification + def test_instrument_yields_payload_for_further_modification assert_equal 2, instrument(:awesome) { |p| p[:result] = 1 + 1 } drain -- 1.7.0.3