Skip to content

Commit

Permalink
Fix session data for Action Cable callbacks (#1314)
Browse files Browse the repository at this point in the history
In issue #1313 it was reported that session data was not reported for
Action Cable callbacks. This broke after PR #1209 where we changed the
way we handle sample data by allowing merging, which required more
strict type checks.

By calling `to_h`, ensure the value is a Hash, which is a valid sample
data type.

Fixes #1313
  • Loading branch information
tombruijn authored Oct 4, 2024
1 parent 8fb8b93 commit 41642be
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 3 deletions.
6 changes: 6 additions & 0 deletions .changesets/fix-session-data-for-action-cable-callbacks.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
bump: patch
type: fix
---

Fix session data reporting for Action Cable actions.
4 changes: 2 additions & 2 deletions lib/appsignal/hooks/action_cable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def install_subscribe_callback
transaction.set_metadata("method", "websocket")
transaction.add_params_if_nil { request.params }
transaction.add_headers_if_nil { request.env }
transaction.add_session_data { request.session if request.respond_to? :session }
transaction.add_session_data { request.session.to_h if request.respond_to? :session }
transaction.add_tags(:request_id => request_id) if request_id
Appsignal::Transaction.complete_current!
end
Expand Down Expand Up @@ -88,7 +88,7 @@ def install_unsubscribe_callback
transaction.set_metadata("method", "websocket")
transaction.add_params_if_nil { request.params }
transaction.add_headers_if_nil { request.env }
transaction.add_session_data { request.session if request.respond_to? :session }
transaction.add_session_data { request.session.to_h if request.respond_to? :session }
transaction.add_tags(:request_id => request_id) if request_id
Appsignal::Transaction.complete_current!
end
Expand Down
1 change: 1 addition & 0 deletions lib/appsignal/integrations/action_cable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def perform_action(*args, &block)
ensure
transaction.set_action_if_nil("#{self.class}##{args.first["action"]}")
transaction.add_params_if_nil(args.first)
transaction.add_session_data { request.session.to_h if request.respond_to? :session }
transaction.set_metadata("path", request.path)
transaction.set_metadata("method", "websocket")
transaction.add_tags(:request_id => request_id) if request_id
Expand Down
31 changes: 30 additions & 1 deletion spec/lib/appsignal/hooks/action_cable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,16 @@ def self.to_s
:with_queue_start => true
)
end
let(:connection) { ActionCable::Connection::Base.new(server, env) }
let(:request) do
ActionDispatch::Request.new(env).tap do |req|
set_rails_session_data(
req,
"user_id" => "123",
"session" => "yes"
)
end
end
let(:connection) { ActionCable::Connection::Base.new(server, request.env) }
let(:identifier) { { :channel => "MyChannel" }.to_json }
let(:params) { {} }
let(:request_id) { SecureRandom.uuid }
Expand Down Expand Up @@ -75,6 +84,10 @@ def self.to_s
"action" => "speak",
"message" => "foo"
)
expect(transaction).to include_session_data(
"user_id" => "123",
"session" => "yes"
)
expect(transaction).to include_tags("request_id" => request_id)
expect(transaction).to_not have_queue_start
expect(transaction).to be_completed
Expand Down Expand Up @@ -152,6 +165,10 @@ def self.to_s
"name" => "subscribed.action_cable",
"title" => ""
)
expect(transaction).to include_session_data(
"user_id" => "123",
"session" => "yes"
)
expect(transaction).to include_tags("request_id" => request_id)
expect(transaction).to_not have_queue_start
expect(transaction).to be_completed
Expand Down Expand Up @@ -194,6 +211,10 @@ def self.to_s
"path" => "/blog"
)
expect(transaction).to include_params("internal" => "true")
expect(transaction).to include_session_data(
"user_id" => "123",
"session" => "yes"
)
expect(transaction).to_not have_queue_start
expect(transaction).to be_completed
end
Expand Down Expand Up @@ -246,6 +267,10 @@ def self.to_s
"path" => "/blog"
)
expect(transaction).to include_params("internal" => "true")
expect(transaction).to include_session_data(
"user_id" => "123",
"session" => "yes"
)
expect(transaction).to include_event(
"body" => "",
"body_format" => Appsignal::EventFormatter::DEFAULT,
Expand Down Expand Up @@ -294,6 +319,10 @@ def self.to_s
"path" => "/blog"
)
expect(transaction).to include_params("internal" => "true")
expect(transaction).to include_session_data(
"user_id" => "123",
"session" => "yes"
)
expect(transaction).to_not have_queue_start
expect(transaction).to be_completed
end
Expand Down
28 changes: 28 additions & 0 deletions spec/support/helpers/env_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,32 @@ def background_env_with_data(args = {})
:queue_start => fixed_time
}.merge(args)
end

def set_rails_session_data(request, data)
ActionDispatch::Request::Session.create(
rails_session_store(data),
request,
{}
)
end

def rails_session_store(data)
Class.new do
def initialize(data)
@data = data
end

def load_session(_env)
[1, @data]
end

def session_exists?(_env)
true
end

def delete_session(_env, _id, _options)
123
end
end.new(data)
end
end

0 comments on commit 41642be

Please sign in to comment.