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

Add ActionCable support #309

Merged
merged 5 commits into from
Jun 15, 2017
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions lib/appsignal/hooks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ def extract_value(object_or_hash, field, default_value = nil, convert_to_s = fal
end
end

require "appsignal/hooks/action_cable"
require "appsignal/hooks/active_support_notifications"
require "appsignal/hooks/celluloid"
require "appsignal/hooks/delayed_job"
Expand Down
99 changes: 99 additions & 0 deletions lib/appsignal/hooks/action_cable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
module Appsignal
class Hooks
# @api private
class ActionCableHook < Appsignal::Hooks::Hook
register :action_cable

def dependencies_present?
defined?(::ActiveSupport::Notifications::Instrumenter) &&
defined?(::ActionCable)
end

def install
patch_perform_action
install_callbacks
end

private

def patch_perform_action
ActionCable::Channel::Base.class_eval do
alias_method :original_perform_action, :perform_action

def perform_action(*args, &block)
# The request is only the original websocket request
request = ActionDispatch::Request.new(connection.env)
transaction = Appsignal::Transaction.create(
request.request_id,
Appsignal::Transaction::ACTION_CABLE,
request
)

begin
original_perform_action(*args, &block)
rescue => exception
transaction.set_error(exception)
raise exception
ensure
transaction.params = args.first
transaction.set_action_if_nil("#{self.class}##{args.first["action"]}")
transaction.set_metadata("path", request.path)
transaction.set_metadata("method", "websocket")
Appsignal::Transaction.complete_current!
end
end
end
end

def install_callbacks
ActionCable::Channel::Base.set_callback :subscribe, :around, :prepend => true do |channel, inner|
# The request is only the original websocket request
request = ActionDispatch::Request.new(channel.connection.env)
transaction = Appsignal::Transaction.create(
request.request_id,
Appsignal::Transaction::ACTION_CABLE,
request
)

begin
Appsignal.instrument "subscribed.action_cable" do
inner.call
end
rescue => exception
transaction.set_error(exception)
raise exception
ensure
transaction.set_action_if_nil("#{channel.class}#subscribed")
transaction.set_metadata("path", request.path)
transaction.set_metadata("method", "websocket")
Appsignal::Transaction.complete_current!
end
end

ActionCable::Channel::Base.set_callback :unsubscribe, :around, :prepend => true do |channel, inner|
# The request is only the original websocket request
request = ActionDispatch::Request.new(channel.connection.env)
transaction = Appsignal::Transaction.create(
request.request_id,
Appsignal::Transaction::ACTION_CABLE,
request
)

begin
Appsignal.instrument "unsubscribed.action_cable" do
inner.call
end
rescue => exception
transaction.set_error(exception)
raise exception
ensure
transaction.set_action_if_nil("#{channel.class}#unsubscribed")
transaction.set_metadata("path", request.path)
transaction.set_metadata("method", "websocket")
Appsignal::Transaction.complete_current!
end
end
end
end
end
end
1 change: 1 addition & 0 deletions lib/appsignal/transaction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module Appsignal
class Transaction
HTTP_REQUEST = "http_request".freeze
BACKGROUND_JOB = "background_job".freeze
ACTION_CABLE = "action_cable".freeze
FRONTEND = "frontend".freeze
BLANK = "".freeze

Expand Down
Loading