Skip to content

Commit

Permalink
Merge pull request #2423 from ganmacs/enable-pretty-print-in-config
Browse files Browse the repository at this point in the history
Enable pretty print in config
  • Loading branch information
repeatedly authored May 31, 2019
2 parents 241a37f + 759be8c commit 71f223f
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 43 deletions.
96 changes: 53 additions & 43 deletions lib/fluent/plugin/in_monitor_agent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,46 +67,12 @@ def do_GET(req, res)
res.body = body
end

def build_object(req)
def build_object(req, opts)
unless req.path_info == ""
return render_json_error(404, "Not found")
end

qs = Hash.new { |_, _| [] }
# parse ?=query string
if req.query_string
begin
qs.merge!(CGI.parse(req.query_string))
rescue
return render_json_error(400, "Invalid query string")
end
end

# if ?debug=1 is set, set :with_debug_info for get_monitor_info
# and :pretty_json for render_json_error
opts = {}

if qs['debug'.freeze].first
opts[:with_debug_info] = true
opts[:pretty_json] = true
end

if ivars = qs['with_ivars'.freeze].first
opts[:ivars] = ivars.split(',')
end

if with_config = qs['with_config'.freeze].first
opts[:with_config] = Fluent::Config.bool_value(with_config)
else
opts[:with_config] = @agent.include_config
end

if with_retry = qs['with_retry'.freeze].first
opts[:with_retry] = Fluent::Config.bool_value(with_retry)
else
opts[:with_retry] = @agent.include_retry
end

qs = opts[:query]
if tag = qs['tag'.freeze].first
# ?tag= to search an output plugin by match pattern
if obj = @agent.plugin_info_by_tag(tag, opts)
Expand All @@ -129,7 +95,7 @@ def build_object(req)
list = @agent.plugins_info_all(opts)
end

[list, opts]
list
end

def render_json(obj, opts={})
Expand All @@ -144,11 +110,52 @@ def render_json_error(code, obj, opts={})
end
[code, {'Content-Type'=>'application/json'}, js]
end

private

def build_option(req)
qs = Hash.new { |_, _| [] }
# parse ?=query string
if req.query_string
begin
qs.merge!(CGI.parse(req.query_string))
rescue
return render_json_error(400, "Invalid query string")
end
end

# if ?debug=1 is set, set :with_debug_info for get_monitor_info
# and :pretty_json for render_json_error
opts = { query: qs }
if qs['debug'.freeze].first
opts[:with_debug_info] = true
opts[:pretty_json] = true
end

if ivars = qs['with_ivars'.freeze].first
opts[:ivars] = ivars.split(',')
end

if with_config = qs['with_config'.freeze].first
opts[:with_config] = Fluent::Config.bool_value(with_config)
else
opts[:with_config] = @agent.include_config
end

if with_retry = qs['with_retry'.freeze].first
opts[:with_retry] = Fluent::Config.bool_value(with_retry)
else
opts[:with_retry] = @agent.include_retry
end

opts
end
end

class LTSVMonitorServlet < MonitorServlet
def process(req)
list, _opts = build_object(req)
opts = build_option(req)
list = build_object(req, opts)
return unless list

normalized = JSON.parse(list.to_json)
Expand All @@ -171,7 +178,8 @@ def process(req)

class JSONMonitorServlet < MonitorServlet
def process(req)
list, opts = build_object(req)
opts = build_option(req)
list = build_object(req, opts)
return unless list

render_json({
Expand All @@ -181,7 +189,7 @@ def process(req)
end

class ConfigMonitorServlet < MonitorServlet
def build_object(req)
def build_object(req, _opt)
{
'pid' => Process.pid,
'ppid' => Process.ppid
Expand All @@ -191,7 +199,8 @@ def build_object(req)

class LTSVConfigMonitorServlet < ConfigMonitorServlet
def process(req)
result = build_object(req)
opts = build_option(req)
result = build_object(req, opts)

row = []
JSON.parse(result.to_json).each_pair { |k, v|
Expand All @@ -205,8 +214,9 @@ def process(req)

class JSONConfigMonitorServlet < ConfigMonitorServlet
def process(req)
result = build_object(req)
render_json(result)
opts = build_option(req)
result = build_object(req, opts)
render_json(result, opts)
end
end

Expand Down
13 changes: 13 additions & 0 deletions test/plugin/test_in_monitor_agent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,19 @@ def get(uri, header = {})
assert_equal(["/etc/fluent/plugin"], res["plugin_dirs"])
assert_nil(res["log_path"])
end

test "/api/config.json?debug=1" do
d = create_driver("
@type monitor_agent
bind '127.0.0.1'
port #{@port}
tag monitor
")
d.instance.start
# To check pretty print
assert_true !get("http://127.0.0.1:#{@port}/api/config.json").body.include?("\n")
assert_true get("http://127.0.0.1:#{@port}/api/config.json?debug=1").body.include?("\n")
end
end

sub_test_case "check retry of buffered plugins" do
Expand Down

0 comments on commit 71f223f

Please sign in to comment.