diff options
author | Matthew Wear <mwear@newrelic.com> | 2016-05-11 16:55:17 -0700 |
---|---|---|
committer | Matthew Wear <mwear@newrelic.com> | 2016-05-12 10:56:35 -0700 |
commit | f3ba0c1dd32821ad700b2784d160d1c161fb3125 (patch) | |
tree | f89c7555ed6f43a24bbb8d228a4a894dd624e007 /actioncable | |
parent | 537a342a8307ebe1d5e69a7e81495f137ae571a4 (diff) | |
download | rails-f3ba0c1dd32821ad700b2784d160d1c161fb3125.tar.gz rails-f3ba0c1dd32821ad700b2784d160d1c161fb3125.tar.bz2 rails-f3ba0c1dd32821ad700b2784d160d1c161fb3125.zip |
Add ActiveSupport::Notifications hook to Broadcaster#broadcast
This addition of this notification hook will give users better visibility
into the messages being sent over the PubSub adapter.
Diffstat (limited to 'actioncable')
-rw-r--r-- | actioncable/CHANGELOG.md | 3 | ||||
-rw-r--r-- | actioncable/lib/action_cable/server/broadcasting.rb | 8 | ||||
-rw-r--r-- | actioncable/test/server/broadcasting_test.rb | 53 |
3 files changed, 58 insertions, 6 deletions
diff --git a/actioncable/CHANGELOG.md b/actioncable/CHANGELOG.md index a767857607..9aee32ddb4 100644 --- a/actioncable/CHANGELOG.md +++ b/actioncable/CHANGELOG.md @@ -1,2 +1,5 @@ +* Add ActiveSupport::Notifications hook to Broadcaster#broadcast + + *Matthew Wear* Please check [5-0-stable](https://github.com/rails/rails/blob/5-0-stable/actioncable/CHANGELOG.md) for previous changes. diff --git a/actioncable/lib/action_cable/server/broadcasting.rb b/actioncable/lib/action_cable/server/broadcasting.rb index 8f93564113..1fc58baa3e 100644 --- a/actioncable/lib/action_cable/server/broadcasting.rb +++ b/actioncable/lib/action_cable/server/broadcasting.rb @@ -39,8 +39,12 @@ module ActionCable def broadcast(message) server.logger.info "[ActionCable] Broadcasting to #{broadcasting}: #{message.inspect}" - encoded = coder ? coder.encode(message) : message - server.pubsub.broadcast broadcasting, encoded + + payload = { broadcasting: broadcasting, message: message, coder: coder } + ActiveSupport::Notifications.instrument("broadcast.action_cable", payload) do + encoded = coder ? coder.encode(message) : message + server.pubsub.broadcast broadcasting, encoded + end end end end diff --git a/actioncable/test/server/broadcasting_test.rb b/actioncable/test/server/broadcasting_test.rb index 3b4a7eaf90..ed377b7d5d 100644 --- a/actioncable/test/server/broadcasting_test.rb +++ b/actioncable/test/server/broadcasting_test.rb @@ -1,10 +1,7 @@ require "test_helper" +require "stubs/test_server" class BroadcastingTest < ActiveSupport::TestCase - class TestServer - include ActionCable::Server::Broadcasting - end - test "fetching a broadcaster converts the broadcasting queue to a string" do broadcasting = :test_queue server = TestServer.new @@ -12,4 +9,52 @@ class BroadcastingTest < ActiveSupport::TestCase assert_equal "test_queue", broadcaster.broadcasting end + + test "broadcast generates notification" do + begin + server = TestServer.new + + events = [] + ActiveSupport::Notifications.subscribe "broadcast.action_cable" do |*args| + events << ActiveSupport::Notifications::Event.new(*args) + end + + broadcasting = "test_queue" + message = { body: "test message" } + server.broadcast(broadcasting, message) + + assert_equal 1, events.length + assert_equal "broadcast.action_cable", events[0].name + assert_equal broadcasting, events[0].payload[:broadcasting] + assert_equal message, events[0].payload[:message] + assert_equal ActiveSupport::JSON, events[0].payload[:coder] + ensure + ActiveSupport::Notifications.unsubscribe "broadcast.action_cable" + end + end + + test "broadcaster from broadcaster_for generates notification" do + begin + server = TestServer.new + + events = [] + ActiveSupport::Notifications.subscribe "broadcast.action_cable" do |*args| + events << ActiveSupport::Notifications::Event.new(*args) + end + + broadcasting = "test_queue" + message = { body: "test message" } + + broadcaster = server.broadcaster_for(broadcasting) + broadcaster.broadcast(message) + + assert_equal 1, events.length + assert_equal "broadcast.action_cable", events[0].name + assert_equal broadcasting, events[0].payload[:broadcasting] + assert_equal message, events[0].payload[:message] + assert_equal ActiveSupport::JSON, events[0].payload[:coder] + ensure + ActiveSupport::Notifications.unsubscribe "broadcast.action_cable" + end + end end |