Skip to content

Commit

Permalink
feat: added support for query params in in_http plugin
Browse files Browse the repository at this point in the history
Signed-off-by: omerlh <omer.levihevroni@snyk.io>
  • Loading branch information
omerlh committed Dec 10, 2020
1 parent 3c85807 commit 55357ea
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
17 changes: 17 additions & 0 deletions lib/fluent/plugin/in_http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ class HttpInput < Input
config_param :use_204_response, :bool, default: false
desc 'Dump error log or not'
config_param :dump_error_log, :bool, default: true
desc 'Add QUERY_ prefix query params to record'
config_param :add_query_params, :bool, default: false

config_section :parse do
config_set_default :@type, 'in_http'
Expand Down Expand Up @@ -326,6 +328,14 @@ def add_params_to_record(record, params)
}
end

if @add_query_params
params.each_pair { |k, v|
if k.start_with?("QUERY_".freeze)
record[k] = v
end
}
end

if @add_remote_addr
record['REMOTE_ADDR'] = params['REMOTE_ADDR']
end
Expand Down Expand Up @@ -533,7 +543,14 @@ def on_message_complete
end
path_info = uri.path

query_params = WEBrick::HTTPUtils.parse_query(uri.query)

query_params.each_pair {|k,v|
params["QUERY_#{k.gsub('-','_').upcase}"] = v
}

params.merge!(@env)

@env.clear

code, header, body = @callback.call(path_info, params)
Expand Down
25 changes: 25 additions & 0 deletions test/plugin/test_in_http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def test_configure
assert_equal 10*1024*1024, d.instance.body_size_limit
assert_equal 5, d.instance.keepalive_timeout
assert_equal false, d.instance.add_http_headers
assert_equal false, d.instance.add_query_params
end

def test_time
Expand Down Expand Up @@ -907,6 +908,30 @@ def test_cors_disallowed
assert_equal ["403", "403"], res_codes
end

def test_add_query_params
d = create_driver(CONFIG + "add_query_params true")
assert_equal true, d.instance.add_query_params

time = event_time("2011-01-02 13:14:15 UTC")
time_i = time.to_i
events = [
["tag1", time, {"a"=>1, "QUERY_A"=>"b"}],
["tag2", time, {"a"=>2, "QUERY_A"=>"b"}],
]
res_codes = []
res_bodies = []

d.run do
events.each do |tag, _t, record|
res = post("/#{tag}?a=b", {"json"=>record.to_json, "time"=>time_i.to_s})
res_codes << res.code
end
end
assert_equal ["200", "200"], res_codes
assert_equal [], res_bodies
assert_equal events, d.events
end

$test_in_http_connection_object_ids = []
$test_in_http_content_types = []
$test_in_http_content_types_flag = false
Expand Down

0 comments on commit 55357ea

Please sign in to comment.