Skip to content

Commit

Permalink
Migrate from to_hash to to_h (#2351)
Browse files Browse the repository at this point in the history
* Migrate from to_hash to to_h

As @solnic pointed out in #2350 (comment)
`to_hash` has special meaning in Ruby and could be called implicitly
in contexts like double splatting argument. So we should switch to `to_h`
to avoid potential issues.
  • Loading branch information
st0012 authored and sl0thentr0py committed Oct 31, 2024
1 parent 52032f3 commit d7522f9
Show file tree
Hide file tree
Showing 54 changed files with 184 additions and 183 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
### Breaking Changes

- Remove `config.async` [#1894](https://github.com/getsentry/sentry-ruby/pull/1894)
- Migrate from to_hash to to_h ([#2351](https://github.com/getsentry/sentry-ruby/pull/2351))

## Unreleased

Expand Down
16 changes: 8 additions & 8 deletions sentry-delayed_job/spec/sentry/delayed_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def do_nothing_with_args(a)
enqueued_job.invoke_job

expect(transport.events.count).to eq(1)
event = transport.events.last.to_hash
event = transport.events.last.to_h
expect(event[:message]).to eq("report")
expect(event[:contexts][:"Delayed-Job"][:id]).to eq(enqueued_job.id.to_s)
expect(event[:tags]).to eq({ "delayed_job.id" => enqueued_job.id.to_s, "delayed_job.queue" => nil })
Expand All @@ -68,7 +68,7 @@ def do_nothing_with_args(a)
enqueued_job.invoke_job

expect(transport.events.count).to eq(1)
event = transport.events.last.to_hash
event = transport.events.last.to_h
expect(event[:message]).to eq("tagged report")
expect(event[:tags]).to eq({ "delayed_job.id" => enqueued_job.id.to_s, "delayed_job.queue" => nil, number: 1 })

Expand All @@ -77,7 +77,7 @@ def do_nothing_with_args(a)
enqueued_job.invoke_job

expect(transport.events.count).to eq(2)
event = transport.events.last.to_hash
event = transport.events.last.to_h
expect(event[:tags]).to eq({ "delayed_job.id" => enqueued_job.id.to_s, "delayed_job.queue" => nil })
end

Expand All @@ -93,7 +93,7 @@ def do_nothing_with_args(a)
end.to raise_error(ZeroDivisionError)

expect(transport.events.count).to eq(1)
event = transport.events.last.to_hash
event = transport.events.last.to_h

expect(event[:sdk]).to eq({ name: "sentry.ruby.delayed_job", version: described_class::VERSION })
expect(event.dig(:exception, :values, 0, :type)).to eq("ZeroDivisionError")
Expand All @@ -109,7 +109,7 @@ def do_nothing_with_args(a)
end.to raise_error(RuntimeError)

expect(transport.events.count).to eq(1)
event = transport.events.last.to_hash
event = transport.events.last.to_h

expect(event[:tags]).to eq({ "delayed_job.id" => enqueued_job.id.to_s, "delayed_job.queue" => nil, number: 1 })
expect(Sentry.get_current_scope.extra).to eq({})
Expand All @@ -123,7 +123,7 @@ def do_nothing_with_args(a)
end.to raise_error(ZeroDivisionError)

expect(transport.events.count).to eq(2)
event = transport.events.last.to_hash
event = transport.events.last.to_h
expect(event[:tags]).to eq({ "delayed_job.id" => enqueued_job.id.to_s, "delayed_job.queue" => nil })
expect(Sentry.get_current_scope.extra).to eq({})
expect(Sentry.get_current_scope.tags).to eq({})
Expand Down Expand Up @@ -228,7 +228,7 @@ def perform
it "injects ActiveJob information to the event" do
expect(transport.events.count).to eq(1)

event = transport.events.last.to_hash
event = transport.events.last.to_h
expect(event[:message]).to eq("report from ActiveJob")
expect(event[:tags]).to match({ "delayed_job.id" => anything, "delayed_job.queue" => "default", number: 1 })
expect(event[:contexts][:"Active-Job"][:job_class]).to eq("ReportingJob")
Expand All @@ -255,7 +255,7 @@ def perform
it "injects ActiveJob information to the event" do
expect(transport.events.count).to eq(1)

event = transport.events.last.to_hash
event = transport.events.last.to_h
expect(event.dig(:exception, :values, 0, :type)).to eq("ZeroDivisionError")
expect(event[:tags]).to match({ "delayed_job.id" => anything, "delayed_job.queue" => "default", number: 2 })
expect(event[:contexts][:"Active-Job"][:job_class]).to eq("FailedJob")
Expand Down
10 changes: 5 additions & 5 deletions sentry-rails/spec/sentry/rails/activejob_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ def perform
expect(transport.events.size).to eq(1)

event = transport.events.first
exceptions_data = event.exception.to_hash[:values]
exceptions_data = event.exception.to_h[:values]

expect(exceptions_data.count).to eq(2)
expect(exceptions_data[0][:type]).to eq("FailedJob::TestError")
Expand Down Expand Up @@ -293,7 +293,7 @@ def perform
first = transport.events[0]
check_in_id = first.check_in_id
expect(first).to be_a(Sentry::CheckInEvent)
expect(first.to_hash).to include(
expect(first.to_h).to include(
type: 'check_in',
check_in_id: check_in_id,
monitor_slug: "normaljobwithcron",
Expand All @@ -302,7 +302,7 @@ def perform

second = transport.events[1]
expect(second).to be_a(Sentry::CheckInEvent)
expect(second.to_hash).to include(
expect(second.to_h).to include(
:duration,
type: 'check_in',
check_in_id: check_in_id,
Expand All @@ -321,7 +321,7 @@ def perform
first = transport.events[0]
check_in_id = first.check_in_id
expect(first).to be_a(Sentry::CheckInEvent)
expect(first.to_hash).to include(
expect(first.to_h).to include(
type: 'check_in',
check_in_id: check_in_id,
monitor_slug: "failed_job",
Expand All @@ -331,7 +331,7 @@ def perform

second = transport.events[1]
expect(second).to be_a(Sentry::CheckInEvent)
expect(second.to_hash).to include(
expect(second.to_h).to include(
:duration,
type: 'check_in',
check_in_id: check_in_id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@

expect(transport.events.count).to eq(1)

transaction = transport.events.last.to_hash
transaction = transport.events.last.to_h
breadcrumbs = transaction[:breadcrumbs][:values]
process_action_crumb = breadcrumbs.last
expect(process_action_crumb[:category]).to eq("process_action.action_controller")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@

expect(transport.events.count).to eq(1)

transaction = transport.events.last.to_hash
transaction = transport.events.last.to_h
breadcrumbs = transaction[:breadcrumbs][:values]
process_action_crumb = breadcrumbs.last
expect(process_action_crumb[:category]).to eq("process_action.action_controller")
Expand Down
6 changes: 3 additions & 3 deletions sentry-rails/spec/sentry/rails/controller_methods_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def request
event = transport.events.last
expect(event.message).to eq("foo")
expect(event.tags).to eq({ new_tag: true })
expect(event.to_hash.dig(:request, :url)).to eq("http://example.org/test")
expect(event.to_h.dig(:request, :url)).to eq("http://example.org/test")
end
end

Expand All @@ -49,8 +49,8 @@ def request

event = transport.events.last
expect(event.tags).to eq({ new_tag: true })
expect(event.to_hash.dig(:exception, :values, 0, :type)).to eq("ZeroDivisionError")
expect(event.to_hash.dig(:request, :url)).to eq("http://example.org/test")
expect(event.to_h.dig(:exception, :values, 0, :type)).to eq("ZeroDivisionError")
expect(event.to_h.dig(:request, :url)).to eq("http://example.org/test")
end
end
end
4 changes: 2 additions & 2 deletions sentry-rails/spec/sentry/rails/event_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
end

it "sets right SDK information" do
event_hash = Sentry::Rails.capture_message("foo").to_hash
event_hash = Sentry::Rails.capture_message("foo").to_h

expect(event_hash[:sdk]).to eq(name: "sentry.ruby.rails", version: Sentry::Rails::VERSION)
end
Expand All @@ -27,7 +27,7 @@
e
end

let(:hash) { Sentry::Rails.capture_exception(exception).to_hash }
let(:hash) { Sentry::Rails.capture_exception(exception).to_h }

it 'marks in_app correctly' do
frames = hash[:exception][:values][0][:stacktrace][:frames]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

expect(transport.events.count).to eq(1)

transaction = transport.events.first.to_hash
transaction = transport.events.first.to_h
expect(transaction[:type]).to eq("transaction")
expect(transaction[:spans].count).to eq(2)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

expect(transport.events.count).to eq(1)

transaction = transport.events.first.to_hash
transaction = transport.events.first.to_h
expect(transaction[:type]).to eq("transaction")
expect(transaction[:spans].count).to eq(2)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

expect(transport.events.count).to eq(1)

transaction = transport.events.first.to_hash
transaction = transport.events.first.to_h
expect(transaction[:type]).to eq("transaction")
expect(transaction[:spans].count).to eq(1)

Expand Down Expand Up @@ -67,7 +67,7 @@ def foo
it "doesn't record query's source location" do
expect(transport.events.count).to eq(1)

transaction = transport.events.first.to_hash
transaction = transport.events.first.to_h
expect(transaction[:type]).to eq("transaction")
expect(transaction[:spans].count).to eq(1)

Expand All @@ -86,7 +86,7 @@ def foo
it "records query's source location" do
expect(transport.events.count).to eq(1)

transaction = transport.events.first.to_hash
transaction = transport.events.first.to_h
expect(transaction[:type]).to eq("transaction")
expect(transaction[:spans].count).to eq(1)

Expand All @@ -104,7 +104,7 @@ def foo
it "doesn't record query's source location" do
expect(transport.events.count).to eq(1)

transaction = transport.events.first.to_hash
transaction = transport.events.first.to_h
expect(transaction[:type]).to eq("transaction")
expect(transaction[:spans].count).to eq(1)

Expand All @@ -131,7 +131,7 @@ def foo

expect(transport.events.count).to eq(1)

transaction = transport.events.first.to_hash
transaction = transport.events.first.to_h
expect(transaction[:type]).to eq("transaction")
expect(transaction[:spans].count).to eq(2)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
expect(response).to have_http_status(:ok)
expect(transport.events.count).to eq(2)

analysis_transaction = transport.events.first.to_hash
analysis_transaction = transport.events.first.to_h
expect(analysis_transaction[:type]).to eq("transaction")

if Rails.version.to_f > 6.1
Expand All @@ -40,7 +40,7 @@
expect(analysis_transaction[:spans][0][:origin]).to eq("auto.file.rails")
end

request_transaction = transport.events.last.to_hash
request_transaction = transport.events.last.to_h
expect(request_transaction[:type]).to eq("transaction")
expect(request_transaction[:spans].count).to eq(2)

Expand Down
8 changes: 4 additions & 4 deletions sentry-rails/spec/sentry/rails/tracing_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
expect(response).to have_http_status(:internal_server_error)
expect(transport.events.count).to eq(2)

event = transport.events.first.to_hash
transaction = transport.events.last.to_hash
event = transport.events.first.to_h
transaction = transport.events.last.to_h

expect(event.dig(:contexts, :trace, :trace_id).length).to eq(32)
expect(event.dig(:contexts, :trace, :trace_id)).to eq(transaction.dig(:contexts, :trace, :trace_id))
Expand Down Expand Up @@ -64,7 +64,7 @@
expect(response).to have_http_status(:ok)
expect(transport.events.count).to eq(1)

transaction = transport.events.last.to_hash
transaction = transport.events.last.to_h

expect(transaction[:type]).to eq("transaction")
expect(transaction.dig(:contexts, :trace, :op)).to eq("http.server")
Expand Down Expand Up @@ -254,7 +254,7 @@

expect(transport.events.count).to eq(3)

transaction = transport.events.last.to_hash
transaction = transport.events.last.to_h

expect(transaction[:type]).to eq("transaction")
expect(transaction[:transaction]).to eq("PostsController#show")
Expand Down
22 changes: 11 additions & 11 deletions sentry-resque/spec/sentry/resque_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def self.perform(msg)
process_job(worker)

expect(transport.events.count).to eq(1)
event = transport.events.last.to_hash
event = transport.events.last.to_h
expect(event[:message]).to eq("report")
expect(event[:tags]).to eq({ "resque.queue" => "default" })
expect(event[:contexts][:"Resque"]).to include({ job_class: "MessageJob", arguments: ["report"], queue: "default" })
Expand All @@ -88,7 +88,7 @@ def self.perform(msg)
process_job(worker)

expect(transport.events.count).to eq(1)
event = transport.events.last.to_hash
event = transport.events.last.to_h
expect(event[:message]).to eq("tagged report")
expect(event[:tags]).to include({ number: 1 })

Expand All @@ -97,7 +97,7 @@ def self.perform(msg)
process_job(worker)

expect(transport.events.count).to eq(2)
event = transport.events.last.to_hash
event = transport.events.last.to_h
expect(event[:tags]).to eq({ "resque.queue" => "default" })
end

Expand All @@ -109,7 +109,7 @@ def self.perform(msg)
end.to change { Resque::Stat.get("failed") }.by(1)

expect(transport.events.count).to eq(1)
event = transport.events.last.to_hash
event = transport.events.last.to_h

expect(event[:sdk]).to eq({ name: "sentry.ruby.resque", version: described_class::VERSION })
expect(event.dig(:exception, :values, 0, :type)).to eq("ZeroDivisionError")
Expand All @@ -123,7 +123,7 @@ def self.perform(msg)
end.to change { Resque::Stat.get("failed") }.by(1)

expect(transport.events.count).to eq(1)
event = transport.events.last.to_hash
event = transport.events.last.to_h

expect(event[:tags]).to eq({ "resque.queue" => "default", number: 1 })
expect(Sentry.get_current_scope.extra).to eq({})
Expand All @@ -135,7 +135,7 @@ def self.perform(msg)
end.to change { Resque::Stat.get("failed") }.by(1)

expect(transport.events.count).to eq(2)
event = transport.events.last.to_hash
event = transport.events.last.to_h
expect(event[:tags]).to eq({ "resque.queue" => "default" })
expect(Sentry.get_current_scope.extra).to eq({})
expect(Sentry.get_current_scope.tags).to eq({})
Expand All @@ -161,7 +161,7 @@ def self.perform(msg)
end
end.to change { transport.events.count }.by(1)

event = transport.events.last.to_hash
event = transport.events.last.to_h

expect(event[:sdk]).to eq({ name: "sentry.ruby.resque", version: described_class::VERSION })
expect(event.dig(:exception, :values, 0, :type)).to eq("ZeroDivisionError")
Expand All @@ -175,7 +175,7 @@ def self.perform(msg)
end.to change { Resque::Stat.get("failed") }.by(1)
.and change { transport.events.count }.by(1)

event = transport.events.last.to_hash
event = transport.events.last.to_h

expect(event[:sdk]).to eq({ name: "sentry.ruby.resque", version: described_class::VERSION })
expect(event.dig(:exception, :values, 0, :type)).to eq("ZeroDivisionError")
Expand All @@ -201,7 +201,7 @@ def self.perform(msg)
end
end.to change { transport.events.count }.by(3)

event = transport.events.last.to_hash
event = transport.events.last.to_h

expect(event[:sdk]).to eq({ name: "sentry.ruby.resque", version: described_class::VERSION })
expect(event.dig(:exception, :values, 0, :type)).to eq("ZeroDivisionError")
Expand Down Expand Up @@ -266,7 +266,7 @@ def perform
it "injects ActiveJob information to the event" do
expect(transport.events.count).to eq(1)

event = transport.events.last.to_hash
event = transport.events.last.to_h
expect(event[:message]).to eq("report from ActiveJob")
expect(event[:tags]).to match({ "resque.queue" => "default", number: 1 })
expect(event[:contexts][:"Active-Job"][:job_class]).to eq("AJMessageJob")
Expand All @@ -291,7 +291,7 @@ def perform
it "injects ActiveJob information to the event" do
expect(transport.events.count).to eq(1)

event = transport.events.last.to_hash
event = transport.events.last.to_h
expect(event.dig(:exception, :values, 0, :type)).to eq("ZeroDivisionError")
expect(event[:tags]).to match({ "resque.queue" => "default", number: 2 })
expect(event[:contexts][:"Active-Job"][:job_class]).to eq("AJFailedJob")
Expand Down
Loading

0 comments on commit d7522f9

Please sign in to comment.