-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Port parser filter from fluent-plugin-parser. fix #1189 #1191
Merged
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
96664ac
Port parser filter from fluent-plugin-parser. fix #1189
repeatedly 0f14fa6
Apply v0.14 style. Use plugin helpers.
repeatedly a11d4fb
Add missing parser parameters to compat_parameters
repeatedly a0c8f7e
Call Timecop.return in teardown to restore Time
repeatedly b166787
Use filter_with_time API
repeatedly 9359eea
Remove duplicated compat parameters
repeatedly 8e8b92d
Use error stream instead of warning logs
repeatedly c993be5
Replace time_parse with reserve_time
repeatedly 5ffcb87
Update test to use new configuration format
repeatedly 2c82887
Use String#scrub instead of own routine
repeatedly 4aef481
Remove useless config_section definition
repeatedly 33cac26
Improve invalid string handling to reduce duplicated code
repeatedly d08a34d
Recent update removes ValuesParser from v0.14 parsers
repeatedly 5cb93a4
Use default_tag for driver run
repeatedly cb3dee6
Remove deleted paramters tests
repeatedly 163dc5c
Rename suppress warning test to unmached pattern
repeatedly 71d700c
Improve tests
repeatedly 82953c5
Forget to update test to use event_time instead of to_i
repeatedly f64bb77
Remove useless shutdown option from run
repeatedly File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
# | ||
# Fluentd | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
|
||
require 'fluent/time' | ||
require 'fluent/config/error' | ||
require 'fluent/plugin/filter' | ||
require 'fluent/plugin_helper/parser' | ||
require 'fluent/plugin_helper/compat_parameters' | ||
|
||
module Fluent::Plugin | ||
class ParserFilter < Filter | ||
Fluent::Plugin.register_filter('parser', self) | ||
|
||
helpers :parser, :compat_parameters | ||
|
||
config_param :key_name, :string | ||
config_param :reserve_data, :bool, default: false | ||
config_param :reserve_time, :bool, default: false | ||
config_param :inject_key_prefix, :string, default: nil | ||
config_param :replace_invalid_sequence, :bool, default: false | ||
config_param :hash_value_field, :string, default: nil | ||
|
||
attr_reader :parser | ||
|
||
def configure(conf) | ||
compat_parameters_convert(conf, :parser) | ||
|
||
super | ||
|
||
@parser = parser_create | ||
end | ||
|
||
FAILED_RESULT = [nil, nil].freeze # reduce allocation cost | ||
REPLACE_CHAR = '?'.freeze | ||
|
||
def filter_with_time(tag, time, record) | ||
raw_value = record[@key_name] | ||
if raw_value.nil? | ||
router.emit_error_event(tag, time, record, ArgumentError.new("#{@key_name} does not exist")) | ||
if @reserve_data | ||
return time, handle_parsed(tag, record, time, {}) | ||
else | ||
return FAILED_RESULT | ||
end | ||
end | ||
begin | ||
@parser.parse(raw_value) do |t, values| | ||
if values | ||
t = if @reserve_time | ||
time | ||
else | ||
t.nil? ? time : t | ||
end | ||
r = handle_parsed(tag, record, t, values) | ||
return t, r | ||
else | ||
router.emit_error_event(tag, time, record, Fluent::Plugin::Parser::ParserError.new("pattern not match with data '#{raw_value}'")) | ||
if @reserve_data | ||
t = time | ||
r = handle_parsed(tag, record, time, {}) | ||
return t, r | ||
else | ||
return FAILED_RESULT | ||
end | ||
end | ||
end | ||
rescue Fluent::Plugin::Parser::ParserError => e | ||
router.emit_error_event(tag, time, record, e) | ||
return FAILED_RESULT | ||
rescue ArgumentError => e | ||
raise unless @replace_invalid_sequence | ||
raise unless e.message.index("invalid byte sequence in") == 0 | ||
|
||
raw_value = raw_value.scrub(REPLACE_CHAR) | ||
retry | ||
rescue => e | ||
router.emit_error_event(tag, time, record, Fluent::Plugin::Parser::ParserError.new("parse failed #{e.message}")) | ||
return FAILED_RESULT | ||
end | ||
end | ||
|
||
private | ||
|
||
def handle_parsed(tag, record, t, values) | ||
if values && @inject_key_prefix | ||
values = Hash[values.map { |k, v| [@inject_key_prefix + k, v] }] | ||
end | ||
r = @hash_value_field ? {@hash_value_field => values} : values | ||
if @reserve_data | ||
r = r ? record.merge(r) : record | ||
end | ||
r | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are there any cases NOT to return values without ParserError?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure but we can't say all existing parsers don't return nil for time and value.
So keeping this behaviour is better for users.