|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +RSpec.describe "Transactions and sample rand propagation" do |
| 4 | + before do |
| 5 | + perform_basic_setup do |config| |
| 6 | + config.traces_sample_rate = 1.0 |
| 7 | + end |
| 8 | + end |
| 9 | + |
| 10 | + describe "sample_rand propagation" do |
| 11 | + it "uses value from the baggage" do |
| 12 | + env = { |
| 13 | + "HTTP_SENTRY_TRACE" => "771a43a4192642f0b136d5159a501700-7c51afd529da4a2a-1", |
| 14 | + "HTTP_BAGGAGE" => "sentry-trace_id=771a43a4192642f0b136d5159a501700,sentry-sample_rand=0.123456" |
| 15 | + } |
| 16 | + |
| 17 | + transaction = Sentry.continue_trace(env, name: "backend_request", op: "http.server") |
| 18 | + propagation_context = Sentry.get_current_scope.propagation_context |
| 19 | + |
| 20 | + expect(propagation_context.sample_rand).to eq(0.123456) |
| 21 | + expect(transaction.sample_rand).to eq(0.123456) |
| 22 | + |
| 23 | + expect(transaction.sample_rand).to eq(propagation_context.sample_rand) |
| 24 | + end |
| 25 | + |
| 26 | + it "generates deterministic value from trace id if there's no value in the baggage" do |
| 27 | + env = { |
| 28 | + "HTTP_SENTRY_TRACE" => "771a43a4192642f0b136d5159a501700-7c51afd529da4a2a-1", |
| 29 | + "HTTP_BAGGAGE" => "sentry-trace_id=771a43a4192642f0b136d5159a501700" |
| 30 | + } |
| 31 | + |
| 32 | + transaction = Sentry.continue_trace(env, name: "backend_request", op: "http.server") |
| 33 | + propagation_context = Sentry.get_current_scope.propagation_context |
| 34 | + |
| 35 | + expect(transaction.sample_rand).to eq(propagation_context.sample_rand) |
| 36 | + |
| 37 | + expected = Sentry::Utils::SampleRand.generate_from_trace_id("771a43a4192642f0b136d5159a501700") |
| 38 | + |
| 39 | + expect(transaction.sample_rand).to eq(expected) |
| 40 | + expect(propagation_context.sample_rand).to eq(expected) |
| 41 | + end |
| 42 | + |
| 43 | + it "properly handles sampling decisions" do |
| 44 | + test_cases = [ |
| 45 | + { sample_rand: 0.1, sample_rate: 0.5, should_sample: true }, |
| 46 | + { sample_rand: 0.7, sample_rate: 0.5, should_sample: false }, |
| 47 | + { sample_rand: 0.5, sample_rate: 0.5, should_sample: false }, |
| 48 | + { sample_rand: 0.499999, sample_rate: 0.5, should_sample: true } |
| 49 | + ] |
| 50 | + |
| 51 | + test_cases.each do |test_case| |
| 52 | + Sentry.configuration.traces_sample_rate = test_case[:sample_rate] |
| 53 | + |
| 54 | + env = { |
| 55 | + "HTTP_SENTRY_TRACE" => "771a43a4192642f0b136d5159a501700-7c51afd529da4a2a-", |
| 56 | + "HTTP_BAGGAGE" => "sentry-trace_id=771a43a4192642f0b136d5159a501700,sentry-sample_rand=#{test_case[:sample_rand]}" |
| 57 | + } |
| 58 | + |
| 59 | + transaction = Sentry.continue_trace(env, name: "test") |
| 60 | + Sentry.start_transaction(transaction: transaction) |
| 61 | + |
| 62 | + expect(transaction.sampled).to eq(test_case[:should_sample]) |
| 63 | + end |
| 64 | + end |
| 65 | + |
| 66 | + it "ensures baggage propagation includes correct sample_rand" do |
| 67 | + env = { |
| 68 | + "HTTP_SENTRY_TRACE" => "771a43a4192642f0b136d5159a501700-7c51afd529da4a2a-1", |
| 69 | + "HTTP_BAGGAGE" => "sentry-trace_id=771a43a4192642f0b136d5159a501700,sentry-sample_rand=0.654321" |
| 70 | + } |
| 71 | + |
| 72 | + transaction = Sentry.continue_trace(env, name: "backend_request") |
| 73 | + baggage = transaction.get_baggage |
| 74 | + |
| 75 | + expect(baggage.items["sample_rand"]).to eq("0.654321") |
| 76 | + |
| 77 | + Sentry.get_current_scope.set_span(transaction) |
| 78 | + |
| 79 | + headers = Sentry.get_trace_propagation_headers |
| 80 | + |
| 81 | + expect(headers["baggage"]).to include("sentry-sample_rand=0.654321") |
| 82 | + end |
| 83 | + |
| 84 | + it "handles edge cases and invalid sample_rand values" do |
| 85 | + env = { |
| 86 | + "HTTP_SENTRY_TRACE" => "771a43a4192642f0b136d5159a501700-7c51afd529da4a2a-1", |
| 87 | + "HTTP_BAGGAGE" => "sentry-trace_id=771a43a4192642f0b136d5159a501700,sentry-sample_rand=1.5" |
| 88 | + } |
| 89 | + |
| 90 | + transaction = Sentry.continue_trace(env, name: "test") |
| 91 | + propagation_context = Sentry.get_current_scope.propagation_context |
| 92 | + |
| 93 | + expected = Sentry::Utils::SampleRand.generate_from_trace_id("771a43a4192642f0b136d5159a501700") |
| 94 | + |
| 95 | + expect(transaction.sample_rand).to eq(expected) |
| 96 | + expect(propagation_context.sample_rand).to eq(expected) |
| 97 | + end |
| 98 | + |
| 99 | + it "works correctly with multiple sequential requests" do |
| 100 | + requests = [ |
| 101 | + { sample_rand: 0.111111, trace_id: "11111111111111111111111111111111" }, |
| 102 | + { sample_rand: 0.222222, trace_id: "22222222222222222222222222222222" }, |
| 103 | + { sample_rand: 0.333333, trace_id: "33333333333333333333333333333333" } |
| 104 | + ] |
| 105 | + |
| 106 | + requests.each do |request| |
| 107 | + env = { |
| 108 | + "HTTP_SENTRY_TRACE" => "#{request[:trace_id]}-7c51afd529da4a2a-1", |
| 109 | + "HTTP_BAGGAGE" => "sentry-trace_id=#{request[:trace_id]},sentry-sample_rand=#{request[:sample_rand]}" |
| 110 | + } |
| 111 | + |
| 112 | + transaction = Sentry.continue_trace(env, name: "test") |
| 113 | + |
| 114 | + expect(transaction.sample_rand).to eq(request[:sample_rand]) |
| 115 | + expect(transaction.trace_id).to eq(request[:trace_id]) |
| 116 | + end |
| 117 | + end |
| 118 | + end |
| 119 | +end |
0 commit comments