|
445 | 445 | end |
446 | 446 |
|
447 | 447 | describe ".start_transaction" do |
| 448 | + describe "when not continuing an existing trace" do |
| 449 | + before do |
| 450 | + perform_basic_setup do |config| |
| 451 | + config.traces_sample_rate = 1.0 |
| 452 | + end |
| 453 | + end |
| 454 | + |
| 455 | + it "does not adopt the scope's propagation context when it wasn't established for this call" do |
| 456 | + propagation_context = Sentry.get_current_scope.propagation_context |
| 457 | + |
| 458 | + transaction = described_class.start_transaction(name: "test", op: "test.op") |
| 459 | + |
| 460 | + # each independent call gets its own, unrelated trace_id by default - only |
| 461 | + # calls that flow through an env flagged with |
| 462 | + # PropagationContext::ESTABLISHED_ENV_KEY (e.g. a Rack request that went |
| 463 | + # through Sentry::Rails::CaptureContext) adopt the scope's propagation context |
| 464 | + expect(transaction.trace_id).not_to eq(propagation_context.trace_id) |
| 465 | + end |
| 466 | + |
| 467 | + context "when the scope's propagation context was established for this exact env" do |
| 468 | + let(:env) { { Sentry::PropagationContext::ESTABLISHED_ENV_KEY => true } } |
| 469 | + |
| 470 | + before do |
| 471 | + Sentry.get_current_scope.generate_propagation_context(env) |
| 472 | + end |
| 473 | + |
| 474 | + it "adopts the scope's propagation context trace_id and sample_rand" do |
| 475 | + propagation_context = Sentry.get_current_scope.propagation_context |
| 476 | + |
| 477 | + transaction = described_class.start_transaction( |
| 478 | + name: "test", op: "test.op", custom_sampling_context: { env: env } |
| 479 | + ) |
| 480 | + |
| 481 | + expect(transaction.trace_id).to eq(propagation_context.trace_id) |
| 482 | + expect(transaction.sample_rand).to eq(propagation_context.sample_rand) |
| 483 | + end |
| 484 | + |
| 485 | + it "does not override an explicitly provided trace_id" do |
| 486 | + transaction = described_class.start_transaction( |
| 487 | + name: "test", op: "test.op", trace_id: "a" * 32, custom_sampling_context: { env: env } |
| 488 | + ) |
| 489 | + |
| 490 | + expect(transaction.trace_id).to eq("a" * 32) |
| 491 | + end |
| 492 | + |
| 493 | + it "does not override an explicitly provided sample_rand" do |
| 494 | + propagation_context = Sentry.get_current_scope.propagation_context |
| 495 | + |
| 496 | + transaction = described_class.start_transaction( |
| 497 | + name: "test", op: "test.op", sample_rand: 0.999999, custom_sampling_context: { env: env } |
| 498 | + ) |
| 499 | + |
| 500 | + expect(transaction.trace_id).to eq(propagation_context.trace_id) |
| 501 | + expect(transaction.sample_rand).to eq(0.999999) |
| 502 | + end |
| 503 | + end |
| 504 | + end |
| 505 | + |
448 | 506 | describe "sampler example" do |
449 | 507 | before do |
450 | 508 | perform_basic_setup do |config| |
|
1038 | 1096 | propagation_context = Sentry.get_current_scope.propagation_context |
1039 | 1097 | expect(propagation_context.incoming_trace).to eq(false) |
1040 | 1098 | end |
| 1099 | + |
| 1100 | + context "when trace context was already established for this env" do |
| 1101 | + let(:env) { { "HTTP_FOO" => "bar", Sentry::PropagationContext::ESTABLISHED_ENV_KEY => true } } |
| 1102 | + |
| 1103 | + it "does not regenerate the scope's propagation context" do |
| 1104 | + existing_propagation_context = Sentry.get_current_scope.propagation_context |
| 1105 | + |
| 1106 | + expect(Sentry.get_current_scope).not_to receive(:generate_propagation_context) |
| 1107 | + described_class.continue_trace(env) |
| 1108 | + |
| 1109 | + expect(Sentry.get_current_scope.propagation_context).to eq(existing_propagation_context) |
| 1110 | + end |
| 1111 | + end |
1041 | 1112 | end |
1042 | 1113 |
|
1043 | 1114 | context "with incoming sentry trace" do |
|
0 commit comments