|
1 | 1 | # frozen_string_literal: true
|
2 | 2 |
|
3 |
| -RSpec.describe "Tracing", type: :feature do |
4 |
| - it "works" do |
| 3 | +RSpec.describe "Tracing", type: :e2e do |
| 4 | + def expect_valid_sample_rand(sample_rand) |
| 5 | + expect(sample_rand).not_to be_nil |
| 6 | + expect(sample_rand).to match(/^\d+\.\d{1,6}$/) |
| 7 | + sample_rand_value = sample_rand.to_f |
| 8 | + expect(sample_rand_value).to be >= 0.0 |
| 9 | + expect(sample_rand_value).to be < 1.0 |
| 10 | + end |
| 11 | + |
| 12 | + def expect_dsc_in_envelope_headers |
| 13 | + envelopes_with_dsc = logged_events[:envelopes].select do |envelope| |
| 14 | + envelope["headers"] && envelope["headers"]["trace"] |
| 15 | + end |
| 16 | + |
| 17 | + expect(envelopes_with_dsc).not_to be_empty |
| 18 | + |
| 19 | + dsc_metadata = envelopes_with_dsc.map { |envelope| envelope["headers"]["trace"] } |
| 20 | + |
| 21 | + envelopes_with_sample_rand = dsc_metadata.select { |dsc| dsc["sample_rand"] } |
| 22 | + expect(envelopes_with_sample_rand).not_to be_empty |
| 23 | + |
| 24 | + envelopes_with_sample_rand.each do |dsc| |
| 25 | + expect_valid_sample_rand(dsc["sample_rand"]) |
| 26 | + end |
| 27 | + |
| 28 | + dsc_metadata |
| 29 | + end |
| 30 | + |
| 31 | + def get_http_server_transactions_with_headers |
| 32 | + transaction_events = logged_events[:events].select { |event| event["type"] == "transaction" } |
| 33 | + expect(transaction_events).not_to be_empty |
| 34 | + |
| 35 | + http_server_transactions = transaction_events.select { |event| |
| 36 | + event.dig("contexts", "trace", "op") == "http.server" |
| 37 | + } |
| 38 | + expect(http_server_transactions).not_to be_empty |
| 39 | + |
| 40 | + transactions_with_headers = http_server_transactions.select { |transaction| |
| 41 | + headers = transaction.dig("request", "headers") |
| 42 | + headers && (headers["Sentry-Trace"] || headers["sentry-trace"]) |
| 43 | + } |
| 44 | + expect(transactions_with_headers).not_to be_empty |
| 45 | + |
| 46 | + transactions_with_headers |
| 47 | + end |
| 48 | + it "validates basic tracing functionality" do |
5 | 49 | visit "/error"
|
6 | 50 |
|
7 | 51 | expect(page).to have_content("Svelte Mini App")
|
|
16 | 60 | error_events = logged_events[:events].select { |event| event["exception"] }
|
17 | 61 | expect(error_events).not_to be_empty
|
18 | 62 |
|
19 |
| - error_event = error_events.first |
| 63 | + error_event = error_events.last |
20 | 64 | exception_values = error_event.dig("exception", "values")
|
21 | 65 | expect(exception_values).not_to be_empty
|
22 | 66 | expect(exception_values.first["type"]).to eq("ZeroDivisionError")
|
23 | 67 |
|
24 |
| - transaction_events = logged_events[:events].select { |event| event["type"] == "transaction" } |
25 |
| - |
26 | 68 | expect(error_event.dig("contexts", "trace")).not_to be_nil
|
27 | 69 | error_trace_id = error_event.dig("contexts", "trace", "trace_id")
|
28 |
| - expect(error_trace_id).to_not be(nil) |
| 70 | + expect(error_trace_id).not_to be_nil |
| 71 | + |
| 72 | + transaction_events = logged_events[:events].select { |event| event["type"] == "transaction" } |
| 73 | + expect(transaction_events).not_to be_empty |
| 74 | + |
| 75 | + transactions_with_dsc = transaction_events.select { |event| |
| 76 | + event.dig("_meta", "dsc", "sample_rand") |
| 77 | + } |
| 78 | + |
| 79 | + transactions_with_dsc.each do |transaction| |
| 80 | + expect_valid_sample_rand(transaction.dig("_meta", "dsc", "sample_rand")) |
| 81 | + end |
| 82 | + end |
| 83 | + |
| 84 | + describe "propagated sample_rand behavior" do |
| 85 | + it "validates DSC metadata is properly generated and included in envelope headers" do |
| 86 | + visit "/error" |
| 87 | + |
| 88 | + expect(page).to have_content("Svelte Mini App") |
| 89 | + expect(page).to have_button("Trigger Error") |
29 | 90 |
|
30 |
| - if transaction_events.any? |
31 |
| - transaction_event = transaction_events.first |
32 |
| - trace_context = transaction_event.dig("contexts", "trace") |
| 91 | + click_button "trigger-error-btn" |
33 | 92 |
|
34 |
| - expect(trace_context).not_to be_nil |
| 93 | + expect(page).to have_content("Error:") |
35 | 94 |
|
36 |
| - transaction_trace_id = trace_context["trace_id"] |
| 95 | + dsc_envelopes = expect_dsc_in_envelope_headers |
37 | 96 |
|
38 |
| - expect(transaction_trace_id).to_not be(nil) |
39 |
| - expect(error_trace_id).to eq(transaction_trace_id) |
| 97 | + dsc_envelopes.each do |dsc| |
| 98 | + expect(dsc["trace_id"]).not_to be_nil |
| 99 | + expect(dsc["trace_id"]).to match(/^[a-f0-9]{32}$/) |
40 | 100 |
|
41 |
| - if transaction_event["_meta"] && transaction_event["_meta"]["dsc"] |
42 |
| - dsc = transaction_event["_meta"]["dsc"] |
43 |
| - expect(dsc).to include("sample_rand") |
| 101 | + expect(dsc["sample_rate"]).not_to be_nil |
| 102 | + expect(dsc["sample_rate"].to_f).to be > 0.0 |
44 | 103 |
|
45 |
| - sample_rand = dsc["sample_rand"] |
46 |
| - expect(sample_rand).to_not be(nil) |
| 104 | + expect(dsc["sample_rand"]).not_to be_nil |
| 105 | + expect_valid_sample_rand(dsc["sample_rand"]) |
| 106 | + |
| 107 | + expect(dsc["sampled"]).to eq("true") |
| 108 | + expect(dsc["environment"]).to eq("development") |
| 109 | + expect(dsc["public_key"]).to eq("user") |
47 | 110 | end
|
48 | 111 | end
|
49 | 112 |
|
50 |
| - logged_events[:envelopes].each do |envelope| |
51 |
| - envelope["items"].each do |item| |
52 |
| - if item["payload"] && item["payload"]["_meta"] && item["payload"]["_meta"]["dsc"] |
53 |
| - dsc = item["payload"]["_meta"]["dsc"] |
| 113 | + it "validates DSC sample_rand generation across multiple requests" do |
| 114 | + visit "/error" |
| 115 | + |
| 116 | + expect(page).to have_content("Svelte Mini App") |
| 117 | + |
| 118 | + 3.times do |i| |
| 119 | + click_button "trigger-error-btn" |
| 120 | + sleep 0.1 |
| 121 | + end |
| 122 | + |
| 123 | + expect(page).to have_content("Error:") |
| 124 | + |
| 125 | + dsc_envelopes = expect_dsc_in_envelope_headers |
| 126 | + expect(dsc_envelopes.length).to be >= 2 |
| 127 | + |
| 128 | + trace_ids = dsc_envelopes.map { |dsc| dsc["trace_id"] }.uniq |
| 129 | + sample_rands = dsc_envelopes.map { |dsc| dsc["sample_rand"] }.uniq |
| 130 | + |
| 131 | + expect(trace_ids.length).to be >= 2 |
| 132 | + expect(sample_rands.length).to be >= 2 |
54 | 133 |
|
55 |
| - if dsc["sample_rand"] |
56 |
| - expect(dsc["sample_rand"]).to_not be(nil) |
57 |
| - end |
58 |
| - end |
| 134 | + sample_rands.each do |sample_rand| |
| 135 | + expect_valid_sample_rand(sample_rand) |
59 | 136 | end
|
60 | 137 | end
|
61 | 138 | end
|
|
0 commit comments