|
133 | 133 | end |
134 | 134 | end |
135 | 135 |
|
| 136 | + describe ".should_continue_trace?" do |
| 137 | + # Decision matrix: |
| 138 | + # | Baggage org | SDK org | strict | Result | |
| 139 | + # |-------------|---------|--------|--------------| |
| 140 | + # | 1 | 1 | false | Continue | |
| 141 | + # | None | 1 | false | Continue | |
| 142 | + # | 1 | None | false | Continue | |
| 143 | + # | None | None | false | Continue | |
| 144 | + # | 1 | 2 | false | Start new | |
| 145 | + # | 1 | 1 | true | Continue | |
| 146 | + # | None | 1 | true | Start new | |
| 147 | + # | 1 | None | true | Start new | |
| 148 | + # | None | None | true | Continue | |
| 149 | + # | 1 | 2 | true | Start new | |
| 150 | + |
| 151 | + let(:sentry_trace) { "771a43a4192642f0b136d5159a501700-7c51afd529da4a2a-1" } |
| 152 | + |
| 153 | + def make_env(sentry_trace:, baggage_org_id: nil) |
| 154 | + baggage_parts = ["sentry-trace_id=771a43a4192642f0b136d5159a501700"] |
| 155 | + baggage_parts << "sentry-org_id=#{baggage_org_id}" if baggage_org_id |
| 156 | + |
| 157 | + { |
| 158 | + "sentry-trace" => sentry_trace, |
| 159 | + "baggage" => baggage_parts.join(",") |
| 160 | + } |
| 161 | + end |
| 162 | + |
| 163 | + context "with strict_trace_continuation=false" do |
| 164 | + it "continues when baggage org matches SDK org" do |
| 165 | + perform_basic_setup do |config| |
| 166 | + config.dsn = "https://key@o1.ingest.sentry.io/42" |
| 167 | + config.strict_trace_continuation = false |
| 168 | + end |
| 169 | + |
| 170 | + env = make_env(sentry_trace: sentry_trace, baggage_org_id: "1") |
| 171 | + propagation_context = described_class.new(scope, env) |
| 172 | + expect(propagation_context.incoming_trace).to eq(true) |
| 173 | + expect(propagation_context.trace_id).to eq("771a43a4192642f0b136d5159a501700") |
| 174 | + end |
| 175 | + |
| 176 | + it "continues when baggage has no org but SDK has org" do |
| 177 | + perform_basic_setup do |config| |
| 178 | + config.dsn = "https://key@o1.ingest.sentry.io/42" |
| 179 | + config.strict_trace_continuation = false |
| 180 | + end |
| 181 | + |
| 182 | + env = make_env(sentry_trace: sentry_trace, baggage_org_id: nil) |
| 183 | + propagation_context = described_class.new(scope, env) |
| 184 | + expect(propagation_context.incoming_trace).to eq(true) |
| 185 | + expect(propagation_context.trace_id).to eq("771a43a4192642f0b136d5159a501700") |
| 186 | + end |
| 187 | + |
| 188 | + it "continues when baggage has org but SDK has no org" do |
| 189 | + perform_basic_setup do |config| |
| 190 | + config.strict_trace_continuation = false |
| 191 | + end |
| 192 | + |
| 193 | + env = make_env(sentry_trace: sentry_trace, baggage_org_id: "1") |
| 194 | + propagation_context = described_class.new(scope, env) |
| 195 | + expect(propagation_context.incoming_trace).to eq(true) |
| 196 | + expect(propagation_context.trace_id).to eq("771a43a4192642f0b136d5159a501700") |
| 197 | + end |
| 198 | + |
| 199 | + it "continues when neither has org" do |
| 200 | + perform_basic_setup do |config| |
| 201 | + config.strict_trace_continuation = false |
| 202 | + end |
| 203 | + |
| 204 | + env = make_env(sentry_trace: sentry_trace, baggage_org_id: nil) |
| 205 | + propagation_context = described_class.new(scope, env) |
| 206 | + expect(propagation_context.incoming_trace).to eq(true) |
| 207 | + expect(propagation_context.trace_id).to eq("771a43a4192642f0b136d5159a501700") |
| 208 | + end |
| 209 | + |
| 210 | + it "starts new trace when orgs mismatch" do |
| 211 | + perform_basic_setup do |config| |
| 212 | + config.dsn = "https://key@o2.ingest.sentry.io/42" |
| 213 | + config.strict_trace_continuation = false |
| 214 | + end |
| 215 | + |
| 216 | + env = make_env(sentry_trace: sentry_trace, baggage_org_id: "1") |
| 217 | + propagation_context = described_class.new(scope, env) |
| 218 | + expect(propagation_context.incoming_trace).to eq(false) |
| 219 | + expect(propagation_context.trace_id).not_to eq("771a43a4192642f0b136d5159a501700") |
| 220 | + end |
| 221 | + end |
| 222 | + |
| 223 | + context "with strict_trace_continuation=true" do |
| 224 | + it "continues when baggage org matches SDK org" do |
| 225 | + perform_basic_setup do |config| |
| 226 | + config.dsn = "https://key@o1.ingest.sentry.io/42" |
| 227 | + config.strict_trace_continuation = true |
| 228 | + end |
| 229 | + |
| 230 | + env = make_env(sentry_trace: sentry_trace, baggage_org_id: "1") |
| 231 | + propagation_context = described_class.new(scope, env) |
| 232 | + expect(propagation_context.incoming_trace).to eq(true) |
| 233 | + expect(propagation_context.trace_id).to eq("771a43a4192642f0b136d5159a501700") |
| 234 | + end |
| 235 | + |
| 236 | + it "starts new trace when baggage has no org but SDK has org" do |
| 237 | + perform_basic_setup do |config| |
| 238 | + config.dsn = "https://key@o1.ingest.sentry.io/42" |
| 239 | + config.strict_trace_continuation = true |
| 240 | + end |
| 241 | + |
| 242 | + env = make_env(sentry_trace: sentry_trace, baggage_org_id: nil) |
| 243 | + propagation_context = described_class.new(scope, env) |
| 244 | + expect(propagation_context.incoming_trace).to eq(false) |
| 245 | + expect(propagation_context.trace_id).not_to eq("771a43a4192642f0b136d5159a501700") |
| 246 | + end |
| 247 | + |
| 248 | + it "starts new trace when baggage has org but SDK has no org" do |
| 249 | + perform_basic_setup do |config| |
| 250 | + config.strict_trace_continuation = true |
| 251 | + end |
| 252 | + |
| 253 | + env = make_env(sentry_trace: sentry_trace, baggage_org_id: "1") |
| 254 | + propagation_context = described_class.new(scope, env) |
| 255 | + expect(propagation_context.incoming_trace).to eq(false) |
| 256 | + expect(propagation_context.trace_id).not_to eq("771a43a4192642f0b136d5159a501700") |
| 257 | + end |
| 258 | + |
| 259 | + it "continues when neither has org" do |
| 260 | + perform_basic_setup do |config| |
| 261 | + config.strict_trace_continuation = true |
| 262 | + end |
| 263 | + |
| 264 | + env = make_env(sentry_trace: sentry_trace, baggage_org_id: nil) |
| 265 | + propagation_context = described_class.new(scope, env) |
| 266 | + expect(propagation_context.incoming_trace).to eq(true) |
| 267 | + expect(propagation_context.trace_id).to eq("771a43a4192642f0b136d5159a501700") |
| 268 | + end |
| 269 | + |
| 270 | + it "starts new trace when orgs mismatch" do |
| 271 | + perform_basic_setup do |config| |
| 272 | + config.dsn = "https://key@o2.ingest.sentry.io/42" |
| 273 | + config.strict_trace_continuation = true |
| 274 | + end |
| 275 | + |
| 276 | + env = make_env(sentry_trace: sentry_trace, baggage_org_id: "1") |
| 277 | + propagation_context = described_class.new(scope, env) |
| 278 | + expect(propagation_context.incoming_trace).to eq(false) |
| 279 | + expect(propagation_context.trace_id).not_to eq("771a43a4192642f0b136d5159a501700") |
| 280 | + end |
| 281 | + end |
| 282 | + |
| 283 | + context "with explicit org_id config" do |
| 284 | + it "uses explicit org_id over DSN-parsed org_id" do |
| 285 | + perform_basic_setup do |config| |
| 286 | + config.dsn = "https://key@o1234.ingest.sentry.io/42" |
| 287 | + config.org_id = "9999" |
| 288 | + config.strict_trace_continuation = false |
| 289 | + end |
| 290 | + |
| 291 | + env = make_env(sentry_trace: sentry_trace, baggage_org_id: "1234") |
| 292 | + propagation_context = described_class.new(scope, env) |
| 293 | + # org_id mismatch: baggage has 1234 but SDK effective org_id is 9999 |
| 294 | + expect(propagation_context.incoming_trace).to eq(false) |
| 295 | + expect(propagation_context.trace_id).not_to eq("771a43a4192642f0b136d5159a501700") |
| 296 | + end |
| 297 | + |
| 298 | + it "continues when explicit org_id matches baggage org_id" do |
| 299 | + perform_basic_setup do |config| |
| 300 | + config.dsn = "https://key@o1234.ingest.sentry.io/42" |
| 301 | + config.org_id = "5678" |
| 302 | + config.strict_trace_continuation = false |
| 303 | + end |
| 304 | + |
| 305 | + env = make_env(sentry_trace: sentry_trace, baggage_org_id: "5678") |
| 306 | + propagation_context = described_class.new(scope, env) |
| 307 | + expect(propagation_context.incoming_trace).to eq(true) |
| 308 | + expect(propagation_context.trace_id).to eq("771a43a4192642f0b136d5159a501700") |
| 309 | + end |
| 310 | + end |
| 311 | + end |
| 312 | + |
136 | 313 | describe ".extract_sentry_trace" do |
137 | 314 | it "extracts valid sentry-trace without whitespace" do |
138 | 315 | sentry_trace = "771a43a4192642f0b136d5159a501700-7c51afd529da4a2a-1" |
|
0 commit comments