From e23bd3a3793419959265e1d661f05caae7d9f2b0 Mon Sep 17 00:00:00 2001 From: Neel Shah Date: Wed, 30 Aug 2023 13:47:57 +0200 Subject: [PATCH] Specs --- sentry-ruby/spec/sentry/net/http_spec.rb | 4 +- .../sentry/rack/capture_exceptions_spec.rb | 26 +++++++ sentry-ruby/spec/sentry/scope_spec.rb | 13 ++++ sentry-ruby/spec/sentry_spec.rb | 73 +++++++++++++++++-- 4 files changed, 109 insertions(+), 7 deletions(-) diff --git a/sentry-ruby/spec/sentry/net/http_spec.rb b/sentry-ruby/spec/sentry/net/http_spec.rb index 1e96f7feb..49c628a22 100644 --- a/sentry-ruby/spec/sentry/net/http_spec.rb +++ b/sentry-ruby/spec/sentry/net/http_spec.rb @@ -138,7 +138,7 @@ "sentry-user_id=Am%C3%A9lie, "\ "other-vendor-value-2=foo;bar;" - transaction = Sentry::Transaction.from_sentry_trace(sentry_trace, baggage: baggage) + transaction = Sentry.continue_trace({ "sentry-trace" => sentry_trace, "baggage" => baggage }) Sentry.get_current_scope.set_span(transaction) response = http.request(request) @@ -190,7 +190,7 @@ "sentry-user_id=Am%C3%A9lie, "\ "other-vendor-value-2=foo;bar;" - transaction = Sentry::Transaction.from_sentry_trace(sentry_trace, baggage: baggage) + transaction = Sentry.continue_trace({ "sentry-trace" => sentry_trace, "baggage" => baggage }) Sentry.get_current_scope.set_span(transaction) response = http.request(request) diff --git a/sentry-ruby/spec/sentry/rack/capture_exceptions_spec.rb b/sentry-ruby/spec/sentry/rack/capture_exceptions_spec.rb index 7429b2d0d..22766342c 100644 --- a/sentry-ruby/spec/sentry/rack/capture_exceptions_spec.rb +++ b/sentry-ruby/spec/sentry/rack/capture_exceptions_spec.rb @@ -565,6 +565,32 @@ def will_be_sampled_by_sdk end end + describe "tracing without performance" do + let(:incoming_prop_context) { Sentry::PropagationContext.new(Sentry::Scope.new) } + let(:env) do + { + "HTTP_SENTRY_TRACE" => incoming_prop_context.get_traceparent, + "HTTP_BAGGAGE" => incoming_prop_context.get_baggage.serialize + } + end + + let(:stack) do + app = ->(_e) { raise exception } + described_class.new(app) + end + + it "captures exception with correct DSC and trace context" do + expect { stack.call(env) }.to raise_error(ZeroDivisionError) + + trace_context = last_sentry_event.contexts[:trace] + expect(trace_context[:trace_id]).to eq(incoming_prop_context.trace_id) + expect(trace_context[:parent_span_id]).to eq(incoming_prop_context.span_id) + expect(trace_context[:span_id].length).to eq(16) + + expect(last_sentry_event.dynamic_sampling_context).to eq(incoming_prop_context.get_dynamic_sampling_context) + end + end + describe "session capturing" do context "when auto_session_tracking is false" do before do diff --git a/sentry-ruby/spec/sentry/scope_spec.rb b/sentry-ruby/spec/sentry/scope_spec.rb index 3573e3a6e..95f1a396a 100644 --- a/sentry-ruby/spec/sentry/scope_spec.rb +++ b/sentry-ruby/spec/sentry/scope_spec.rb @@ -311,4 +311,17 @@ end end end + + describe "#generate_propagation_context" do + it "initializes new propagation context without env" do + expect(Sentry::PropagationContext).to receive(:new).with(subject, nil) + subject.generate_propagation_context + end + + it "initializes new propagation context without env" do + env = { foo: 42 } + expect(Sentry::PropagationContext).to receive(:new).with(subject, env) + subject.generate_propagation_context(env) + end + end end diff --git a/sentry-ruby/spec/sentry_spec.rb b/sentry-ruby/spec/sentry_spec.rb index beb931131..80c74180f 100644 --- a/sentry-ruby/spec/sentry_spec.rb +++ b/sentry-ruby/spec/sentry_spec.rb @@ -331,18 +331,18 @@ unsampled_trace = "d298e6b033f84659928a2267c3879aaa-2a35b8e9a1b974f4-0" not_sampled_trace = "d298e6b033f84659928a2267c3879aaa-2a35b8e9a1b974f4-" - transaction = Sentry::Transaction.from_sentry_trace(sampled_trace, op: "rack.request", name: "/payment") + transaction = Sentry.continue_trace({ "sentry-trace" => sampled_trace }, op: "rack.request", name: "/payment") described_class.start_transaction(transaction: transaction) expect(transaction.sampled).to eq(true) - transaction = Sentry::Transaction.from_sentry_trace(unsampled_trace, op: "rack.request", name: "/payment") + transaction = Sentry.continue_trace({ "sentry-trace" => unsampled_trace }, op: "rack.request", name: "/payment") described_class.start_transaction(transaction: transaction) expect(transaction.sampled).to eq(false) allow(Random).to receive(:rand).and_return(0.4) - transaction = Sentry::Transaction.from_sentry_trace(not_sampled_trace, op: "rack.request", name: "/payment") + transaction = Sentry.continue_trace({ "sentry-trace" => not_sampled_trace }, op: "rack.request", name: "/payment") described_class.start_transaction(transaction: transaction) expect(transaction.sampled).to eq(true) @@ -672,7 +672,7 @@ traceparent = described_class.get_traceparent propagation_context = described_class.get_current_scope.propagation_context - expect(traceparent).to match(Sentry::Transaction::SENTRY_TRACE_REGEXP) + expect(traceparent).to match(Sentry::PropagationContext::SENTRY_TRACE_REGEXP) expect(traceparent).to eq("#{propagation_context.trace_id}-#{propagation_context.span_id}") end @@ -683,7 +683,7 @@ traceparent = described_class.get_traceparent - expect(traceparent).to match(Sentry::Transaction::SENTRY_TRACE_REGEXP) + expect(traceparent).to match(Sentry::PropagationContext::SENTRY_TRACE_REGEXP) expect(traceparent).to eq("#{span.trace_id}-#{span.span_id}-1") end end @@ -716,6 +716,69 @@ end end + describe ".continue_trace" do + + context "without incoming sentry trace" do + let(:env) { { "HTTP_FOO" => "bar" } } + + it "returns nil with tracing disabled" do + expect(described_class.continue_trace(env)).to eq(nil) + end + + it "returns nil with tracing enabled" do + Sentry.configuration.traces_sample_rate = 1.0 + expect(described_class.continue_trace(env)).to eq(nil) + end + + it "sets new propagation context on scope" do + expect(Sentry.get_current_scope).to receive(:generate_propagation_context).and_call_original + described_class.continue_trace(env) + + propagation_context = Sentry.get_current_scope.propagation_context + expect(propagation_context.incoming_trace).to eq(false) + end + end + + context "with incoming sentry trace" do + let(:incoming_prop_context) { Sentry::PropagationContext.new(Sentry::Scope.new) } + let(:env) do + { + "HTTP_SENTRY_TRACE" => incoming_prop_context.get_traceparent, + "HTTP_BAGGAGE" => incoming_prop_context.get_baggage.serialize + } + end + + it "returns nil with tracing disabled" do + expect(described_class.continue_trace(env)).to eq(nil) + end + + it "sets new propagation context from env on scope" do + expect(Sentry.get_current_scope).to receive(:generate_propagation_context).and_call_original + described_class.continue_trace(env) + + propagation_context = Sentry.get_current_scope.propagation_context + expect(propagation_context.incoming_trace).to eq(true) + expect(propagation_context.trace_id).to eq(incoming_prop_context.trace_id) + expect(propagation_context.parent_span_id).to eq(incoming_prop_context.span_id) + expect(propagation_context.parent_sampled).to eq(nil) + expect(propagation_context.baggage.items).to eq(incoming_prop_context.get_baggage.items) + expect(propagation_context.baggage.mutable).to eq(false) + end + + it "returns new Transaction with tracing enabled" do + Sentry.configuration.traces_sample_rate = 1.0 + + transaction = described_class.continue_trace(env, name: "foobar") + expect(transaction).to be_a(Sentry::Transaction) + expect(transaction.name).to eq("foobar") + expect(transaction.trace_id).to eq(incoming_prop_context.trace_id) + expect(transaction.parent_span_id).to eq(incoming_prop_context.span_id) + expect(transaction.baggage.items).to eq(incoming_prop_context.get_baggage.items) + expect(transaction.baggage.mutable).to eq(false) + end + end + end + describe 'release detection' do let(:fake_root) { "/tmp/sentry/" }