|
16 | 16 | error_events = logged_events[:events].select { |event| event["exception"] }
|
17 | 17 | expect(error_events).not_to be_empty
|
18 | 18 |
|
19 |
| - error_event = error_events.first |
| 19 | + error_event = error_events.last |
20 | 20 | exception_values = error_event.dig("exception", "values")
|
21 | 21 | expect(exception_values).not_to be_empty
|
22 | 22 | expect(exception_values.first["type"]).to eq("ZeroDivisionError")
|
|
28 | 28 | expect(error_trace_id).to_not be(nil)
|
29 | 29 |
|
30 | 30 | if transaction_events.any?
|
31 |
| - transaction_event = transaction_events.first |
32 |
| - trace_context = transaction_event.dig("contexts", "trace") |
| 31 | + transaction_event = transaction_events.find do |event| |
| 32 | + event.dig("contexts", "trace", "trace_id") == error_trace_id |
| 33 | + end |
33 | 34 |
|
| 35 | + expect(transaction_event).not_to be_nil, "No transaction event found with trace_id #{error_trace_id}" |
| 36 | + |
| 37 | + trace_context = transaction_event.dig("contexts", "trace") |
34 | 38 | expect(trace_context).not_to be_nil
|
35 | 39 |
|
36 | 40 | transaction_trace_id = trace_context["trace_id"]
|
|
59 | 63 | end
|
60 | 64 | end
|
61 | 65 | end
|
| 66 | + |
| 67 | + describe "propagated random value behavior" do |
| 68 | + it "properly propagates and uses sample_rand for sampling decisions in backend transactions" do |
| 69 | + visit "/error" |
| 70 | + |
| 71 | + expect(page).to have_content("Svelte Mini App") |
| 72 | + expect(page).to have_button("Trigger Error") |
| 73 | + |
| 74 | + click_button "trigger-error-btn" |
| 75 | + |
| 76 | + expect(page).to have_content("Error:") |
| 77 | + |
| 78 | + expect(logged_events[:event_count]).to be > 0 |
| 79 | + |
| 80 | + transaction_events = logged_events[:events].select { |event| event["type"] == "transaction" } |
| 81 | + expect(transaction_events).not_to be_empty |
| 82 | + |
| 83 | + transactions = transaction_events.select { |event| |
| 84 | + event.dig("contexts", "trace", "op") == "http.server" |
| 85 | + } |
| 86 | + |
| 87 | + expect(transactions).not_to be_empty, "Should have backend transactions" |
| 88 | + |
| 89 | + transaction = transactions.first |
| 90 | + expect(transaction).not_to be_nil |
| 91 | + |
| 92 | + trace_context = transaction.dig("contexts", "trace") |
| 93 | + expect(trace_context).not_to be_nil |
| 94 | + expect(trace_context["trace_id"]).not_to be_nil |
| 95 | + |
| 96 | + dsc = transaction.dig("_meta", "dsc") |
| 97 | + if dsc && dsc["sample_rand"] |
| 98 | + sample_rand = dsc["sample_rand"] |
| 99 | + |
| 100 | + expect(sample_rand).to match(/^\d+\.\d{1,6}$/) |
| 101 | + |
| 102 | + sample_rand_value = sample_rand.to_f |
| 103 | + expect(sample_rand_value).to be >= 0.0 |
| 104 | + expect(sample_rand_value).to be < 1.0 |
| 105 | + end |
| 106 | + |
| 107 | + logged_events[:envelopes].each do |envelope| |
| 108 | + envelope["items"].each do |item| |
| 109 | + next unless dsc = item.dig("payload", "_meta", "dsc") |
| 110 | + |
| 111 | + sample_rand = dsc["sample_rand"] |
| 112 | + expect(sample_rand).to match(/^\d+\.\d{1,6}$/) |
| 113 | + |
| 114 | + sample_rand_value = sample_rand.to_f |
| 115 | + expect(sample_rand_value).to be >= 0.0 |
| 116 | + expect(sample_rand_value).to be < 1.0 |
| 117 | + |
| 118 | + item["payload"]["spans"].each do |span| |
| 119 | + sample_rand = span["data"]["sentry.sample_rand"] |
| 120 | + expect(sample_rand).to be_a(Float) |
| 121 | + expect(sample_rand).to be >= 0.0 |
| 122 | + expect(sample_rand).to be < 1.0 |
| 123 | + end |
| 124 | + end |
| 125 | + end |
| 126 | + end |
| 127 | + |
| 128 | + it "verifies sampling decisions are based on propagated sample_rand" do |
| 129 | + visit "/error" |
| 130 | + |
| 131 | + expect(page).to have_content("Svelte Mini App") |
| 132 | + expect(page).to have_button("Trigger Error") |
| 133 | + |
| 134 | + click_button "trigger-error-btn" |
| 135 | + |
| 136 | + expect(page).to have_content("Error:") |
| 137 | + |
| 138 | + transaction_events = logged_events[:events].select { |event| event["type"] == "transaction" } |
| 139 | + expect(transaction_events).not_to be_empty |
| 140 | + |
| 141 | + transaction_events.each do |transaction| |
| 142 | + dsc = transaction.dig("_meta", "dsc") |
| 143 | + next unless dsc && dsc["sample_rand"] |
| 144 | + |
| 145 | + sample_rand = dsc["sample_rand"].to_f |
| 146 | + sample_rate = dsc["sample_rate"]&.to_f |
| 147 | + |
| 148 | + expected_sampled = sample_rand < sample_rate |
| 149 | + |
| 150 | + expect(sample_rand).to be >= 0.0 |
| 151 | + expect(sample_rand).to be < 1.0 |
| 152 | + expect(sample_rate).to be > 0.0 |
| 153 | + expect(sample_rate).to be <= 1.0 |
| 154 | + |
| 155 | + expect(dsc["sample_rand"]).to match(/^\d+\.\d{1,6}$/) |
| 156 | + end |
| 157 | + end |
| 158 | + |
| 159 | + it "maintains consistent sample_rand across multiple requests in the same trace" do |
| 160 | + visit "/error" |
| 161 | + |
| 162 | + expect(page).to have_content("Svelte Mini App") |
| 163 | + |
| 164 | + 3.times do |i| |
| 165 | + click_button "trigger-error-btn" |
| 166 | + sleep 0.1 |
| 167 | + end |
| 168 | + |
| 169 | + expect(page).to have_content("Error:") |
| 170 | + |
| 171 | + transaction_events = logged_events[:events].select { |event| event["type"] == "transaction" } |
| 172 | + expect(transaction_events.length).to be >= 2 |
| 173 | + |
| 174 | + traces = transaction_events.group_by { |event| event.dig("contexts", "trace", "trace_id") } |
| 175 | + |
| 176 | + traces.each do |trace_id, transactions| |
| 177 | + next if transactions.length < 2 |
| 178 | + |
| 179 | + sample_rands = transactions.map { |transaction| transaction.dig("_meta", "dsc", "sample_rand") }.compact.uniq |
| 180 | + |
| 181 | + expect(sample_rands.length).to eq(1), |
| 182 | + "All transactions in trace #{trace_id} should have the same sample_rand, but found: #{sample_rands}" |
| 183 | + end |
| 184 | + end |
| 185 | + end |
62 | 186 | end
|
0 commit comments