Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use prepend to patch Action Cable classes #310

Merged
merged 1 commit into from
Jan 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 7 additions & 14 deletions lib/lograge/rails_ext/action_cable/channel/base.rb
Original file line number Diff line number Diff line change
@@ -1,24 +1,15 @@
# frozen_string_literal: true

module ActionCable
module Channel
class Base
module Lograge
module ActionCable
module ChannelInstrumentation
def subscribe_to_channel
ActiveSupport::Notifications.instrument('subscribe.action_cable', notification_payload('subscribe')) do
run_callbacks :subscribe do
subscribed
end

reject_subscription if subscription_rejected?
ensure_confirmation_sent
end
ActiveSupport::Notifications.instrument('subscribe.action_cable', notification_payload('subscribe')) { super }
end

def unsubscribe_from_channel
ActiveSupport::Notifications.instrument('unsubscribe.action_cable', notification_payload('unsubscribe')) do
run_callbacks :unsubscribe do
unsubscribed
end
super
end
end

Expand All @@ -30,3 +21,5 @@ def notification_payload(method_name)
end
end
end

ActionCable::Channel::Base.prepend(Lograge::ActionCable::ChannelInstrumentation)
33 changes: 7 additions & 26 deletions lib/lograge/rails_ext/action_cable/connection/base.rb
Original file line number Diff line number Diff line change
@@ -1,40 +1,21 @@
# frozen_string_literal: true

module ActionCable
module Connection
class Base
module Lograge
module ActionCable
module ConnectionInstrumentation
def handle_open
ActiveSupport::Notifications.instrument('connect.action_cable', notification_payload('connect')) do
@protocol = websocket.protocol
connect if respond_to?(:connect)
subscribe_to_internal_channel
send_welcome_message

message_buffer.process!
server.add_connection(self)
rescue ActionCable::Connection::Authorization::UnauthorizedError
respond_to_invalid_request
end
ActiveSupport::Notifications.instrument('connect.action_cable', notification_payload('connect')) { super }
end

def handle_close
ActiveSupport::Notifications.instrument('disconnect.action_cable', notification_payload('disconnect')) do
logger.info finished_request_message if Lograge.lograge_config.keep_original_rails_log

server.remove_connection(self)

subscriptions.unsubscribe_from_all
unsubscribe_from_internal_channel

disconnect if respond_to?(:disconnect)
end
ActiveSupport::Notifications.instrument('disconnect.action_cable', notification_payload('disconnect')) { super }
end

private

def notification_payload(method_name)
{ connection_class: self.class.name, action: method_name, data: request.params }
end
end
end
end

ActionCable::Connection::Base.prepend(Lograge::ActionCable::ConnectionInstrumentation)