-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Make sure JSON parser returns Hash #4106
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -203,54 +203,24 @@ def on_request(path_info, params) | |
| begin | ||
| path = path_info[1..-1] # remove / | ||
| tag = path.split('/').join('.') | ||
| record_time, record = parse_params(params) | ||
|
|
||
| # Skip nil record | ||
| if record.nil? | ||
| log.debug { "incoming event is invalid: path=#{path_info} params=#{params.to_json}" } | ||
| if @respond_with_empty_img | ||
| return RESPONSE_IMG | ||
| else | ||
| if @use_204_response | ||
| return RESPONSE_204 | ||
| else | ||
| return RESPONSE_200 | ||
| end | ||
| mes = Fluent::MultiEventStream.new | ||
| parse_params(params) do |record_time, record| | ||
| if record.nil? | ||
| log.debug { "incoming event is invalid: path=#{path_info} params=#{params.to_json}" } | ||
| next | ||
| end | ||
| end | ||
|
|
||
| mes = nil | ||
| # Support batched requests | ||
| if record.is_a?(Array) | ||
| mes = Fluent::MultiEventStream.new | ||
| record.each do |single_record| | ||
| add_params_to_record(single_record, params) | ||
|
|
||
| if param_time = params['time'] | ||
| param_time = param_time.to_f | ||
| single_time = param_time.zero? ? Fluent::EventTime.now : @float_time_parser.parse(param_time) | ||
| elsif @custom_parser | ||
| single_time = @custom_parser.parse_time(single_record) | ||
| single_time, single_record = @custom_parser.convert_values(single_time, single_record) | ||
| else | ||
| single_time = convert_time_field(single_record) | ||
| end | ||
|
|
||
| mes.add(single_time, single_record) | ||
| end | ||
| else | ||
| add_params_to_record(record, params) | ||
|
|
||
| time = if param_time = params['time'] | ||
| param_time = param_time.to_f | ||
| param_time.zero? ? Fluent::EventTime.now : @float_time_parser.parse(param_time) | ||
| else | ||
| if record_time.nil? | ||
| convert_time_field(record) | ||
| else | ||
| record_time | ||
| end | ||
| record_time.nil? ? convert_time_field(record) : record_time | ||
| end | ||
|
|
||
| mes.add(time, record) | ||
| end | ||
| rescue => e | ||
| if @dump_error_log | ||
|
|
@@ -261,11 +231,7 @@ def on_request(path_info, params) | |
|
|
||
| # TODO server error | ||
| begin | ||
| if mes | ||
| router.emit_stream(tag, mes) | ||
| else | ||
| router.emit(tag, time, record) | ||
| end | ||
| router.emit_stream(tag, mes) unless mes.empty? | ||
| rescue => e | ||
| if @dump_error_log | ||
| log.error "failed to emit data", error: e | ||
|
|
@@ -308,31 +274,37 @@ def on_server_connect(conn) | |
| def parse_params_default(params) | ||
| if msgpack = params['msgpack'] | ||
| @parser_msgpack.parse(msgpack) do |_time, record| | ||
| return nil, record | ||
| if record.is_a?(Array) | ||
| # TODO: Temporarily supporting this case for compatibility. | ||
| # We should not consider this case here. | ||
| # We should fix MessagePackParser so that it doesn't return Array. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why don't you do this in this PR?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought it should be summarized as fixing the JSON parser. If it looks like I should fix it in this PR, I will try today or tomorrow.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It should be done in this PR instead of adding such workaround, since it's considered as a same problem and sometimes such workaround lives long unexpectedly. I’ll postpone merging this to v1.17.0.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for your review. I Understood. |
||
| record.each do |single_record| | ||
| yield nil, single_record | ||
| end | ||
| else | ||
| yield nil, record | ||
| end | ||
| end | ||
| elsif js = params['json'] | ||
| @parser_json.parse(js) do |_time, record| | ||
| return nil, record | ||
| yield nil, record | ||
| end | ||
| elsif ndjson = params['ndjson'] | ||
| events = [] | ||
| ndjson.split(/\r?\n/).each do |js| | ||
| @parser_json.parse(js) do |_time, record| | ||
| events.push(record) | ||
| yield nil, record | ||
| end | ||
| end | ||
| return nil, events | ||
| else | ||
| raise "'json', 'ndjson' or 'msgpack' parameter is required" | ||
| end | ||
| end | ||
|
|
||
| def parse_params_with_parser(params) | ||
| if content = params[EVENT_RECORD_PARAMETER] | ||
| @custom_parser.parse(content) { |time, record| | ||
| raise "Received event is not #{@format_name}: #{content}" if record.nil? | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Explanation of the diff There did not seem to be any particular reason for |
||
| return time, record | ||
| } | ||
| @custom_parser.parse(content) do |time, record| | ||
| yield time, record | ||
| end | ||
| else | ||
| raise "'#{EVENT_RECORD_PARAMETER}' parameter is required" | ||
| end | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.