From dfc22a40bde8889b3ec6b3728fee80ec06c36ea2 Mon Sep 17 00:00:00 2001 From: Neel Shah Date: Wed, 16 Aug 2023 13:19:17 +0200 Subject: [PATCH] Use continue_trace in sentry-rails --- CHANGELOG.md | 3 ++- sentry-rails/lib/sentry/rails/action_cable.rb | 5 +---- .../lib/sentry/rails/capture_exceptions.rb | 5 +---- sentry-ruby/lib/sentry/propagation_context.rb | 4 ++-- .../sentry/sidekiq/sentry_context_middleware.rb | 9 ++++----- .../sidekiq/sentry_context_middleware_spec.rb | 14 ++++++++------ 6 files changed, 18 insertions(+), 22 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ccc9825e6..6a5a10329 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,7 +11,8 @@ config.trace_propagation_targets = [/example.com/, 'foobar.org/api/v2'] ``` - Tracing without Performance - - Implement `PropagationContext` on `Scope` and add `Sentry.get_trace_propagation_headers` API [#2084](https://github.com/getsentry/sentry-ruby/pull/2084) + - Implement `PropagationContext` on `Scope` and add new `Sentry.get_trace_propagation_headers` API [#2084](https://github.com/getsentry/sentry-ruby/pull/2084) + - Add new `Sentry.continue_trace` API [#2089](https://github.com/getsentry/sentry-ruby/pull/2089) The SDK now supports connecting arbitrary events (Errors / Transactions / Replays) across distributed services and not just Transactions. To continue an incoming trace starting with this version of the SDK, use `Sentry.continue_trace` as follows. diff --git a/sentry-rails/lib/sentry/rails/action_cable.rb b/sentry-rails/lib/sentry/rails/action_cable.rb index 5bf01f5da..155377466 100644 --- a/sentry-rails/lib/sentry/rails/action_cable.rb +++ b/sentry-rails/lib/sentry/rails/action_cable.rb @@ -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 diff --git a/sentry-rails/lib/sentry/rails/capture_exceptions.rb b/sentry-rails/lib/sentry/rails/capture_exceptions.rb index a145d53dd..8d93784ed 100644 --- a/sentry-rails/lib/sentry/rails/capture_exceptions.rb +++ b/sentry-rails/lib/sentry/rails/capture_exceptions.rb @@ -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 diff --git a/sentry-ruby/lib/sentry/propagation_context.rb b/sentry-ruby/lib/sentry/propagation_context.rb index d6d5a9e42..720a46f6d 100644 --- a/sentry-ruby/lib/sentry/propagation_context.rb +++ b/sentry-ruby/lib/sentry/propagation_context.rb @@ -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) diff --git a/sentry-sidekiq/lib/sentry/sidekiq/sentry_context_middleware.rb b/sentry-sidekiq/lib/sentry/sidekiq/sentry_context_middleware.rb index 646c40e60..fcb6e0ada 100644 --- a/sentry-sidekiq/lib/sentry/sidekiq/sentry_context_middleware.rb +++ b/sentry-sidekiq/lib/sentry/sidekiq/sentry_context_middleware.rb @@ -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 @@ -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 @@ -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 diff --git a/sentry-sidekiq/spec/sentry/sidekiq/sentry_context_middleware_spec.rb b/sentry-sidekiq/spec/sentry/sidekiq/sentry_context_middleware_spec.rb index c07f18050..3a2b48de0 100644 --- a/sentry-sidekiq/spec/sentry/sidekiq/sentry_context_middleware_spec.rb +++ b/sentry-sidekiq/spec/sentry/sidekiq/sentry_context_middleware_spec.rb @@ -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 @@ -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 @@ -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