From 8d99b54da6baea1a69121dd2bee08dc378df2a84 Mon Sep 17 00:00:00 2001 From: st0012 Date: Wed, 3 Mar 2021 20:04:18 +0800 Subject: [PATCH 1/2] Ignore sentry-trace when tracing is not enabled --- sentry-ruby/lib/sentry/transaction.rb | 1 + .../sentry/rack/capture_exceptions_spec.rb | 32 ++++++++++++--- sentry-ruby/spec/sentry/transaction_spec.rb | 41 ++++++++++++++----- 3 files changed, 58 insertions(+), 16 deletions(-) diff --git a/sentry-ruby/lib/sentry/transaction.rb b/sentry-ruby/lib/sentry/transaction.rb index f0725d19a..f47b29a1b 100644 --- a/sentry-ruby/lib/sentry/transaction.rb +++ b/sentry-ruby/lib/sentry/transaction.rb @@ -26,6 +26,7 @@ def set_span_recorder end def self.from_sentry_trace(sentry_trace, **options) + return unless Sentry.configuration.tracing_enabled? return unless sentry_trace match = SENTRY_TRACE_REGEXP.match(sentry_trace) diff --git a/sentry-ruby/spec/sentry/rack/capture_exceptions_spec.rb b/sentry-ruby/spec/sentry/rack/capture_exceptions_spec.rb index 36c9a5b7d..3a011e0a7 100644 --- a/sentry-ruby/spec/sentry/rack/capture_exceptions_spec.rb +++ b/sentry-ruby/spec/sentry/rack/capture_exceptions_spec.rb @@ -287,16 +287,38 @@ Sentry.configuration.traces_sample_rate = nil end + let(:stack) do + described_class.new( + ->(_) do + [200, {}, ["ok"]] + end + ) + end + it "doesn't record transaction" do - app = ->(_) do - [200, {}, ["ok"]] + stack.call(env) + + expect(transport.events.count).to eq(0) + end + + context "when sentry-trace header is sent" do + let(:external_transaction) do + Sentry::Transaction.new( + op: "pageload", + status: "ok", + sampled: true, + name: "a/path" + ) end - stack = described_class.new(app) + it "doesn't cause the transaction to be recorded" do + env["HTTP_SENTRY_TRACE"] = external_transaction.to_sentry_trace - stack.call(env) + response = stack.call(env) - expect(transport.events.count).to eq(0) + expect(response[0]).to eq(200) + expect(transport.events).to be_empty + end end end end diff --git a/sentry-ruby/spec/sentry/transaction_spec.rb b/sentry-ruby/spec/sentry/transaction_spec.rb index 0a96e8ac3..336d57936 100644 --- a/sentry-ruby/spec/sentry/transaction_spec.rb +++ b/sentry-ruby/spec/sentry/transaction_spec.rb @@ -15,20 +15,39 @@ describe ".from_sentry_trace" do let(:sentry_trace) { subject.to_sentry_trace } - it "returns correctly-formatted value" do - child_transaction = described_class.from_sentry_trace(sentry_trace, op: "child") - - expect(child_transaction.trace_id).to eq(subject.trace_id) - expect(child_transaction.parent_span_id).to eq(subject.span_id) - expect(child_transaction.parent_sampled).to eq(true) - expect(child_transaction.sampled).to eq(true) - expect(child_transaction.op).to eq("child") + before do + configuration = Sentry::Configuration.new + configuration.traces_sample_rate = 1.0 + allow(Sentry).to receive(:configuration).and_return(configuration) end - it "handles invalid values without crashing" do - child_transaction = described_class.from_sentry_trace("dummy", op: "child") + context "when tracing is enabled" do + it "returns correctly-formatted value" do + child_transaction = described_class.from_sentry_trace(sentry_trace, op: "child") + + expect(child_transaction.trace_id).to eq(subject.trace_id) + expect(child_transaction.parent_span_id).to eq(subject.span_id) + expect(child_transaction.parent_sampled).to eq(true) + expect(child_transaction.sampled).to eq(true) + expect(child_transaction.op).to eq("child") + end + + it "handles invalid values without crashing" do + child_transaction = described_class.from_sentry_trace("dummy", op: "child") - expect(child_transaction).to be_nil + expect(child_transaction).to be_nil + end + end + + context "when tracing is disabled" do + before do + configuration = Sentry::Configuration.new + allow(Sentry).to receive(:configuration).and_return(configuration) + end + + it "returns nil" do + expect(described_class.from_sentry_trace(sentry_trace, op: "child")).to be_nil + end end end From 7444bc64a4f1660713243f57a0f04b19232a4eb6 Mon Sep 17 00:00:00 2001 From: st0012 Date: Wed, 3 Mar 2021 22:12:41 +0800 Subject: [PATCH 2/2] Update changelog --- sentry-ruby/CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/sentry-ruby/CHANGELOG.md b/sentry-ruby/CHANGELOG.md index 6c8a75c3d..375d69308 100644 --- a/sentry-ruby/CHANGELOG.md +++ b/sentry-ruby/CHANGELOG.md @@ -3,6 +3,8 @@ ## Unreleased - Refactor interface construction [#1296](https://github.com/getsentry/sentry-ruby/pull/1296) +- Ignore sentry-trace when tracing is not enabled [#1308](https://github.com/getsentry/sentry-ruby/pull/1308) + - Fixes [#1307](https://github.com/getsentry/sentry-ruby/issues/1307) ## 4.2.2