Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

added path_key to include read path into the event under this key #281

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions lib/fluent/plugin/in_tail.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def initialize
end

config_param :path, :string
config_param :path_key, :string, :default => nil
config_param :tag, :string
config_param :rotate_wait, :time, :default => 5
config_param :pos_file, :string, :default => nil
Expand Down Expand Up @@ -126,11 +127,12 @@ def parse_line(line)
return @parser.parse(line)
end

def convert_line_to_event(line, es)
def convert_line_to_event(line, es, tail_watcher)
begin
line.chomp! # remove \n
time, record = parse_line(line)
if time && record
record[@path_key] ||= tail_watcher.path unless @path_key.nil?
es.add(time, record)
else
log.warn "pattern not match: #{line.inspect}"
Expand All @@ -144,7 +146,7 @@ def convert_line_to_event(line, es)
def parse_singleline(lines, tail_watcher)
es = MultiEventStream.new
lines.each { |line|
convert_line_to_event(line, es)
convert_line_to_event(line, es, tail_watcher)
}
es
end
Expand All @@ -155,7 +157,7 @@ def parse_multilines(lines, tail_watcher)
lines.each { |line|
if @parser.parser.firstline?(line)
if lb
convert_line_to_event(lb, es)
convert_line_to_event(lb, es, tail_watcher)
end
lb = line
else
Expand Down
27 changes: 27 additions & 0 deletions test/plugin/in_tail.rb
Original file line number Diff line number Diff line change
Expand Up @@ -203,4 +203,31 @@ def test_multilinelog_with_multiple_paths
assert_equal({"message" => "test4"}, emits[2][2])
assert_equal({"message" => "test4"}, emits[3][2])
end

def test_path_key
File.open("#{TMP_DIR}/tail.txt", "w") { |f| }

d = create_driver(%[
path #{TMP_DIR}/tail.txt
tag t1
format /(?<message>.*)/
path_key path
], false)

d.run do
sleep 1

File.open("#{TMP_DIR}/tail.txt", "a") {|f|
f.puts "test1"
f.puts "test2"
}
sleep 1
end

emits = d.emits
assert_equal(true, emits.length > 0)
assert_equal({"message"=>"test1", "path"=>"#{TMP_DIR}/tail.txt"}, emits[0][2])
assert_equal({"message"=>"test2", "path"=>"#{TMP_DIR}/tail.txt"}, emits[1][2])
end

end