Skip to content

Commit 64f5250

Browse files
committed
Add a basic e2e spec for tracing
1 parent 1bf7961 commit 64f5250

6 files changed

Lines changed: 159 additions & 1 deletion

File tree

.rspec

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
--require spec_helper
2+
--format progress
3+
--color
4+
--order rand

Procfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
rails: cd spec/apps/rails-mini && bundle exec ruby app.rb
2+
svelte: cd spec/apps/svelte-mini && npm run dev

Rakefile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# frozen_string_literal: true
2+
3+
require "rake/clean"
4+
require_relative "lib/sentry/test/rake_tasks"
5+
6+
Sentry::Test::RakeTasks.define_spec_tasks()
7+
8+
task default: :spec

sentry-ruby/spec/spec_helper.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@
2727

2828
require "sentry-ruby"
2929
require "sentry/test_helper"
30-
require "webmock/rspec"
3130

31+
require "webmock/rspec"
3232
require_relative "support/profiler"
3333
require_relative "support/stacktrace_test_fixture"
3434

spec/features/tracing_spec.rb

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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

spec/spec_helper.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# frozen_string_literal: true
2+
3+
require "sentry-ruby"
4+
require "sentry/test_helper"
5+
6+
require "selenium-webdriver"
7+
8+
require "capybara"
9+
require "capybara/rspec"
10+
11+
Capybara.configure do |config|
12+
config.default_driver = :selenium_headless_chrome
13+
config.javascript_driver = :selenium_headless_chrome
14+
config.app_host = ENV.fetch("SENTRY_E2E_SVELTE_APP_URL", "http://localhost:4001")
15+
end
16+
17+
Capybara.register_driver :selenium_headless_chrome do |app|
18+
options = Selenium::WebDriver::Chrome::Options.new
19+
20+
options.add_argument("--headless")
21+
options.add_argument("--disable-dev-shm-usage")
22+
options.add_argument("--no-sandbox")
23+
options.add_argument("--disable-gpu")
24+
options.add_argument("--temp-profile")
25+
options.binary = "/usr/bin/chromium" if File.exist?("/usr/bin/chromium")
26+
27+
Capybara::Selenium::Driver.new(app, browser: :chrome, options: options)
28+
end
29+
30+
RSpec.configure do |config|
31+
config.include(Capybara::DSL, type: :e2e)
32+
end

0 commit comments

Comments
 (0)