|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +RSpec.describe Sentry::PropagationContext do |
| 4 | + before do |
| 5 | + perform_basic_setup |
| 6 | + end |
| 7 | + |
| 8 | + let(:scope) { Sentry.get_current_scope } |
| 9 | + |
| 10 | + describe "sample_rand integration" do |
| 11 | + describe "#initialize" do |
| 12 | + it "generates sample_rand when no incoming trace" do |
| 13 | + context = described_class.new(scope) |
| 14 | + |
| 15 | + expect(context.sample_rand).to be_a(Float) |
| 16 | + expect(context.sample_rand).to be >= 0.0 |
| 17 | + expect(context.sample_rand).to be < 1.0 |
| 18 | + end |
| 19 | + |
| 20 | + it "generates deterministic sample_rand from trace_id" do |
| 21 | + context1 = described_class.new(scope) |
| 22 | + context2 = described_class.new(scope) |
| 23 | + |
| 24 | + # Different contexts should have different trace_ids and thus different sample_rand |
| 25 | + expect(context1.sample_rand).not_to eq(context2.sample_rand) |
| 26 | + |
| 27 | + # But same trace_id should generate same sample_rand |
| 28 | + trace_id = context1.trace_id |
| 29 | + allow(Sentry::Utils).to receive(:uuid).and_return(trace_id) |
| 30 | + context3 = described_class.new(scope) |
| 31 | + |
| 32 | + expect(context3.sample_rand).to eq(context1.sample_rand) |
| 33 | + end |
| 34 | + |
| 35 | + context "with incoming trace" do |
| 36 | + let(:env) do |
| 37 | + { |
| 38 | + "HTTP_SENTRY_TRACE" => "771a43a4192642f0b136d5159a501700-7c51afd529da4a2a-1", |
| 39 | + "HTTP_BAGGAGE" => "sentry-trace_id=771a43a4192642f0b136d5159a501700,sentry-sample_rand=0.123456" |
| 40 | + } |
| 41 | + end |
| 42 | + |
| 43 | + it "uses sample_rand from incoming baggage" do |
| 44 | + context = described_class.new(scope, env) |
| 45 | + |
| 46 | + expect(context.sample_rand).to eq(0.123456) |
| 47 | + expect(context.incoming_trace).to be true |
| 48 | + end |
| 49 | + end |
| 50 | + |
| 51 | + context "with incoming trace but no sample_rand in baggage" do |
| 52 | + let(:env) do |
| 53 | + { |
| 54 | + "HTTP_SENTRY_TRACE" => "771a43a4192642f0b136d5159a501700-7c51afd529da4a2a-1", |
| 55 | + "HTTP_BAGGAGE" => "sentry-trace_id=771a43a4192642f0b136d5159a501700,sentry-sample_rate=0.5" |
| 56 | + } |
| 57 | + end |
| 58 | + |
| 59 | + it "generates sample_rand based on sampling decision" do |
| 60 | + context = described_class.new(scope, env) |
| 61 | + |
| 62 | + expect(context.sample_rand).to be_a(Float) |
| 63 | + expect(context.sample_rand).to be >= 0.0 |
| 64 | + expect(context.sample_rand).to be < 1.0 |
| 65 | + expect(context.incoming_trace).to be true |
| 66 | + |
| 67 | + # For sampled=true and sample_rate=0.5, sample_rand should be < 0.5 |
| 68 | + expect(context.sample_rand).to be < 0.5 |
| 69 | + end |
| 70 | + |
| 71 | + it "is deterministic for same trace" do |
| 72 | + context1 = described_class.new(scope, env) |
| 73 | + context2 = described_class.new(scope, env) |
| 74 | + |
| 75 | + expect(context1.sample_rand).to eq(context2.sample_rand) |
| 76 | + end |
| 77 | + end |
| 78 | + |
| 79 | + context "with incoming trace but no baggage" do |
| 80 | + let(:env) do |
| 81 | + { |
| 82 | + "HTTP_SENTRY_TRACE" => "771a43a4192642f0b136d5159a501700-7c51afd529da4a2a-1" |
| 83 | + } |
| 84 | + end |
| 85 | + |
| 86 | + it "generates deterministic sample_rand from trace_id" do |
| 87 | + context = described_class.new(scope, env) |
| 88 | + |
| 89 | + expect(context.sample_rand).to be_a(Float) |
| 90 | + expect(context.sample_rand).to be >= 0.0 |
| 91 | + expect(context.sample_rand).to be < 1.0 |
| 92 | + expect(context.incoming_trace).to be true |
| 93 | + |
| 94 | + # Should be deterministic based on trace_id |
| 95 | + expected = Sentry::Utils::SampleRand.generate_from_trace_id("771a43a4192642f0b136d5159a501700") |
| 96 | + expect(context.sample_rand).to eq(expected) |
| 97 | + end |
| 98 | + end |
| 99 | + end |
| 100 | + |
| 101 | + describe "#get_baggage" do |
| 102 | + it "includes sample_rand in baggage" do |
| 103 | + context = described_class.new(scope) |
| 104 | + baggage = context.get_baggage |
| 105 | + |
| 106 | + expect(baggage.items["sample_rand"]).to eq(Sentry::Utils::SampleRand.format(context.sample_rand)) |
| 107 | + end |
| 108 | + |
| 109 | + context "with incoming baggage containing sample_rand" do |
| 110 | + let(:env) do |
| 111 | + { |
| 112 | + "HTTP_SENTRY_TRACE" => "771a43a4192642f0b136d5159a501700-7c51afd529da4a2a-1", |
| 113 | + "HTTP_BAGGAGE" => "sentry-trace_id=771a43a4192642f0b136d5159a501700,sentry-sample_rand=0.654321" |
| 114 | + } |
| 115 | + end |
| 116 | + |
| 117 | + it "preserves incoming sample_rand in baggage" do |
| 118 | + context = described_class.new(scope, env) |
| 119 | + baggage = context.get_baggage |
| 120 | + |
| 121 | + expect(baggage.items["sample_rand"]).to eq("0.654321") |
| 122 | + end |
| 123 | + end |
| 124 | + end |
| 125 | + |
| 126 | + describe "extract_sample_rand_from_baggage" do |
| 127 | + it "extracts valid sample_rand from baggage" do |
| 128 | + baggage = Sentry::Baggage.new({ "sample_rand" => "0.123456" }) |
| 129 | + context = described_class.new(scope) |
| 130 | + |
| 131 | + sample_rand = context.send(:extract_sample_rand_from_baggage, baggage) |
| 132 | + expect(sample_rand).to eq(0.123456) |
| 133 | + end |
| 134 | + |
| 135 | + it "returns nil for invalid sample_rand" do |
| 136 | + baggage = Sentry::Baggage.new({ "sample_rand" => "1.5" }) # > 1.0 is invalid |
| 137 | + context = described_class.new(scope) |
| 138 | + |
| 139 | + sample_rand = context.send(:extract_sample_rand_from_baggage, baggage) |
| 140 | + expect(sample_rand).to be_nil |
| 141 | + end |
| 142 | + |
| 143 | + it "returns nil when no sample_rand in baggage" do |
| 144 | + baggage = Sentry::Baggage.new({ "trace_id" => "abc123" }) |
| 145 | + context = described_class.new(scope) |
| 146 | + |
| 147 | + sample_rand = context.send(:extract_sample_rand_from_baggage, baggage) |
| 148 | + expect(sample_rand).to be_nil |
| 149 | + end |
| 150 | + |
| 151 | + it "returns nil when baggage is nil" do |
| 152 | + context = described_class.new(scope) |
| 153 | + |
| 154 | + sample_rand = context.send(:extract_sample_rand_from_baggage, nil) |
| 155 | + expect(sample_rand).to be_nil |
| 156 | + end |
| 157 | + end |
| 158 | + |
| 159 | + describe "generate_sample_rand" do |
| 160 | + context "with incoming trace and sampling decision" do |
| 161 | + let(:context) do |
| 162 | + ctx = described_class.new(scope) |
| 163 | + ctx.instance_variable_set(:@incoming_trace, true) |
| 164 | + ctx.instance_variable_set(:@parent_sampled, true) |
| 165 | + ctx.instance_variable_set(:@baggage, Sentry::Baggage.new({ "sample_rate" => "0.5" })) |
| 166 | + ctx.instance_variable_set(:@trace_id, "771a43a4192642f0b136d5159a501700") |
| 167 | + ctx |
| 168 | + end |
| 169 | + |
| 170 | + it "generates sample_rand based on sampling decision" do |
| 171 | + sample_rand = context.send(:generate_sample_rand) |
| 172 | + |
| 173 | + expect(sample_rand).to be_a(Float) |
| 174 | + expect(sample_rand).to be >= 0.0 |
| 175 | + expect(sample_rand).to be < 0.5 |
| 176 | + end |
| 177 | + end |
| 178 | + |
| 179 | + context "without incoming trace" do |
| 180 | + let(:context) do |
| 181 | + ctx = described_class.new(scope) |
| 182 | + ctx.instance_variable_set(:@incoming_trace, false) |
| 183 | + ctx.instance_variable_set(:@trace_id, "771a43a4192642f0b136d5159a501700") |
| 184 | + ctx |
| 185 | + end |
| 186 | + |
| 187 | + it "generates deterministic sample_rand from trace_id" do |
| 188 | + sample_rand = context.send(:generate_sample_rand) |
| 189 | + expected = Sentry::Utils::SampleRand.generate_from_trace_id("771a43a4192642f0b136d5159a501700") |
| 190 | + |
| 191 | + expect(sample_rand).to eq(expected) |
| 192 | + end |
| 193 | + end |
| 194 | + end |
| 195 | + end |
| 196 | +end |
0 commit comments