Skip to content

Commit

Permalink
Fix Rails session data not being reported
Browse files Browse the repository at this point in the history
The Rails session data was a non Hash object. It would not be stored on
the transaction because it wasn't a Hash. It can be cast to a Hash, so
let's explicitly cast it before setting the session data.
  • Loading branch information
tombruijn committed Aug 23, 2024
1 parent 0a658e5 commit 1565c7f
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
6 changes: 6 additions & 0 deletions .changesets/fix-rails-session-data-not-being-reported.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
bump: patch
type: fix
---

Fix Rails session data not being reported.
16 changes: 13 additions & 3 deletions lib/appsignal/rack/abstract_middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,7 @@ def add_transaction_metadata_after(transaction, request)
transaction.set_metadata("method", request_method) if request_method

transaction.add_params { params_for(request) }
transaction.add_session_data do
request.session if request.respond_to?(:session)
end
transaction.add_session_data { session_data_for(request) }
transaction.add_headers do
request.env if request.respond_to?(:env)
end
Expand Down Expand Up @@ -174,6 +172,18 @@ def request_method_for(request)
nil
end

def session_data_for(request)
return unless request.respond_to?(:session)

request.session.to_h
rescue => error
Appsignal.internal_logger.error(
"Exception while fetching session data from '#{@request_class}': " \
"#{error.class} #{error}"
)
nil
end

def request_for(env)
@request_class.new(env)
end
Expand Down
17 changes: 17 additions & 0 deletions spec/lib/appsignal/rack/abstract_middleware_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
describe Appsignal::Rack::AbstractMiddleware do
class HashLike < Hash
def initialize(value)
@value = value
end

def to_h
@value
end
end

let(:app) { DummyApp.new }
let(:request_path) { "/some/path" }
let(:env) do
Expand Down Expand Up @@ -261,6 +271,13 @@ def params

expect(last_transaction).to include_session_data("session" => "data", "user_id" => 123)
end

it "sets session data if the session is a Hash-like type" do
env["rack.session"] = HashLike.new("hash-like" => "value", "user_id" => 123)
make_request

expect(last_transaction).to include_session_data("hash-like" => "value", "user_id" => 123)
end
end

context "with queue start header" do
Expand Down

0 comments on commit 1565c7f

Please sign in to comment.