-
-
Notifications
You must be signed in to change notification settings - Fork 501
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support transaction (part 1) (#1108)
* Support Transaction recording * Add AR support to the test app * Support tracing in sentry-rails * Change APIs * Use new transaction API in sentry-rails * Update sentry_trace related APIs
- Loading branch information
Showing
34 changed files
with
971 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
require "sentry/rails/tracing/abstract_subscriber" | ||
require "sentry/rails/tracing/active_record_subscriber" | ||
|
||
module Sentry | ||
module Rails | ||
module Tracing | ||
def self.subscribe_tracing_events | ||
# need to avoid duplicated subscription | ||
return if @subscribed | ||
|
||
Tracing::ActiveRecordSubscriber.subscribe! | ||
|
||
@subscribed = true | ||
end | ||
end | ||
end | ||
end |
35 changes: 35 additions & 0 deletions
35
sentry-rails/lib/sentry/rails/tracing/abstract_subscriber.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
module Sentry | ||
module Rails | ||
module Tracing | ||
class AbstractSubscriber | ||
|
||
class << self | ||
def subscribe! | ||
raise NotImplementedError | ||
end | ||
|
||
def subscribe_to_event(event_name) | ||
if ::Rails.version.to_i == 5 | ||
ActiveSupport::Notifications.subscribe(event_name) do |_, start, finish, _, payload| | ||
next unless get_current_transaction | ||
|
||
duration = finish.to_f - start.to_f | ||
yield(event_name, duration, payload) | ||
end | ||
else | ||
ActiveSupport::Notifications.subscribe(event_name) do |event| | ||
next unless get_current_transaction | ||
|
||
yield(event_name, event.duration, event.payload) | ||
end | ||
end | ||
end | ||
|
||
def get_current_transaction | ||
Sentry.get_current_scope.get_transaction | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
23 changes: 23 additions & 0 deletions
23
sentry-rails/lib/sentry/rails/tracing/active_record_subscriber.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
module Sentry | ||
module Rails | ||
module Tracing | ||
class ActiveRecordSubscriber < AbstractSubscriber | ||
EVENT_NAME = "sql.active_record".freeze | ||
EXCLUDED_EVENTS = ["SCHEMA", "TRANSACTION"].freeze | ||
|
||
def self.subscribe! | ||
subscribe_to_event(EVENT_NAME) do |event_name, duration, payload| | ||
if !EXCLUDED_EVENTS.include? payload[:name] | ||
timestamp = Time.now.utc.to_f | ||
start_timestamp = timestamp - duration.to_f | ||
|
||
new_span = get_current_transaction.start_child(op: event_name, description: payload[:sql], start_timestamp: start_timestamp, timestamp: timestamp) | ||
new_span.set_data(:name, payload[:name]) | ||
new_span.set_data(:connection_id, payload[:connection_id]) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
require "spec_helper" | ||
|
||
RSpec.describe Sentry::Rails::Tracing, type: :request do | ||
let(:transport) do | ||
Sentry.get_current_client.transport | ||
end | ||
|
||
let(:event) do | ||
transport.events.last.to_json_compatible | ||
end | ||
|
||
after do | ||
transport.events = [] | ||
end | ||
|
||
context "with traces_sample_rate set" do | ||
before do | ||
expect(described_class).to receive(:subscribe_tracing_events).and_call_original | ||
|
||
make_basic_app do |config| | ||
config.traces_sample_rate = 1.0 | ||
end | ||
end | ||
|
||
it "records transaction" do | ||
get "/posts" | ||
|
||
expect(transport.events.count).to eq(2) | ||
|
||
event = transport.events.first.to_hash | ||
transaction = transport.events.last.to_hash | ||
|
||
expect(event.dig(:contexts, :trace, :trace_id).length).to eq(32) | ||
expect(event.dig(:contexts, :trace, :trace_id)).to eq(transaction.dig(:contexts, :trace, :trace_id)) | ||
|
||
expect(transaction[:type]).to eq("transaction") | ||
expect(transaction[:spans].count).to eq(2) | ||
|
||
first_span = transaction[:spans][0] | ||
expect(first_span[:op]).to eq("rack.request") | ||
expect(first_span[:status]).to eq("internal_error") | ||
expect(first_span[:data]).to eq({ "status_code" => 500 }) | ||
|
||
second_span = transaction[:spans][1] | ||
expect(second_span[:op]).to eq("sql.active_record") | ||
expect(second_span[:description]).to eq("SELECT \"posts\".* FROM \"posts\"") | ||
expect(second_span[:parent_span_id]).to eq(first_span[:span_id]) | ||
end | ||
end | ||
|
||
context "without traces_sample_rate set" do | ||
before do | ||
expect(described_class).not_to receive(:subscribe_tracing_events) | ||
|
||
make_basic_app | ||
end | ||
|
||
it "doesn't record any transaction" do | ||
get "/posts" | ||
|
||
expect(transport.events.count).to eq(1) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.