Skip to content

Commit

Permalink
Merge pull request #1061 from cosmo0920/migrate-in_syslog-to-v0.14
Browse files Browse the repository at this point in the history
Migrate in syslog to v0.14 API
  • Loading branch information
tagomoris authored Jul 11, 2016
2 parents 3ec844f + fac84e5 commit 4082b65
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 38 deletions.
32 changes: 10 additions & 22 deletions lib/fluent/plugin/in_syslog.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@
require 'cool.io'
require 'yajl'

require 'fluent/input'
require 'fluent/plugin/input'
require 'fluent/config/error'
require 'fluent/parser'
require 'fluent/plugin/parser'

module Fluent
module Fluent::Plugin
class SyslogInput < Input
Plugin.register_input('syslog', self)
Fluent::Plugin.register_input('syslog', self)

helpers :parser, :event_loop

SYSLOG_REGEXP = /^\<([0-9]+)\>(.*)/

Expand Down Expand Up @@ -99,14 +101,13 @@ def configure(conf)
@use_default = false

if conf.has_key?('format')
@parser = Plugin.new_parser(conf['format'])
@parser.configure(conf)
@parser = parser_create(usage: 'syslog_input', type: conf['format'], conf: conf)
else
conf['with_priority'] = true
@parser = TextParser::SyslogParser.new
@parser.configure(conf)
@parser = parser_create(usage: 'syslog_input', type: 'syslog', conf: conf)
@use_default = true
end
@_event_loop_run_timeout = @blocking_timeout
end

def start
Expand All @@ -118,29 +119,16 @@ def start
method(:receive_data_parser)
end

@loop = Coolio::Loop.new
@handler = listen(callback)
@loop.attach(@handler)

@thread = Thread.new(&method(:run))
event_loop_attach(@handler)
end

def shutdown
@loop.watchers.each {|w| w.detach }
@loop.stop
@handler.close
@thread.join

super
end

def run
@loop.run(@blocking_timeout)
rescue
log.error "unexpected error", error: $!.to_s
log.error_backtrace
end

private

def receive_data_parser(data, addr)
Expand Down
32 changes: 16 additions & 16 deletions test/plugin/test_in_syslog.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require_relative '../helper'
require 'fluent/test'
require 'fluent/test/driver/input'
require 'fluent/plugin/in_syslog'

class SyslogInputTest < Test::Unit::TestCase
Expand Down Expand Up @@ -34,7 +34,7 @@ def setup
]

def create_driver(conf=CONFIG)
Fluent::Test::InputTestDriver.new(Fluent::SyslogInput).configure(conf)
Fluent::Test::Driver::Input.new(Fluent::Plugin::SyslogInput).configure(conf)
end

def test_configure
Expand Down Expand Up @@ -69,9 +69,9 @@ def test_time_format
sleep 1
end

emits = d.emits
emits.each_index {|i|
assert_equal_event_time(tests[i]['expected'], emits[i][1])
events = d.events
events.each_index {|i|
assert_equal_event_time(tests[i]['expected'], events[i][1])
}
}
end
Expand All @@ -89,7 +89,7 @@ def test_msg_size
sleep 1
end

compare_test_result(d.emits, tests)
compare_test_result(d.events, tests)
end

def test_msg_size_with_tcp
Expand All @@ -105,7 +105,7 @@ def test_msg_size_with_tcp
sleep 1
end

compare_test_result(d.emits, tests)
compare_test_result(d.events, tests)
end

def test_msg_size_with_same_tcp_connection
Expand All @@ -121,7 +121,7 @@ def test_msg_size_with_same_tcp_connection
sleep 1
end

compare_test_result(d.emits, tests)
compare_test_result(d.events, tests)
end

def test_msg_size_with_json_format
Expand All @@ -141,11 +141,11 @@ def test_msg_size_with_json_format
sleep 1
end

compare_test_result(d.emits, tests)
compare_test_result(d.events, tests)
end

def test_msg_size_with_include_source_host
d = create_driver([CONFIG, 'include_source_host'].join("\n"))
d = create_driver([CONFIG, 'include_source_host true'].join("\n"))
tests = create_test_case

host = nil
Expand All @@ -159,7 +159,7 @@ def test_msg_size_with_include_source_host
sleep 1
end

compare_test_result(d.emits, tests, host)
compare_test_result(d.events, tests, host)
end

def create_test_case
Expand All @@ -170,11 +170,11 @@ def create_test_case
]
end

def compare_test_result(emits, tests, host = nil)
emits.each_index { |i|
assert_equal('syslog.kern.info', emits[0][0]) # <6> means kern.info
assert_equal(tests[i]['expected'], emits[i][2]['message'])
assert_equal(host, emits[i][2]['source_host']) if host
def compare_test_result(events, tests, host = nil)
events.each_index { |i|
assert_equal('syslog.kern.info', events[i][0]) # <6> means kern.info
assert_equal(tests[i]['expected'], events[i][2]['message'])
assert_equal(host, events[i][2]['source_host']) if host
}
end
end

0 comments on commit 4082b65

Please sign in to comment.