|
100 | 100 | expect(baggage.mutable).to eq(false) |
101 | 101 | end |
102 | 102 | end |
| 103 | + |
| 104 | + describe ".serialize_with_third_party" do |
| 105 | + let(:sentry_items) do |
| 106 | + { |
| 107 | + "trace_id" => "771a43a4192642f0b136d5159a501700", |
| 108 | + "public_key" => "49d0f7386ad645858ae85020e393bef3", |
| 109 | + "sample_rate" => "0.01337" |
| 110 | + } |
| 111 | + end |
| 112 | + |
| 113 | + context "when combined baggage is within limits" do |
| 114 | + it "includes both sentry and third-party items unchanged" do |
| 115 | + third_party_header = "routingKey=myvalue,tenantId=123" |
| 116 | + result = described_class.serialize_with_third_party(sentry_items, third_party_header) |
| 117 | + |
| 118 | + expect(result).to include("sentry-trace_id=771a43a4192642f0b136d5159a501700") |
| 119 | + expect(result).to include("sentry-public_key=49d0f7386ad645858ae85020e393bef3") |
| 120 | + expect(result).to include("sentry-sample_rate=0.01337") |
| 121 | + expect(result).to include("routingKey=myvalue") |
| 122 | + expect(result).to include("tenantId=123") |
| 123 | + end |
| 124 | + end |
| 125 | + |
| 126 | + context "when exceeding MAX_MEMBER_COUNT (64)" do |
| 127 | + it "drops third-party items first" do |
| 128 | + # Create 10 sentry items |
| 129 | + many_sentry_items = (0...10).each_with_object({}) do |i, hash| |
| 130 | + hash["key#{i}"] = "value#{i}" |
| 131 | + end |
| 132 | + |
| 133 | + # Create 60 third-party items (total would be 70, exceeds 64) |
| 134 | + third_party_items = (0...60).map { |i| "third#{i}=val#{i}" }.join(",") |
| 135 | + |
| 136 | + result = described_class.serialize_with_third_party(many_sentry_items, third_party_items) |
| 137 | + |
| 138 | + # All 10 sentry items should be present |
| 139 | + (0...10).each do |i| |
| 140 | + expect(result).to include("sentry-key#{i}=value#{i}") |
| 141 | + end |
| 142 | + |
| 143 | + # Count total items (should be 64 max) |
| 144 | + total_items = result.split(",").size |
| 145 | + expect(total_items).to be <= 64 |
| 146 | + |
| 147 | + # Some third-party items should be dropped |
| 148 | + third_party_count = result.split(",").count { |item| item.start_with?("third") } |
| 149 | + expect(third_party_count).to be < 60 |
| 150 | + end |
| 151 | + end |
| 152 | + |
| 153 | + context "when exceeding MAX_BAGGAGE_BYTES (8192)" do |
| 154 | + it "drops third-party items first" do |
| 155 | + # Create sentry items that are ~2KB |
| 156 | + large_sentry_items = (0...5).each_with_object({}) do |i, hash| |
| 157 | + hash["key#{i}"] = "x" * 350 |
| 158 | + end |
| 159 | + |
| 160 | + # Create third-party items that would push us over 8192 bytes |
| 161 | + large_third_party = (0...20).map { |i| "third#{i}=#{'y' * 350}" }.join(",") |
| 162 | + |
| 163 | + result = described_class.serialize_with_third_party(large_sentry_items, large_third_party) |
| 164 | + |
| 165 | + # All sentry items should be present |
| 166 | + (0...5).each do |i| |
| 167 | + expect(result).to include("sentry-key#{i}=") |
| 168 | + end |
| 169 | + |
| 170 | + # Total size should not exceed 8192 bytes |
| 171 | + expect(result.bytesize).to be <= 8192 |
| 172 | + |
| 173 | + # Some third-party items should be dropped |
| 174 | + third_party_count = result.split(",").count { |item| item.start_with?("third") } |
| 175 | + expect(third_party_count).to be < 20 |
| 176 | + end |
| 177 | + end |
| 178 | + |
| 179 | + context "when sentry items alone exceed limits" do |
| 180 | + it "drops sentry items to fit within limits" do |
| 181 | + # Create 70 sentry items (exceeds 64) |
| 182 | + many_sentry_items = (0...70).each_with_object({}) do |i, hash| |
| 183 | + hash["key#{i}"] = "value#{i}" |
| 184 | + end |
| 185 | + |
| 186 | + result = described_class.serialize_with_third_party(many_sentry_items, nil) |
| 187 | + |
| 188 | + # Should have exactly 64 items |
| 189 | + total_items = result.split(",").size |
| 190 | + expect(total_items).to eq(64) |
| 191 | + end |
| 192 | + |
| 193 | + it "drops sentry items to fit within byte limit" do |
| 194 | + # Create sentry items that exceed 8192 bytes |
| 195 | + large_sentry_items = (0...30).each_with_object({}) do |i, hash| |
| 196 | + hash["key#{i}"] = "x" * 400 |
| 197 | + end |
| 198 | + |
| 199 | + result = described_class.serialize_with_third_party(large_sentry_items, nil) |
| 200 | + |
| 201 | + # Should not exceed byte limit |
| 202 | + expect(result.bytesize).to be <= 8192 |
| 203 | + |
| 204 | + # Should have dropped some items |
| 205 | + total_items = result.split(",").size |
| 206 | + expect(total_items).to be < 30 |
| 207 | + end |
| 208 | + end |
| 209 | + |
| 210 | + context "when third_party_header is nil or empty" do |
| 211 | + it "handles nil third-party header" do |
| 212 | + result = described_class.serialize_with_third_party(sentry_items, nil) |
| 213 | + |
| 214 | + expect(result).to include("sentry-trace_id=771a43a4192642f0b136d5159a501700") |
| 215 | + expect(result).to include("sentry-public_key=49d0f7386ad645858ae85020e393bef3") |
| 216 | + expect(result).to include("sentry-sample_rate=0.01337") |
| 217 | + end |
| 218 | + |
| 219 | + it "handles empty third-party header" do |
| 220 | + result = described_class.serialize_with_third_party(sentry_items, "") |
| 221 | + |
| 222 | + expect(result).to include("sentry-trace_id=771a43a4192642f0b136d5159a501700") |
| 223 | + expect(result).to include("sentry-public_key=49d0f7386ad645858ae85020e393bef3") |
| 224 | + expect(result).to include("sentry-sample_rate=0.01337") |
| 225 | + end |
| 226 | + end |
| 227 | + end |
103 | 228 | end |
0 commit comments