Skip to content

Commit

Permalink
Update YAML.load call to support ruby 3.1
Browse files Browse the repository at this point in the history
Because YAML.safe_load is now the default behavior

Ruby 3.1 is released, and YAML.safe_load becomes the default behavior of
YAML.load, which doesn't support aliases by default. This breaks when
AppSignal tries to read configuration file that contains aliases.

https://www.ruby-lang.org/en/news/2021/12/25/ruby-3-1-0-released/
  • Loading branch information
hieuk09 authored and tombruijn committed Jan 4, 2022
1 parent 7fddebd commit d7bfcdf
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 4 deletions.
6 changes: 6 additions & 0 deletions .changesets/add-ruby-3-1-0-support.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
bump: "patch"
type: "add"
---

Add Ruby 3.1.0 support. There was an issue with `YAML.load` arguments when parsing the `appsignal.yml` config file.
3 changes: 2 additions & 1 deletion lib/appsignal/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,8 @@ def detect_from_system
def load_from_disk
return if !config_file || !File.exist?(config_file)

configurations = YAML.load(ERB.new(IO.read(config_file)).result)
read_options = RUBY_VERSION >= "3.1.0" ? { :aliases => true } : {}
configurations = YAML.load(ERB.new(IO.read(config_file)).result, **read_options)
config_for_this_env = configurations[env]
if config_for_this_env
config_for_this_env =
Expand Down
6 changes: 5 additions & 1 deletion lib/appsignal/integrations/sidekiq.rb
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,11 @@ def parse_arguments(job)

# Based on: https://github.com/mperham/sidekiq/blob/63ee43353bd3b753beb0233f64865e658abeb1c3/lib/sidekiq/api.rb#L403-L412
def safe_load(content, default)
yield(*YAML.load(content))
if RUBY_VERSION >= "3.1.0"
yield(*YAML.unsafe_load(content))
else
yield(*YAML.load(content))
end
rescue => error
# Sidekiq issue #1761: in dev mode, it's possible to have jobs enqueued
# which haven't been loaded into memory yet so the YAML can't be
Expand Down
4 changes: 2 additions & 2 deletions spec/lib/appsignal/event_formatter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def format(_payload, _foo = nil)

class MockDependentFormatter < Appsignal::EventFormatter
def initialize
NonsenseDependency.something
raise "There is an error"
end

def format(_payload)
Expand Down Expand Up @@ -72,7 +72,7 @@ def format(_payload)
end
expect(klass.registered?("mock.dependent")).to be_falsy
expect(logs).to contains_log :error, \
"'uninitialized constant MockDependentFormatter::NonsenseDependency' " \
"'There is an error' " \
"when initializing mock.dependent event formatter"
end
end
Expand Down

0 comments on commit d7bfcdf

Please sign in to comment.