diff --git a/lib/fluent/config/literal_parser.rb b/lib/fluent/config/literal_parser.rb index 3af66d54c3..b2468a3f10 100644 --- a/lib/fluent/config/literal_parser.rb +++ b/lib/fluent/config/literal_parser.rb @@ -18,6 +18,7 @@ require 'json' require 'yajl' +require 'socket' require 'irb/ruby-lex' # RubyLex require 'fluent/config/basic_parser' @@ -162,6 +163,11 @@ def eval_embedded_code(code) if @eval_context.nil? parse_error! "embedded code is not allowed in this file" end + # Add hostname and worker_id to code for preventing unused warnings + code = < workers 2 root_dir #{@root_path} @type dummy - @id dummy + @id "dummy#{worker_id}" # check worker_id works or not with actual command @label @dummydata tag dummy dummy {"message": "yay!"} diff --git a/test/config/test_literal_parser.rb b/test/config/test_literal_parser.rb index ae8d0e9cf1..212d470377 100644 --- a/test/config/test_literal_parser.rb +++ b/test/config/test_literal_parser.rb @@ -232,6 +232,14 @@ def test_falseX test("\"\#{\n=begin\n}\"") { assert_parse_error("\"\#{\n=begin\n}\"") } # error in embedded ruby code test('"#{v1}foo#{v2}"') { assert_text_parsed_as("#{v1}foo#{v2}", '"#{v1}foo#{v2}"') } test('"#{1+1}foo#{2+2}bar"') { assert_text_parsed_as("#{1+1}foo#{2+2}bar", '"#{1+1}foo#{2+2}bar"') } + test('"foo#{hostname}"') { assert_text_parsed_as("foo#{Socket.gethostname}", '"foo#{hostname}"') } + test('"foo#{worker_id}"') { + ENV.delete('SERVERENGINE_WORKER_ID') + assert_text_parsed_as("foo", '"foo#{worker_id}"') + ENV['SERVERENGINE_WORKER_ID'] = '1' + assert_text_parsed_as("foo1", '"foo#{worker_id}"') + ENV.delete('SERVERENGINE_WORKER_ID') + } end sub_test_case 'array parsing' do