Skip to content

Commit

Permalink
Add backtrace line 0 to 3.4 ERB failure logs
Browse files Browse the repository at this point in the history
Ruby 3.4 no longer provides the line number in the exception message
for the error raised by a failure to parse ERB in the newrelic.yml file.

To help our customers with debugging, add the line number to the output
by printing the first line of the backtrace for the exception to the
agent logs

Resolves #2902
  • Loading branch information
kaylareopelle committed Dec 11, 2024
1 parent 51678b8 commit 5840115
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 14 deletions.
5 changes: 4 additions & 1 deletion lib/new_relic/agent/configuration/yaml_source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,10 @@ def process_erb(file)
file.gsub!(/^\s*#.*$/, '#')
ERB.new(file).result(binding)
rescue ScriptError, StandardError => e
log_failure('Failed ERB processing configuration file. This is typically caused by a Ruby error in <% %> templating blocks in your newrelic.yml file.', e)
message = 'Failed ERB processing configuration file. This is typically caused by a Ruby error in <% %> templating blocks in your newrelic.yml file.'
failure_array = [message, e]
failure_array << e.backtrace[0] if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('3.4.0')
log_failure(*failure_array)
nil
end
end
Expand Down
11 changes: 3 additions & 8 deletions test/multiverse/suites/config_file_loading/Envfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,10 @@

omit_collector!

# TODO: RUBY 3.4
# The CI has a prism-related error when it tries to run this suite
# The problem may be a bug fixed in future preview releases
# Disable ths suite for now, and try again when the next version
# is out.
PSYCH_VERSIONS = [
[nil, 2.4, 3.3],
['4.0.0', 2.4, 3.3],
['3.3.0', 2.4, 3.3]
[nil],
['4.0.0'],
['3.3.0']
]

def stringio_version
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,6 @@ def test_warning_logged_when_config_file_yaml_parsing_error
assert_log_contains(log, /ERROR.*Failed to read or parse configuration file at config\/newrelic\.yml/)
end

# TODO: RUBY 3.4 SUPPORT
# Both error class and output have changes in Ruby 3.4
# See Issue: https://github.com/newrelic/newrelic-ruby-agent/issues/2902
unless Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('3.4.0')
def test_warning_logged_when_config_file_erb_error
path = File.join(@cwd, 'config', 'newrelic.yml')
setup_config(path, {}, "\n\n\n<%= this is not ruby %>") # the error is on line 4
Expand All @@ -172,7 +168,6 @@ def test_warning_logged_when_config_file_erb_error
assert_log_contains(log, /ERROR.*Failed ERB processing/)
assert_log_contains(log, /\(erb\):4/)
end
end

def test_exclude_commented_out_erb_lines
config_contents = <<~YAML
Expand Down

0 comments on commit 5840115

Please sign in to comment.