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 tracing support to ActionCable integration #1640

Merged
merged 5 commits into from
Dec 25, 2021
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
## Unreleased

### Features

- Add Action Cable exception capturing (Rails 6+) [#1638](https://github.com/getsentry/sentry-ruby/pull/1638)
- Add tracing support to `ActionCable` integration [#1640](https://github.com/getsentry/sentry-ruby/pull/1640)

### Bug Fixes

Expand Down
40 changes: 30 additions & 10 deletions sentry-rails/lib/sentry/rails/action_cable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,40 @@ module Sentry
module Rails
module ActionCableExtensions
class ErrorHandler
def self.capture(env, transaction_name:, extra_context: nil, &block)
Sentry.with_scope do |scope|
scope.set_rack_env(env)
scope.set_extras(action_cable: extra_context) if extra_context
scope.set_transaction_name(transaction_name)
class << self
def capture(env, transaction_name:, extra_context: nil, &block)
Sentry.with_scope do |scope|
scope.set_rack_env(env)
scope.set_context("action_cable", extra_context) if extra_context
scope.set_transaction_name(transaction_name)
transaction = start_transaction(env, scope.transaction_name)
scope.set_span(transaction) if transaction

begin
block.call
rescue Exception => e # rubocop:disable Lint/RescueException
Sentry::Rails.capture_exception(e)
begin
block.call
finish_transaction(transaction, 200)
sl0thentr0py marked this conversation as resolved.
Show resolved Hide resolved
rescue Exception => e # rubocop:disable Lint/RescueException
Sentry::Rails.capture_exception(e)
finish_transaction(transaction, 500)

raise
raise
end
end
end

def start_transaction(env, transaction_name)
sentry_trace = env["HTTP_SENTRY_TRACE"]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not possible to define HTTP headers on WebSocket connections from Javascript to specify the trace ID. And the Sentry JS library only seems to append the trace id to XHR's. So I'm not sure how to actually use this feature?

options = { name: transaction_name, op: "rails.action_cable".freeze }
transaction = Sentry::Transaction.from_sentry_trace(sentry_trace, **options) if sentry_trace
Sentry.start_transaction(transaction: transaction, **options)
end

def finish_transaction(transaction, status_code)
return unless transaction

transaction.set_http_status(status_code)
transaction.finish
end
end
end

Expand Down
202 changes: 161 additions & 41 deletions sentry-rails/spec/sentry/rails/action_cable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
require "spec_helper"
require "action_cable/engine"

::ActionCable.server.config.cable = { "adapter" => "test" }

# ensure we can access `connection.env` in tests like we can in production
ActiveSupport.on_load :action_cable_channel_test_case do
class ::ActionCable::Channel::ConnectionStub
Expand Down Expand Up @@ -30,73 +32,191 @@ def unsubscribed
RSpec.describe "Sentry::Rails::ActionCableExtensions", type: :channel do
let(:transport) { Sentry.get_current_client.transport }

before(:all) do
make_basic_app
::ActionCable.server.config.cable = { "adapter" => "test" }
end

after do
transport.events = []
end

describe ChatChannel do
it "captures errors during the subscribe" do
expect { subscribe room_id: 42 }.to raise_error('foo')
expect(transport.events.count).to eq(1)
context "without tracing" do
before do
make_basic_app
end

describe ChatChannel do
it "captures errors during the subscribe" do
expect { subscribe room_id: 42 }.to raise_error('foo')
expect(transport.events.count).to eq(1)

event = transport.events.last.to_json_compatible
event = transport.events.last.to_json_compatible
expect(event["transaction"]).to eq("ChatChannel#subscribed")
expect(event["contexts"]).to include("action_cable" => { "params" => { "room_id" => 42 } })
end
end

expect(event).to include(
"transaction" => "ChatChannel#subscribed",
"extra" => {
describe AppearanceChannel do
before { subscribe room_id: 42 }

it "captures errors during the action" do
expect { perform :appear, foo: 'bar' }.to raise_error('foo')
expect(transport.events.count).to eq(1)

event = transport.events.last.to_json_compatible

expect(event["transaction"]).to eq("AppearanceChannel#appear")
expect(event["contexts"]).to include(
"action_cable" => {
"params" => { "room_id" => 42 }
"params" => { "room_id" => 42 },
"data" => { "action" => "appear", "foo" => "bar" }
}
}
)
)
end

it "captures errors during unsubscribe" do
expect { unsubscribe }.to raise_error('foo')
expect(transport.events.count).to eq(1)

event = transport.events.last.to_json_compatible

expect(Sentry.get_current_scope.extra).to eq({})
expect(event["transaction"]).to eq("AppearanceChannel#unsubscribed")
expect(event["contexts"]).to include(
"action_cable" => {
"params" => { "room_id" => 42 }
}
)
end
end
end

describe AppearanceChannel do
before { subscribe room_id: 42 }
context "with tracing enabled" do
before do
make_basic_app do |config|
config.traces_sample_rate = 1.0
end
end

describe ChatChannel do
it "captures errors and transactions during the subscribe" do
expect { subscribe room_id: 42 }.to raise_error('foo')
expect(transport.events.count).to eq(2)

event = transport.events.first.to_json_compatible

it "captures errors during the action" do
expect { perform :appear, foo: 'bar' }.to raise_error('foo')
expect(transport.events.count).to eq(1)
expect(event["transaction"]).to eq("ChatChannel#subscribed")
expect(event["contexts"]).to include("action_cable" => { "params" => { "room_id" => 42 } })

event = transport.events.last.to_json_compatible
transaction = transport.events.last.to_json_compatible

expect(event).to include(
"transaction" => "AppearanceChannel#appear",
"extra" => {
expect(transaction["type"]).to eq("transaction")
expect(transaction["transaction"]).to eq("ChatChannel#subscribed")
expect(transaction["contexts"]).to include(
"action_cable" => {
"params" => { "room_id" => 42 }
}
)
expect(transaction["contexts"]).to include(
"trace" => hash_including(
"op" => "rails.action_cable",
"status" => "internal_error"
)
)
end
end

describe AppearanceChannel do
before { subscribe room_id: 42 }

it "captures errors and transactions during the action" do
expect { perform :appear, foo: 'bar' }.to raise_error('foo')
expect(transport.events.count).to eq(3)

subscription_transaction = transport.events[0].to_json_compatible

expect(subscription_transaction["type"]).to eq("transaction")
expect(subscription_transaction["transaction"]).to eq("AppearanceChannel#subscribed")
expect(subscription_transaction["contexts"]).to include(
"action_cable" => {
"params" => { "room_id" => 42 }
}
)
expect(subscription_transaction["contexts"]).to include(
"trace" => hash_including(
"op" => "rails.action_cable",
"status" => "ok"
)
)

event = transport.events[1].to_json_compatible

expect(event["transaction"]).to eq("AppearanceChannel#appear")
expect(event["contexts"]).to include(
"action_cable" => {
"params" => { "room_id" => 42 },
"data" => { "action" => "appear", "foo" => "bar" }
}
}
)
)

expect(Sentry.get_current_scope.extra).to eq({})
end
action_transaction = transport.events[2].to_json_compatible

it "captures errors during unsubscribe" do
expect { unsubscribe }.to raise_error('foo')
expect(transport.events.count).to eq(1)
expect(action_transaction["type"]).to eq("transaction")
expect(action_transaction["transaction"]).to eq("AppearanceChannel#appear")
expect(action_transaction["contexts"]).to include(
"action_cable" => {
"params" => { "room_id" => 42 },
"data" => { "action" => "appear", "foo" => "bar" }
}
)
expect(action_transaction["contexts"]).to include(
"trace" => hash_including(
"op" => "rails.action_cable",
"status" => "internal_error"
)
)
end

it "captures errors during unsubscribe" do
expect { unsubscribe }.to raise_error('foo')
expect(transport.events.count).to eq(3)

subscription_transaction = transport.events[0].to_json_compatible

expect(subscription_transaction["type"]).to eq("transaction")
expect(subscription_transaction["transaction"]).to eq("AppearanceChannel#subscribed")
expect(subscription_transaction["contexts"]).to include(
"action_cable" => {
"params" => { "room_id" => 42 }
}
)
expect(subscription_transaction["contexts"]).to include(
"trace" => hash_including(
"op" => "rails.action_cable",
"status" => "ok"
)
)

event = transport.events[1].to_json_compatible

expect(event["transaction"]).to eq("AppearanceChannel#unsubscribed")
expect(event["contexts"]).to include(
"action_cable" => {
"params" => { "room_id" => 42 }
}
)

event = transport.events.last.to_json_compatible
transaction = transport.events[2].to_json_compatible

expect(event).to include(
"transaction" => "AppearanceChannel#unsubscribed",
"extra" => {
expect(transaction["type"]).to eq("transaction")
expect(transaction["transaction"]).to eq("AppearanceChannel#unsubscribed")
expect(transaction["contexts"]).to include(
"action_cable" => {
"params" => { "room_id" => 42 }
}
}
)

expect(Sentry.get_current_scope.extra).to eq({})
)
expect(transaction["contexts"]).to include(
"trace" => hash_including(
"op" => "rails.action_cable",
"status" => "internal_error"
)
)
end
end
end
end
Expand Down