Skip to content

Commit

Permalink
Specs
Browse files Browse the repository at this point in the history
  • Loading branch information
sl0thentr0py committed Aug 30, 2023
1 parent 96fe215 commit fd07c39
Show file tree
Hide file tree
Showing 5 changed files with 183 additions and 8 deletions.
4 changes: 2 additions & 2 deletions sentry-ruby/spec/sentry/net/http_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
75 changes: 74 additions & 1 deletion sentry-ruby/spec/sentry/propagation_context_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,83 @@
let(:subject) { described_class.new(scope) }

describe "#initialize" do
it "generates correct attributes" do
it "generates correct attributes without env" do
expect(subject.trace_id.length).to eq(32)
expect(subject.span_id.length).to eq(16)
expect(subject.parent_span_id).to be_nil
expect(subject.parent_sampled).to be_nil
expect(subject.baggage).to be_nil
expect(subject.incoming_trace).to eq(false)
end

it "generates correct attributes when incoming sentry-trace and baggage" do
env = {
"sentry-trace" => "771a43a4192642f0b136d5159a501700-7c51afd529da4a2a",
"baggage" => "other-vendor-value-1=foo;bar;baz, "\
"sentry-trace_id=771a43a4192642f0b136d5159a501700, "\
"sentry-public_key=49d0f7386ad645858ae85020e393bef3, "\
"sentry-sample_rate=0.01337, "\
"sentry-user_id=Am%C3%A9lie, "\
"other-vendor-value-2=foo;bar;"
}

subject = described_class.new(scope, env)
expect(subject.trace_id).to eq("771a43a4192642f0b136d5159a501700")
expect(subject.span_id.length).to eq(16)
expect(subject.parent_span_id).to eq("7c51afd529da4a2a")
expect(subject.parent_sampled).to eq(nil)
expect(subject.incoming_trace).to eq(true)
expect(subject.baggage).to be_a(Sentry::Baggage)
expect(subject.baggage.mutable).to eq(false)
expect(subject.baggage.items).to eq({
"public_key"=>"49d0f7386ad645858ae85020e393bef3",
"sample_rate"=>"0.01337",
"trace_id"=>"771a43a4192642f0b136d5159a501700",
"user_id"=>"Amélie"
})
end

it "generates correct attributes when incoming HTTP_SENTRY_TRACE and HTTP_BAGGAGE" do
env = {
"HTTP_SENTRY_TRACE" => "771a43a4192642f0b136d5159a501700-7c51afd529da4a2a",
"HTTP_BAGGAGE" => "other-vendor-value-1=foo;bar;baz, "\
"sentry-trace_id=771a43a4192642f0b136d5159a501700, "\
"sentry-public_key=49d0f7386ad645858ae85020e393bef3, "\
"sentry-sample_rate=0.01337, "\
"sentry-user_id=Am%C3%A9lie, "\
"other-vendor-value-2=foo;bar;"
}

subject = described_class.new(scope, env)
expect(subject.trace_id).to eq("771a43a4192642f0b136d5159a501700")
expect(subject.span_id.length).to eq(16)
expect(subject.parent_span_id).to eq("7c51afd529da4a2a")
expect(subject.parent_sampled).to eq(nil)
expect(subject.incoming_trace).to eq(true)
expect(subject.baggage).to be_a(Sentry::Baggage)
expect(subject.baggage.mutable).to eq(false)
expect(subject.baggage.items).to eq({
"public_key"=>"49d0f7386ad645858ae85020e393bef3",
"sample_rate"=>"0.01337",
"trace_id"=>"771a43a4192642f0b136d5159a501700",
"user_id"=>"Amélie"
})
end

it "generates correct attributes when incoming sentry-trace only (from older SDKs)" do
env = {
"sentry-trace" => "771a43a4192642f0b136d5159a501700-7c51afd529da4a2a"
}

subject = described_class.new(scope, env)
expect(subject.trace_id).to eq("771a43a4192642f0b136d5159a501700")
expect(subject.span_id.length).to eq(16)
expect(subject.parent_span_id).to eq("7c51afd529da4a2a")
expect(subject.parent_sampled).to eq(nil)
expect(subject.incoming_trace).to eq(true)
expect(subject.baggage).to be_a(Sentry::Baggage)
expect(subject.baggage.mutable).to eq(false)
expect(subject.baggage.items).to eq({})
end
end

Expand Down
26 changes: 26 additions & 0 deletions sentry-ruby/spec/sentry/rack/capture_exceptions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions sentry-ruby/spec/sentry/scope_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
73 changes: 68 additions & 5 deletions sentry-ruby/spec/sentry_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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/" }

Expand Down

0 comments on commit fd07c39

Please sign in to comment.