|
| 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 | + expect(context1.sample_rand).not_to eq(context2.sample_rand) |
| 25 | + |
| 26 | + trace_id = context1.trace_id |
| 27 | + allow(Sentry::Utils).to receive(:uuid).and_return(trace_id) |
| 28 | + context3 = described_class.new(scope) |
| 29 | + |
| 30 | + expect(context3.sample_rand).to eq(context1.sample_rand) |
| 31 | + end |
| 32 | + |
| 33 | + context "with incoming trace" do |
| 34 | + let(:env) do |
| 35 | + { |
| 36 | + "HTTP_SENTRY_TRACE" => "771a43a4192642f0b136d5159a501700-7c51afd529da4a2a-1", |
| 37 | + "HTTP_BAGGAGE" => "sentry-trace_id=771a43a4192642f0b136d5159a501700,sentry-sample_rand=0.123456" |
| 38 | + } |
| 39 | + end |
| 40 | + |
| 41 | + it "uses sample_rand from incoming baggage" do |
| 42 | + context = described_class.new(scope, env) |
| 43 | + |
| 44 | + expect(context.sample_rand).to eq(0.123456) |
| 45 | + expect(context.incoming_trace).to be true |
| 46 | + end |
| 47 | + end |
| 48 | + |
| 49 | + context "with incoming trace but no sample_rand in baggage" do |
| 50 | + let(:env) do |
| 51 | + { |
| 52 | + "HTTP_SENTRY_TRACE" => "771a43a4192642f0b136d5159a501700-7c51afd529da4a2a-1", |
| 53 | + "HTTP_BAGGAGE" => "sentry-trace_id=771a43a4192642f0b136d5159a501700,sentry-sample_rate=0.5" |
| 54 | + } |
| 55 | + end |
| 56 | + |
| 57 | + it "generates sample_rand based on sampling decision" do |
| 58 | + context = described_class.new(scope, env) |
| 59 | + |
| 60 | + expect(context.sample_rand).to be_a(Float) |
| 61 | + expect(context.sample_rand).to be >= 0.0 |
| 62 | + expect(context.sample_rand).to be < 1.0 |
| 63 | + expect(context.incoming_trace).to be true |
| 64 | + |
| 65 | + expect(context.sample_rand).to be < 0.5 |
| 66 | + end |
| 67 | + |
| 68 | + it "is deterministic for same trace" do |
| 69 | + context1 = described_class.new(scope, env) |
| 70 | + context2 = described_class.new(scope, env) |
| 71 | + |
| 72 | + expect(context1.sample_rand).to eq(context2.sample_rand) |
| 73 | + end |
| 74 | + end |
| 75 | + |
| 76 | + context "with incoming trace and parent_sampled=false" do |
| 77 | + let(:env) do |
| 78 | + { |
| 79 | + "HTTP_SENTRY_TRACE" => "771a43a4192642f0b136d5159a501700-7c51afd529da4a2a-0", |
| 80 | + "HTTP_BAGGAGE" => "sentry-trace_id=771a43a4192642f0b136d5159a501700,sentry-sample_rate=0.5" |
| 81 | + } |
| 82 | + end |
| 83 | + |
| 84 | + it "generates sample_rand based on unsampled decision" do |
| 85 | + context = described_class.new(scope, env) |
| 86 | + |
| 87 | + expect(context.sample_rand).to be_a(Float) |
| 88 | + expect(context.sample_rand).to be >= 0.0 |
| 89 | + expect(context.sample_rand).to be < 1.0 |
| 90 | + expect(context.incoming_trace).to be true |
| 91 | + expect(context.parent_sampled).to be false |
| 92 | + |
| 93 | + expect(context.sample_rand).to be >= 0.5 |
| 94 | + end |
| 95 | + |
| 96 | + it "is deterministic for same trace" do |
| 97 | + context1 = described_class.new(scope, env) |
| 98 | + context2 = described_class.new(scope, env) |
| 99 | + |
| 100 | + expect(context1.sample_rand).to eq(context2.sample_rand) |
| 101 | + end |
| 102 | + |
| 103 | + it "uses parent's explicit unsampled decision instead of falling back to trace_id generation" do |
| 104 | + context = described_class.new(scope, env) |
| 105 | + |
| 106 | + expected_from_decision = Sentry::Utils::SampleRand.generate_from_sampling_decision(false, 0.5, "771a43a4192642f0b136d5159a501700") |
| 107 | + expected_from_trace_id = Sentry::Utils::SampleRand.generate_from_trace_id("771a43a4192642f0b136d5159a501700") |
| 108 | + |
| 109 | + expect(context.sample_rand).to eq(expected_from_decision) |
| 110 | + expect(context.sample_rand).not_to eq(expected_from_trace_id) |
| 111 | + end |
| 112 | + end |
| 113 | + |
| 114 | + context "with incoming trace but no baggage" do |
| 115 | + let(:env) do |
| 116 | + { |
| 117 | + "HTTP_SENTRY_TRACE" => "771a43a4192642f0b136d5159a501700-7c51afd529da4a2a-1" |
| 118 | + } |
| 119 | + end |
| 120 | + |
| 121 | + it "generates deterministic sample_rand from trace_id" do |
| 122 | + context = described_class.new(scope, env) |
| 123 | + |
| 124 | + expect(context.sample_rand).to be_a(Float) |
| 125 | + expect(context.sample_rand).to be >= 0.0 |
| 126 | + expect(context.sample_rand).to be < 1.0 |
| 127 | + expect(context.incoming_trace).to be true |
| 128 | + |
| 129 | + expected = Sentry::Utils::SampleRand.generate_from_trace_id("771a43a4192642f0b136d5159a501700") |
| 130 | + expect(context.sample_rand).to eq(expected) |
| 131 | + end |
| 132 | + end |
| 133 | + end |
| 134 | + |
| 135 | + describe "#get_baggage" do |
| 136 | + it "includes sample_rand in baggage" do |
| 137 | + context = described_class.new(scope) |
| 138 | + baggage = context.get_baggage |
| 139 | + |
| 140 | + expect(baggage.items["sample_rand"]).to eq(Sentry::Utils::SampleRand.format(context.sample_rand)) |
| 141 | + end |
| 142 | + |
| 143 | + context "with incoming baggage containing sample_rand" do |
| 144 | + let(:env) do |
| 145 | + { |
| 146 | + "HTTP_SENTRY_TRACE" => "771a43a4192642f0b136d5159a501700-7c51afd529da4a2a-1", |
| 147 | + "HTTP_BAGGAGE" => "sentry-trace_id=771a43a4192642f0b136d5159a501700,sentry-sample_rand=0.654321" |
| 148 | + } |
| 149 | + end |
| 150 | + |
| 151 | + it "preserves incoming sample_rand in baggage" do |
| 152 | + context = described_class.new(scope, env) |
| 153 | + baggage = context.get_baggage |
| 154 | + |
| 155 | + expect(baggage.items["sample_rand"]).to eq("0.654321") |
| 156 | + end |
| 157 | + end |
| 158 | + end |
| 159 | + end |
| 160 | +end |
0 commit comments