Skip to content

Commit

Permalink
Use continue_trace in sentry-rails
Browse files Browse the repository at this point in the history
  • Loading branch information
sl0thentr0py committed Aug 29, 2023
1 parent ed37188 commit 96fe215
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 21 deletions.
5 changes: 1 addition & 4 deletions sentry-rails/lib/sentry/rails/action_cable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,8 @@ def capture(connection, transaction_name:, extra_context: nil, &block)
end

def start_transaction(env, scope)
sentry_trace = env["HTTP_SENTRY_TRACE"]
baggage = env["HTTP_BAGGAGE"]

options = { name: scope.transaction_name, source: scope.transaction_source, op: OP_NAME }
transaction = Sentry::Transaction.from_sentry_trace(sentry_trace, baggage: baggage, **options) if sentry_trace
transaction = Sentry.continue_trace(env, **options)
Sentry.start_transaction(transaction: transaction, **options)
end

Expand Down
5 changes: 1 addition & 4 deletions sentry-rails/lib/sentry/rails/capture_exceptions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,13 @@ def capture_exception(exception, env)
end

def start_transaction(env, scope)
sentry_trace = env["HTTP_SENTRY_TRACE"]
baggage = env["HTTP_BAGGAGE"]

options = { name: scope.transaction_name, source: scope.transaction_source, op: transaction_op }

if @assets_regexp && scope.transaction_name.match?(@assets_regexp)
options.merge!(sampled: false)
end

transaction = Sentry::Transaction.from_sentry_trace(sentry_trace, baggage: baggage, **options) if sentry_trace
transaction = Sentry.continue_trace(env, **options)
Sentry.start_transaction(transaction: transaction, custom_sampling_context: { env: env }, **options)
end

Expand Down
4 changes: 2 additions & 2 deletions sentry-ruby/lib/sentry/propagation_context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ def initialize(scope, env = nil)
@incoming_trace = false

if env
sentry_trace_header = env["HTTP_SENTRY_TRACE"]
baggage_header = env["HTTP_BAGGAGE"]
sentry_trace_header = env["HTTP_SENTRY_TRACE"] || env[SENTRY_TRACE_HEADER_NAME]
baggage_header = env["HTTP_BAGGAGE"] || env[BAGGAGE_HEADER_NAME]

if sentry_trace_header
sentry_trace_data = self.class.extract_sentry_trace(sentry_trace_header)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def call(_worker, job, queue)
scope.set_tags(build_tags(job["tags"]))
scope.set_contexts(sidekiq: job.merge("queue" => queue))
scope.set_transaction_name(context_filter.transaction_name, source: :task)
transaction = start_transaction(scope, job["sentry_trace"])
transaction = start_transaction(scope, job["trace_propagation_headers"])
scope.set_span(transaction) if transaction

begin
Expand All @@ -39,9 +39,9 @@ def build_tags(tags)
Array(tags).each_with_object({}) { |name, tags_hash| tags_hash[:"sidekiq.#{name}"] = true }
end

def start_transaction(scope, sentry_trace)
def start_transaction(scope, env)
options = { name: scope.transaction_name, source: scope.transaction_source, op: OP_NAME }
transaction = Sentry::Transaction.from_sentry_trace(sentry_trace, **options) if sentry_trace
transaction = Sentry.continue_trace(env, **options)
Sentry.start_transaction(transaction: transaction, **options)
end

Expand All @@ -58,9 +58,8 @@ def call(_worker_class, job, _queue, _redis_pool)
return yield unless Sentry.initialized?

user = Sentry.get_current_scope.user
transaction = Sentry.get_current_scope.get_transaction
job["sentry_user"] = user unless user.empty?
job["sentry_trace"] = transaction.to_sentry_trace if transaction
job["trace_propagation_headers"] = Sentry.get_trace_propagation_headers
yield
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,12 @@
expect(event.tags.keys).to include(:"sidekiq.marvel", :"sidekiq.dc")
end

context "with sentry_trace" do
context "with trace_propagation_headers" do
let(:parent_transaction) { Sentry.start_transaction(op: "sidekiq") }

it "starts the transaction from it" do
execute_worker(processor, HappyWorker, sentry_trace: parent_transaction.to_sentry_trace)
trace_propagation_headers = { "sentry-trace" => parent_transaction.to_sentry_trace }
execute_worker(processor, HappyWorker, trace_propagation_headers: trace_propagation_headers)

expect(transport.events.count).to eq(1)
transaction = transport.events.first
Expand Down Expand Up @@ -93,12 +94,11 @@
end
end

it "does not add user or sentry_trace to the job if they're absence in the current scope" do
it "does not add user to the job if they're absent in the current scope" do
client.push('queue' => 'default', 'class' => HappyWorker, 'args' => [])

expect(queue.size).to be(1)
expect(queue.first["sentry_user"]).to be_nil
expect(queue.first["sentry_trace"]).to be_nil
end

describe "with user" do
Expand All @@ -121,11 +121,13 @@
Sentry.get_current_scope.set_span(transaction)
end

it "sets user of the current scope to the job" do
it "sets the correct trace_propagation_headers linked to the transaction" do
client.push('queue' => 'default', 'class' => HappyWorker, 'args' => [])

expect(queue.size).to be(1)
expect(queue.first["sentry_trace"]).to eq(transaction.to_sentry_trace)
headers = queue.first["trace_propagation_headers"]
expect(headers["sentry-trace"]).to eq(transaction.to_sentry_trace)
expect(headers["baggage"]).to eq(transaction.to_baggage)
end
end
end

0 comments on commit 96fe215

Please sign in to comment.