From fb523bf18c91acc56dd061ad6aa788e7dd0e8a19 Mon Sep 17 00:00:00 2001 From: autopp Date: Wed, 28 Dec 2016 22:09:45 +0900 Subject: [PATCH 1/3] add query parameter with_ivars to in_monitor_agent --- lib/fluent/plugin/in_monitor_agent.rb | 11 +++++++++++ test/plugin/test_in_monitor_agent.rb | 10 ++++++---- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/lib/fluent/plugin/in_monitor_agent.rb b/lib/fluent/plugin/in_monitor_agent.rb index d63237dbe9..7d87f15657 100644 --- a/lib/fluent/plugin/in_monitor_agent.rb +++ b/lib/fluent/plugin/in_monitor_agent.rb @@ -84,6 +84,10 @@ def build_object(req, res) opts[:pretty_json] = true end + if ivars = (qs['with_ivars'] || []).first + opts[:ivars] = ivars.split(',') + end + if with_config = get_search_parameter(qs, 'with_config'.freeze) opts[:with_config] = Fluent::Config.bool_value(with_config) end @@ -357,6 +361,13 @@ def get_monitor_info(pe, opts={}) } end obj['instance_variables'] = iv + elsif ivars = opts[:ivars] + iv = {} + ivars.each {|name| + iname = :"@#{name}" + iv[name] = pe.instance_variable_get(iname) if pe.instance_variable_defined?(iname) + } + obj['instance_variables'] = iv end obj diff --git a/test/plugin/test_in_monitor_agent.rb b/test/plugin/test_in_monitor_agent.rb index 2f7f2bb2e1..fb6df0971b 100644 --- a/test/plugin/test_in_monitor_agent.rb +++ b/test/plugin/test_in_monitor_agent.rb @@ -311,10 +311,10 @@ def get(uri, header = {}) assert_equal(expected_null_response, null_response) end - data(:with_config_yes => [true, "?with_config=yes"], - :with_config_no => [false, "?with_config=no"]) - test "/api/plugins.json with query parameter. query parameter is preferred than include_config" do |(with_config, query_param)| - + data(:with_config_yes => [true, false, "?with_config=yes"], + :with_config_no => [false, false, "?with_config=no"], + :with_ivars => [false, true, "?with_config=no&with_ivars=id,num_errors"]) + test "/api/plugins.json with query parameter. query parameter is preferred than include_config" do |(with_config, with_ivars, query_param)| d = create_driver(" @type monitor_agent bind '127.0.0.1' @@ -330,6 +330,7 @@ def get(uri, header = {}) "type" => "test_in" } expected_test_in_response.merge!("config" => {"@id" => "test_in", "@type" => "test_in"}) if with_config + expected_test_in_response.merge!("instance_variables" => {"id" => "test_in" }) if with_ivars expected_null_response = { "buffer_queue_length" => 0, "buffer_total_queued_size" => 0, @@ -340,6 +341,7 @@ def get(uri, header = {}) "type" => "null" } expected_null_response.merge!("config" => {"@id" => "null", "@type" => "null"}) if with_config + expected_null_response.merge!("instance_variables" => {"id" => "null", "num_errors" => 0 }) if with_ivars response = JSON.parse(get("http://127.0.0.1:#{@port}/api/plugins.json#{query_param}")) test_in_response = response["plugins"][0] null_response = response["plugins"][5] From 1b05faf5cf7a35e87b44fb558315f026c55aadc4 Mon Sep 17 00:00:00 2001 From: autopp Date: Wed, 4 Jan 2017 11:28:33 +0900 Subject: [PATCH 2/3] separate test case for with_ivar --- test/plugin/test_in_monitor_agent.rb | 44 +++++++++++++++++++++++----- 1 file changed, 37 insertions(+), 7 deletions(-) diff --git a/test/plugin/test_in_monitor_agent.rb b/test/plugin/test_in_monitor_agent.rb index abbb8ce587..24ffeb7e98 100644 --- a/test/plugin/test_in_monitor_agent.rb +++ b/test/plugin/test_in_monitor_agent.rb @@ -313,10 +313,10 @@ def get(uri, header = {}) assert_equal(expected_null_response, null_response) end - data(:with_config_and_retry_yes => [true, true, false, "?with_config=yes&with_retry"], - :with_config_and_retry_no => [false, false, false, "?with_config=no&with_retry=no"], - :with_ivars_given => [false, false, true, "?with_config=no&with_retry=no&with_ivars=id,num_errors"]) - test "/api/plugins.json with query parameter. query parameter is preferred than include_config" do |(with_config, with_retry, with_ivars, query_param)| + data(:with_config_and_retry_yes => [true, true, "?with_config=yes&with_retry"], + :with_config_and_retry_no => [false, false, "?with_config=no&with_retry=no"]) + test "/api/plugins.json with query parameter. query parameter is preferred than include_config" do |(with_config, with_retry, query_param)| + d = create_driver(" @type monitor_agent bind '127.0.0.1' @@ -332,7 +332,6 @@ def get(uri, header = {}) "type" => "test_in" } expected_test_in_response.merge!("config" => {"@id" => "test_in", "@type" => "test_in"}) if with_config - expected_test_in_response.merge!("instance_variables" => {"id" => "test_in" }) if with_ivars expected_null_response = { "buffer_queue_length" => 0, "buffer_total_queued_size" => 0, @@ -344,8 +343,6 @@ def get(uri, header = {}) } expected_null_response.merge!("config" => {"@id" => "null", "@type" => "null"}) if with_config expected_null_response.merge!("retry" => {}) if with_retry - expected_null_response.merge!("instance_variables" => {"id" => "null", "num_errors" => 0 }) if with_ivars - response = JSON.parse(get("http://127.0.0.1:#{@port}/api/plugins.json#{query_param}")) test_in_response = response["plugins"][0] null_response = response["plugins"][5] @@ -353,6 +350,39 @@ def get(uri, header = {}) assert_equal(expected_null_response, null_response) end + test "/api/plugins.json with 'with_ivars'. response contains specified instance variables of each plugin" do + d = create_driver(" + @type monitor_agent + bind '127.0.0.1' + port #{@port} + tag monitor +") + d.instance.start + expected_test_in_response = { + "output_plugin" => false, + "plugin_category" => "input", + "plugin_id" => "test_in", + "retry_count" => nil, + "type" => "test_in", + "instance_variables" => {"id" => "test_in"} + } + expected_null_response = { + "buffer_queue_length" => 0, + "buffer_total_queued_size" => 0, + "output_plugin" => true, + "plugin_category" => "output", + "plugin_id" => "null", + "retry_count" => 0, + "type" => "null", + "instance_variables" => {"id" => "null", "num_errors" => 0} + } + response = JSON.parse(get("http://127.0.0.1:#{@port}/api/plugins.json?with_config=no&with_retry=no&with_ivars=id,num_errors")) + test_in_response = response["plugins"][0] + null_response = response["plugins"][5] + assert_equal(expected_test_in_response, test_in_response) + assert_equal(expected_null_response, null_response) + end + test "/api/config" do d = create_driver(" @type monitor_agent From 64088dff4e7fd2b111242ede9474468bf78ec4b5 Mon Sep 17 00:00:00 2001 From: autopp Date: Wed, 4 Jan 2017 21:57:52 +0900 Subject: [PATCH 3/3] use string instead of symbol for instance variable --- lib/fluent/plugin/in_monitor_agent.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/fluent/plugin/in_monitor_agent.rb b/lib/fluent/plugin/in_monitor_agent.rb index d5752b2860..6b9127c038 100644 --- a/lib/fluent/plugin/in_monitor_agent.rb +++ b/lib/fluent/plugin/in_monitor_agent.rb @@ -371,7 +371,7 @@ def get_monitor_info(pe, opts={}) elsif ivars = opts[:ivars] iv = {} ivars.each {|name| - iname = :"@#{name}" + iname = "@#{name}" iv[name] = pe.instance_variable_get(iname) if pe.instance_variable_defined?(iname) } obj['instance_variables'] = iv