Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PoC: json_array switch to use json array data format for HTTP request body #2973

Merged
merged 5 commits into from
May 7, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 16 additions & 3 deletions lib/fluent/plugin/out_http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ class RetryableResponse < StandardError; end
config_param :proxy, :string, default: ENV['HTTP_PROXY'] || ENV['http_proxy']
desc 'Content-Type for HTTP request'
config_param :content_type, :string, default: nil
desc 'JSON array data format for HTTP request body'
config_param :json_array, :bool, default: false
desc 'Additional headers for HTTP request'
config_param :headers, :hash, default: nil

Expand Down Expand Up @@ -100,6 +102,13 @@ def configure(conf)
@proxy_uri = URI.parse(@proxy) if @proxy
@formatter = formatter_create
@content_type = setup_content_type unless @content_type

if @json_array
if @formatter_configs.first[:@type] != "json"
raise Fluent::ConfigError, "json_array option could be used with json formatter only"
rgeraskin marked this conversation as resolved.
Show resolved Hide resolved
end
define_singleton_method(:format, method(:format_json_array))
end
end

def multi_workers_ready?
Expand All @@ -114,6 +123,10 @@ def format(tag, time, record)
@formatter.format(tag, time, record)
end

def format_json_array(tag, time, record)
@formatter.format(tag, time, record).strip() << ","
rgeraskin marked this conversation as resolved.
Show resolved Hide resolved
end

def write(chunk)
uri = parse_endpoint(chunk)
req = create_request(chunk, uri)
Expand All @@ -128,7 +141,7 @@ def write(chunk)
def setup_content_type
case @formatter_configs.first[:@type]
when 'json'
'application/x-ndjson'
@json_array ? 'application/json' : 'application/x-ndjson'
when 'csv'
'text/csv'
when 'tsv', 'ltsv'
Expand Down Expand Up @@ -202,7 +215,7 @@ def create_request(chunk, uri)
req.basic_auth(@auth.username, @auth.password)
end
set_headers(req)
req.body = chunk.read
req.body = @json_array ? "[#{chunk.read[0...-1]}]" : chunk.read
rgeraskin marked this conversation as resolved.
Show resolved Hide resolved
req
end

Expand Down Expand Up @@ -234,4 +247,4 @@ def send_request(uri, req)
end
end
end
end
end
rgeraskin marked this conversation as resolved.
Show resolved Hide resolved