From ebd5797fc85c89674f0d3eb8e6c807f9ba5ee5fc Mon Sep 17 00:00:00 2001 From: Masahiro Nakagawa Date: Wed, 10 Jan 2018 19:58:27 +0900 Subject: [PATCH 1/2] config: Add hostname and worker_id short-cut. fix #1473 --- lib/fluent/config/literal_parser.rb | 3 +++ test/command/test_fluentd.rb | 4 ++-- test/config/test_literal_parser.rb | 8 ++++++++ 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/lib/fluent/config/literal_parser.rb b/lib/fluent/config/literal_parser.rb index 3af66d54c3..2b99a97457 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,8 @@ def eval_embedded_code(code) if @eval_context.nil? parse_error! "embedded code is not allowed in this file" end + hostname = Socket.gethostname + worker_id = ENV['SERVERENGINE_WORKER_ID'] || '' @eval_context.instance_eval(code) end diff --git a/test/command/test_fluentd.rb b/test/command/test_fluentd.rb index f47fdfdc3b..2fcbd4f86c 100644 --- a/test/command/test_fluentd.rb +++ b/test/command/test_fluentd.rb @@ -494,14 +494,14 @@ def assert_fluentd_fails_to_start(cmdline, *pattern_list, timeout: 10) end test 'success to start the number of workers specified in configuration' do - conf = < 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 From e55b22764c0d363bcab558cf2ade91568dfe88fc Mon Sep 17 00:00:00 2001 From: Masahiro Nakagawa Date: Tue, 16 Jan 2018 10:54:19 +0900 Subject: [PATCH 2/2] Add hostname and worker_id to code to suppress warnings --- lib/fluent/config/literal_parser.rb | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/fluent/config/literal_parser.rb b/lib/fluent/config/literal_parser.rb index 2b99a97457..b2468a3f10 100644 --- a/lib/fluent/config/literal_parser.rb +++ b/lib/fluent/config/literal_parser.rb @@ -163,8 +163,11 @@ def eval_embedded_code(code) if @eval_context.nil? parse_error! "embedded code is not allowed in this file" end - hostname = Socket.gethostname - worker_id = ENV['SERVERENGINE_WORKER_ID'] || '' + # Add hostname and worker_id to code for preventing unused warnings + code = <