diff --git a/lib/fluent/plugin/in_http.rb b/lib/fluent/plugin/in_http.rb index 9552e7e8d6..efff85bbb9 100644 --- a/lib/fluent/plugin/in_http.rb +++ b/lib/fluent/plugin/in_http.rb @@ -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 @@ -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) diff --git a/test/plugin/test_in_http.rb b/test/plugin/test_in_http.rb index 9e8c6bbde8..f3442a6b33 100644 --- a/test/plugin/test_in_http.rb +++ b/test/plugin/test_in_http.rb @@ -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_time_key + d = create_driver(CONFIG + %[ + + time_key missing + + ]) + 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")