|
257 | 257 | end |
258 | 258 |
|
259 | 259 | it "uses traces_sample_rate for sampling (positive result)" do |
260 | | - allow(Random).to receive(:rand).and_return(0.4) |
| 260 | + # Create transaction with sample_rand < sample_rate (0.5) to ensure sampled |
| 261 | + transaction = described_class.new(op: "rack.request", hub: Sentry.get_current_hub, sample_rand: 0.4) |
261 | 262 |
|
262 | | - subject.set_initial_sample_decision(sampling_context: {}) |
263 | | - expect(subject.sampled).to eq(true) |
264 | | - expect(subject.effective_sample_rate).to eq(0.5) |
| 263 | + transaction.set_initial_sample_decision(sampling_context: {}) |
| 264 | + expect(transaction.sampled).to eq(true) |
| 265 | + expect(transaction.effective_sample_rate).to eq(0.5) |
265 | 266 | expect(string_io.string).to include( |
266 | 267 | "[Tracing] Starting <rack.request> transaction" |
267 | 268 | ) |
268 | 269 | end |
269 | 270 |
|
270 | 271 | it "uses traces_sample_rate for sampling (negative result)" do |
271 | | - allow(Random).to receive(:rand).and_return(0.6) |
| 272 | + # Create transaction with sample_rand > sample_rate (0.5) to ensure not sampled |
| 273 | + transaction = described_class.new(op: "rack.request", hub: Sentry.get_current_hub, sample_rand: 0.6) |
272 | 274 |
|
273 | | - subject.set_initial_sample_decision(sampling_context: {}) |
274 | | - expect(subject.sampled).to eq(false) |
275 | | - expect(subject.effective_sample_rate).to eq(0.5) |
| 275 | + transaction.set_initial_sample_decision(sampling_context: {}) |
| 276 | + expect(transaction.sampled).to eq(false) |
| 277 | + expect(transaction.effective_sample_rate).to eq(0.5) |
276 | 278 | expect(string_io.string).to include( |
277 | 279 | "[Tracing] Discarding <rack.request> transaction because it's not included in the random sample (sampling rate = 0.5)" |
278 | 280 | ) |
|
474 | 476 | end |
475 | 477 |
|
476 | 478 | it "submits the event with the transaction's hub by default" do |
477 | | - subject.instance_variable_set(:@hub, another_hub) |
| 479 | + # Create transaction with the specific hub from the beginning |
| 480 | + transaction = described_class.new( |
| 481 | + op: "sql.query", |
| 482 | + description: "SELECT * FROM users;", |
| 483 | + status: "ok", |
| 484 | + sampled: true, |
| 485 | + parent_sampled: true, |
| 486 | + name: "foo", |
| 487 | + source: :view, |
| 488 | + hub: another_hub |
| 489 | + ) |
478 | 490 |
|
479 | 491 | expect(another_hub).to receive(:capture_event) |
480 | 492 |
|
481 | | - subject.finish |
| 493 | + transaction.finish |
482 | 494 | end |
483 | 495 | end |
484 | 496 |
|
|
511 | 523 | it "records lost event with reason backpressure" do |
512 | 524 | expect(Sentry.get_current_client.transport).to receive(:any_rate_limited?).and_return(true) |
513 | 525 | Sentry.backpressure_monitor.run |
514 | | - allow(Random).to receive(:rand).and_return(0.6) |
515 | 526 |
|
516 | | - subject.finish |
| 527 | + # Create transaction with sample_rand that will be rejected after backpressure downsampling |
| 528 | + # With traces_sample_rate = 1.0 and downsample_factor = 1, effective rate becomes 0.5 |
| 529 | + # So sample_rand = 0.6 > 0.5 will be rejected |
| 530 | + transaction = described_class.new(hub: Sentry.get_current_hub, sample_rand: 0.6) |
| 531 | + |
| 532 | + transaction.finish |
517 | 533 | expect(Sentry.get_current_client.transport).to have_recorded_lost_event(:backpressure, 'transaction') |
518 | 534 | expect(Sentry.get_current_client.transport).to have_recorded_lost_event(:backpressure, 'span') |
519 | 535 | end |
|
556 | 572 | name: "foo", |
557 | 573 | source: source, |
558 | 574 | hub: Sentry.get_current_hub, |
559 | | - baggage: incoming_baggage |
| 575 | + baggage: incoming_baggage, |
| 576 | + sample_rand: 0.123456 # Use a known value for predictable testing |
560 | 577 | ) |
561 | 578 |
|
562 | 579 | transaction.set_initial_sample_decision(sampling_context: {}) |
|
574 | 591 | expect(baggage.items).to eq({ |
575 | 592 | "environment" => "development", |
576 | 593 | "public_key" => "12345", |
| 594 | + "sample_rand" => "0.123456", # Known value from transaction creation |
577 | 595 | "trace_id" => subject.trace_id, |
578 | 596 | "transaction"=>"foo", |
579 | 597 | "sample_rate" => "1.0", |
|
630 | 648 | expect(baggage.items).to eq({ |
631 | 649 | "environment" => "development", |
632 | 650 | "public_key" => "12345", |
| 651 | + "sample_rand" => "0.123456", # Known value from transaction creation |
633 | 652 | "trace_id" => subject.trace_id, |
634 | 653 | "transaction"=>"foo", |
635 | 654 | "sample_rate" => "1.0", |
|
0 commit comments