Skip to content

Commit

Permalink
Merge pull request #1976 from fluent/fix-source_hostname_key
Browse files Browse the repository at this point in the history
in_udp/in_tcp: Fix source_hostname_key
  • Loading branch information
repeatedly authored May 9, 2018
2 parents b930a6e + 553636e commit b76c9d2
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/fluent/plugin/in_tcp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def multi_workers_ready?
def start
super

server_create(:in_tcp_server, @port, bind: @bind) do |data, conn|
server_create(:in_tcp_server, @port, bind: @bind, resolve_name: !!@source_hostname_key) do |data, conn|
conn.buffer << data
begin
pos = 0
Expand Down
2 changes: 1 addition & 1 deletion lib/fluent/plugin/in_udp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def start
super

log.info "listening udp socket", bind: @bind, port: @port
server_create(:in_udp_server, @port, proto: :udp, bind: @bind, max_bytes: @message_length_limit, receive_buffer_size: @receive_buffer_size) do |data, sock|
server_create(:in_udp_server, @port, proto: :udp, bind: @bind, resolve_name: !!@source_hostname_key, max_bytes: @message_length_limit, receive_buffer_size: @receive_buffer_size) do |data, sock|
data.chomp! if @remove_newline
begin
@parser.parse(data) do |time, record|
Expand Down
21 changes: 21 additions & 0 deletions test/plugin/test_in_tcp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,25 @@ def create_tcp_socket(host, port, &block)
assert_equal expected_record, d.events[i][2]
end
end

test 'source_hostname_key' do
d = create_driver(BASE_CONFIG + %!
format none
source_hostname_key host
!)
hostname = nil
d.run(expect_records: 1) do
create_tcp_socket('127.0.0.1', PORT) do |sock|
sock.do_not_reverse_lookup = false
hostname = sock.peeraddr[2]
sock.send("test\n", 0)
end
end

assert_equal 1, d.events.size
event = d.events[0]
assert_equal "tcp", event[0]
assert event[1].is_a?(Fluent::EventTime)
assert_equal hostname, event[2]['host']
end
end
21 changes: 21 additions & 0 deletions test/plugin/test_in_udp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def create_udp_socket(host, port)
else
UDPSocket.new(Socket::AF_INET6)
end
u.do_not_reverse_lookup = false
u.connect(host, port)
if block_given?
begin
Expand Down Expand Up @@ -174,6 +175,26 @@ def create_udp_socket(host, port)
end
end

test 'source_hostname_key' do
d = create_driver(BASE_CONFIG + %!
format none
source_hostname_key host
!)
hostname = nil
d.run(expect_records: 1) do
create_udp_socket('127.0.0.1', PORT) do |u|
u.send("test", 0)
hostname = u.peeraddr[2]
end
end

expected = {'message' => 'test'}
assert_equal 1, d.events.size
assert_equal "udp", d.events[0][0]
assert d.events[0][1].is_a?(Fluent::EventTime)
assert_equal hostname, d.events[0][2]['host']
end

test 'receive_buffer_size' do
# doesn't check exact value because it depends on platform and condition

Expand Down

0 comments on commit b76c9d2

Please sign in to comment.