-
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
Add from_encoding parameter to in_tail plugin #1067
Changes from 2 commits
d3d0ebf
94582d4
6da73b9
100abf8
0a5b8ef
3e0fb5a
46a54e1
0696130
57cb9da
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 |
---|---|---|
|
@@ -64,14 +64,10 @@ def initialize | |
config_param :multiline_flush_interval, :time, default: nil | ||
desc 'Enable the additional watch timer.' | ||
config_param :enable_watch_timer, :bool, default: true | ||
desc 'The encoding after conversion of the input.' | ||
config_param :encoding, default: nil | ||
desc 'The encoding of the input.' | ||
config_param :encoding, default: nil do |encoding_name| | ||
begin | ||
Encoding.find(encoding_name) | ||
rescue ArgumentError => e | ||
raise ConfigError, e.message | ||
end | ||
end | ||
config_param :from_encoding, default: nil | ||
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. ditto. |
||
desc 'Add the log path being tailed to records. Specify the field name to be used.' | ||
config_param :path_key, :string, default: nil | ||
|
||
|
@@ -92,6 +88,8 @@ def configure(conf) | |
|
||
configure_parser(conf) | ||
configure_tag | ||
@encoding = parse_encoding_param(conf['encoding']) | ||
@from_encoding = parse_encoding_param(conf['from_encoding']) | ||
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's bad practice to get values by 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.
|
||
|
||
@multiline_mode = conf['format'] =~ /multiline/ | ||
@receive_handler = if @multiline_mode | ||
|
@@ -117,6 +115,14 @@ def configure_tag | |
end | ||
end | ||
|
||
def parse_encoding_param(encoding_name) | ||
begin | ||
Encoding.find(encoding_name) if encoding_name | ||
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 seems that it's invalid to call this method with |
||
rescue ArgumentError => e | ||
raise ConfigError, e.message | ||
end | ||
end | ||
|
||
def start | ||
super | ||
|
||
|
@@ -251,7 +257,11 @@ def close_watcher_after_rotate_wait(tw) | |
def flush_buffer(tw) | ||
if lb = tw.line_buffer | ||
lb.chomp! | ||
lb.force_encoding(@encoding) if @encoding | ||
if @encoding && @from_encoding | ||
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. Here is microbenchmark result. I think default should be fast. So
Code: require 'benchmark'
class Foo
def initialize(enc, from_enc)
@enc = enc
@from_enc = from_enc
end
def f1(num)
num.times {
if @enc && @from_enc
1
elsif @enc
2
end
}
end
def f2(num)
num.times {
if @enc
if @from_enc
1
else
2
end
end
}
end
end
n = 20000000
Benchmark.bmbm do |x|
x.report('f1 with nil / nil') {
foo = Foo.new(nil, nil)
foo.f1(n)
}
x.report('f2 with nil / nil') {
foo = Foo.new(nil, nil)
foo.f2(n)
}
x.report('f1 with true / true') {
foo = Foo.new(true, true)
foo.f1(n)
}
x.report('f2 with true / true') {
foo = Foo.new(true, true)
foo.f2(n)
}
x.report('f1 with true / false') {
foo = Foo.new(true, false)
foo.f1(n)
}
x.report('f2 with true / false') {
foo = Foo.new(true, false)
foo.f2(n)
}
end 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. Thank you for microbenchmark result! |
||
lb.encode!(@encoding, @from_encoding) | ||
elsif @encoding | ||
lb.force_encoding(@encoding) | ||
end | ||
@parser.parse(lb) { |time, record| | ||
if time && record | ||
tag = if @tag_prefix || @tag_suffix | ||
|
@@ -300,7 +310,11 @@ def receive_lines(lines, tail_watcher) | |
def convert_line_to_event(line, es, tail_watcher) | ||
begin | ||
line.chomp! # remove \n | ||
line.force_encoding(@encoding) if @encoding | ||
if @encoding && @from_encoding | ||
line.encode!(@encoding, @from_encoding) | ||
elsif @encoding | ||
line.force_encoding(@encoding) | ||
end | ||
@parser.parse(line) { |time, record| | ||
if time && record | ||
record[@path_key] ||= tail_watcher.path unless @path_key.nil? | ||
|
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.
Specify type of this parameter.