Skip to content

Commit

Permalink
Merge pull request #2025 from fluent/in_http-prefer-time-key-for-batch
Browse files Browse the repository at this point in the history
in_http: Prefer parser's time_key setting in batch. fix #2018
  • Loading branch information
repeatedly authored Jun 25, 2018
2 parents e888474 + 39a0a1a commit 048d30f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 7 deletions.
19 changes: 12 additions & 7 deletions lib/fluent/plugin/in_http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,16 @@ def configure(conf)
@parser_json = parser_create(usage: 'parser_in_http_json', type: 'json')
@parser_json.estimate_current_event = false
@format_name = 'default'
@parser_time_key = if parser_config = conf.elements('parse').first
parser_config['time_key'] || 'time'
else
'time'
end
method(:parse_params_default)
else
@parser = parser_create
@format_name = @parser_configs.first['@type']
@parser_time_key = @parser.time_key
method(:parse_params_with_parser)
end
self.singleton_class.module_eval do
Expand Down Expand Up @@ -203,14 +209,13 @@ def on_request(path_info, params)
single_record['REMOTE_ADDR'] = params['REMOTE_ADDR']
end

if single_record.has_key?('time')
single_time = Fluent::EventTime.from_time(Time.at(single_record['time']))

unless @parser and @parser.keep_time_key
single_record.delete('time')
end
if single_record.has_key?(@parser_time_key)
single_time = Fluent::EventTime.from_time(Time.at(single_record[@parser_time_key]))
unless @parser and @parser.keep_time_key
single_record.delete(@parser_time_key)
end
else
single_time = time
single_time = time
end

mes.add(single_time, single_record)
Expand Down
23 changes: 23 additions & 0 deletions test/plugin/test_in_http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,29 @@ def test_multi_json_with_time_field
assert_equal_event_time time, d.events[1][1]
end

def test_multi_json_with_nonexistent_time_key
d = create_driver(CONFIG + %[
<parse>
time_key missing
</parse>
])
time = event_time("2011-01-02 13:14:15 UTC")
time_i = time.to_i
time_f = time.to_f

records = [{"a" => 1, 'time' => time_i},{"a" => 2, 'time' => time_f}]
tag = "tag1"
res_codes = []
d.run(expect_records: 2, timeout: 5) do
res = post("/#{tag}", {"json" => records.to_json})
res_codes << res.code
end
assert_equal ["200"], res_codes
assert_equal 2, d.events.size
assert_not_equal time_i, d.events[0][1].sec # current time is used because "missing" field doesn't exist
assert_not_equal time_i, d.events[1][1].sec
end

def test_json_with_add_remote_addr
d = create_driver(CONFIG + "add_remote_addr true")
time = event_time("2011-01-02 13:14:15 UTC")
Expand Down

0 comments on commit 048d30f

Please sign in to comment.