|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +RSpec.describe "Tracing", type: :feature do |
| 4 | + it "works", js: false do |
| 5 | + visit "/error" |
| 6 | + |
| 7 | + expect(page).to have_content("Svelte Mini App") |
| 8 | + expect(page).to have_button("Trigger Error") |
| 9 | + click_button "trigger-error-btn" |
| 10 | + expect(page).to have_content("Error:") |
| 11 | + |
| 12 | + events_data = get_rails_events |
| 13 | + |
| 14 | + expect(events_data["event_count"]).to be > 0 |
| 15 | + |
| 16 | + error_events = events_data["events"].select { |event| event["exception"] } |
| 17 | + expect(error_events).not_to be_empty |
| 18 | + |
| 19 | + error_event = error_events.first |
| 20 | + exception_values = error_event.dig("exception", "values") |
| 21 | + expect(exception_values).not_to be_empty |
| 22 | + expect(exception_values.first["type"]).to eq("ZeroDivisionError") |
| 23 | + |
| 24 | + transaction_events = events_data["events"].select { |event| event["type"] == "transaction" } |
| 25 | + |
| 26 | + expect(error_event.dig("contexts", "trace")).not_to be_nil |
| 27 | + error_trace_id = error_event.dig("contexts", "trace", "trace_id") |
| 28 | + expect(error_trace_id).to match(/\A[a-f0-9]{32}\z/) |
| 29 | + |
| 30 | + if transaction_events.any? |
| 31 | + transaction_event = transaction_events.first |
| 32 | + trace_context = transaction_event.dig("contexts", "trace") |
| 33 | + |
| 34 | + expect(trace_context).not_to be_nil |
| 35 | + |
| 36 | + transaction_trace_id = trace_context["trace_id"] |
| 37 | + |
| 38 | + expect(transaction_trace_id).to match(/\A[a-f0-9]{32}\z/) |
| 39 | + expect(error_trace_id).to eq(transaction_trace_id) |
| 40 | + |
| 41 | + if transaction_event["_meta"] && transaction_event["_meta"]["dsc"] |
| 42 | + dsc = transaction_event["_meta"]["dsc"] |
| 43 | + expect(dsc).to include("sample_rand") |
| 44 | + |
| 45 | + sample_rand = dsc["sample_rand"] |
| 46 | + expect(sample_rand).to match(/\A0\.\d{6}\z/) |
| 47 | + end |
| 48 | + end |
| 49 | + |
| 50 | + events_data["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"] |
| 54 | + |
| 55 | + if dsc["sample_rand"] |
| 56 | + expect(dsc["sample_rand"]).to match(/\A0\.\d{6}\z/) |
| 57 | + end |
| 58 | + end |
| 59 | + end |
| 60 | + end |
| 61 | + end |
| 62 | + |
| 63 | + private |
| 64 | + |
| 65 | + def get_rails_events |
| 66 | + log_file_path = File.join(Dir.pwd, "log", "sentry_debug_events.log") |
| 67 | + |
| 68 | + return { "events" => [], "envelopes" => [], "event_count" => 0, "envelope_count" => 0 } unless File.exist?(log_file_path) |
| 69 | + |
| 70 | + events = [] |
| 71 | + File.readlines(log_file_path).each do |line| |
| 72 | + line = line.strip |
| 73 | + next if line.empty? |
| 74 | + |
| 75 | + begin |
| 76 | + events << JSON.parse(line) |
| 77 | + rescue JSON::ParserError => e |
| 78 | + puts "Failed to parse line: #{line}, error: #{e.message}" |
| 79 | + end |
| 80 | + end |
| 81 | + |
| 82 | + extracted_events = [] |
| 83 | + envelopes = [] |
| 84 | + |
| 85 | + events.each do |event_data| |
| 86 | + envelopes << { |
| 87 | + "headers" => event_data["envelope_headers"], |
| 88 | + "items" => event_data["items"] |
| 89 | + } |
| 90 | + |
| 91 | + # Extract actual events from envelope items |
| 92 | + event_data["items"].each do |item| |
| 93 | + if item["headers"]["type"] == "event" |
| 94 | + extracted_events << item["payload"] |
| 95 | + end |
| 96 | + end |
| 97 | + end |
| 98 | + |
| 99 | + { |
| 100 | + "events" => extracted_events, |
| 101 | + "envelopes" => envelopes, |
| 102 | + "event_count" => extracted_events.length, |
| 103 | + "envelope_count" => envelopes.length, |
| 104 | + "raw_events" => events |
| 105 | + } |
| 106 | + end |
| 107 | + |
| 108 | + def clear_rails_events |
| 109 | + log_file_path = File.join(Dir.pwd, "log", "sentry_debug_events.log") |
| 110 | + File.write(log_file_path, "") if File.exist?(log_file_path) |
| 111 | + end |
| 112 | +end |
0 commit comments