Skip to content

Commit 307cb40

Browse files
committed
Add input plugin
1 parent 608b968 commit 307cb40

File tree

5 files changed

+174
-6
lines changed

5 files changed

+174
-6
lines changed

README.md

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,27 +30,49 @@ $ bundle
3030

3131
## Configuration
3232

33-
### plugins_json (string) (optional)
33+
### Filter plugin
34+
35+
#### plugins_json (string) (optional)
3436

3537
Path to `plugins.json`.
3638

3739
Default value: `https://raw.githubusercontent.com/fluent/fluentd-website/master/scripts/plugins.json`.
3840

39-
40-
### Deprecated: obsolete_plugins_yml (string) (optional)
41+
#### Deprecated: obsolete_plugins_yml (string) (optional)
4142

4243
Path to `obsolete-plugins.yml`. This parameter is deprecated. Please use `plugins_json` parameter instead.
4344

4445
Default value: `nil`
4546

46-
### timeout (integer) (optional)
47+
#### timeout (integer) (optional)
48+
49+
Timeout to read data of obsolete plugins.
50+
If it occurs timeout, it just skips to detect obsolete plugins.
51+
52+
Default value: `5`
53+
54+
#### raise_error (bool) (optional)
55+
56+
Raise error if obsolete plugins are detected
57+
58+
Default value: `false`.
59+
60+
### Input plugin
61+
62+
#### plugins_json (string) (optional)
63+
64+
Path to `plugins.json`.
65+
66+
Default value: `https://raw.githubusercontent.com/fluent/fluentd-website/master/scripts/plugins.json`.
67+
68+
#### timeout (integer) (optional)
4769

4870
Timeout to read data of obsolete plugins.
4971
If it occurs timeout, it just skips to detect obsolete plugins.
5072

5173
Default value: `5`
5274

53-
### raise_error (bool) (optional)
75+
#### raise_error (bool) (optional)
5476

5577
Raise error if obsolete plugins are detected
5678

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
require "fluent/plugin/input"
2+
require "fluent/plugin/obsolete_plugins_utils"
3+
4+
module Fluent
5+
module Plugin
6+
class ObsoletePluginsInput < Fluent::Plugin::Input
7+
Fluent::Plugin.register_input("obsolete_plugins", self)
8+
9+
PLUGINS_JSON_URL = "https://raw.githubusercontent.com/fluent/fluentd-website/master/scripts/plugins.json"
10+
11+
desc "Path to plugins.json"
12+
config_param :plugins_json, :string, default: PLUGINS_JSON_URL
13+
desc "Timeout value to read data of obsolete plugins"
14+
config_param :timeout, :integer, default: 5
15+
desc "Raise error if obsolete plugins are detected"
16+
config_param :raise_error, :bool, default: false
17+
18+
def configure(conf)
19+
super
20+
21+
obsolete_plugins = ObsoletePluginsUtils.obsolete_plugins_from_json(@plugins_json, timeout: @timeout)
22+
ObsoletePluginsUtils.notify(log, obsolete_plugins, raise_error: @raise_error)
23+
rescue Fluent::ConfigError
24+
raise
25+
rescue => e
26+
log.info("Failed to notify obsolete plugins", error: e)
27+
end
28+
end
29+
end
30+
end

test/helper.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
require "test/unit/rr"
44
require "timecop"
55
require "fluent/test"
6-
require "fluent/test/driver/filter"
76
require "fluent/test/helpers"
87

98
Test::Unit::TestCase.include(Fluent::Test::Helpers)

test/plugin/test_filter_obsolete_plugins.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require "helper"
2+
require "fluent/test/driver/filter"
23
require "fluent/plugin/filter_obsolete_plugins"
34

45
class ObsoletePluginsFilterTest < Test::Unit::TestCase
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
require "helper"
2+
require "fluent/test/driver/input"
3+
require "fluent/plugin/in_obsolete_plugins"
4+
require "fluent/plugin/obsolete_plugins_utils"
5+
6+
class ObsoletePluginsInputTest < Test::Unit::TestCase
7+
8+
setup do
9+
Fluent::Test.setup
10+
$log = Fluent::Test::TestLogger.new
11+
@time = Time.now
12+
Timecop.freeze(@time)
13+
end
14+
15+
teardown do
16+
Timecop.return
17+
end
18+
19+
sub_test_case "plugins_json" do
20+
CONFIG_JSON = %[
21+
plugins_json #{fixture_path("plugins.json")}
22+
]
23+
24+
test "no obsolete plugins" do
25+
d = create_driver(CONFIG_JSON)
26+
d.run
27+
assert_equal([], d.events)
28+
assert_equal([], d.logs)
29+
end
30+
31+
test "obsolete plugins" do
32+
stub(Gem).loaded_specs do
33+
{
34+
"fluent-plugin-tail-multiline" => nil,
35+
"fluent-plugin-hostname" => nil
36+
}
37+
end
38+
d = create_driver(CONFIG_JSON)
39+
d.run
40+
assert_equal([], d.events)
41+
expected_logs = [
42+
"#{@time} [warn]: fluent-plugin-tail-multiline is obsolete: Merged in in_tail in Fluentd v0.10.45. [fluent/fluentd#269](https://github.com/fluent/fluentd/issues/269)\n",
43+
"#{@time} [warn]: fluent-plugin-hostname is obsolete: Use [filter\\_record\\_transformer](http://docs.fluentd.org/v0.12/articles/filter_record_transformer) instead.\n"
44+
]
45+
assert_equal(expected_logs, d.logs)
46+
end
47+
48+
test "raise error when detect obsolete plugins" do
49+
stub(Gem).loaded_specs do
50+
{
51+
"fluent-plugin-tail-multiline" => nil,
52+
"fluent-plugin-hostname" => nil
53+
}
54+
end
55+
56+
ex = assert_raise(Fluent::ConfigError) do
57+
create_driver(CONFIG_JSON + "raise_error yes")
58+
end
59+
assert_equal("Detected obsolete plugins", ex.message)
60+
end
61+
end
62+
63+
sub_test_case "error handling" do
64+
test "invalid json" do
65+
d = create_driver("plugins_json #{fixture_path('invalid.json')}")
66+
67+
expected_logs = [
68+
"#{@time} [info]: Failed to notify obsolete plugins error_class=JSON::ParserError error=\"expected ',' or '}' after object value, got: EOF at line 11 column 1\"\n",
69+
]
70+
71+
assert_equal(expected_logs, d.logs)
72+
end
73+
74+
test "timeout with slow server" do
75+
server = create_slow_webserver(port: 12345)
76+
77+
mock(Fluent::Plugin::ObsoletePluginsUtils).notify.never
78+
79+
d = create_driver(%[
80+
plugins_json http://localhost:12345/plugins.json
81+
timeout 1
82+
])
83+
84+
sleep 2
85+
86+
expected_logs = [
87+
"#{@time} [info]: Failed to notify obsolete plugins error_class=Timeout::Error error=\"execution expired\"\n",
88+
]
89+
90+
assert_equal(expected_logs, d.logs)
91+
ensure
92+
server.shutdown
93+
end
94+
95+
end
96+
97+
private
98+
99+
def create_driver(conf)
100+
Fluent::Test::Driver::Input.new(Fluent::Plugin::ObsoletePluginsInput).configure(conf)
101+
end
102+
103+
def create_slow_webserver(port: 12345)
104+
require "webrick"
105+
106+
server = WEBrick::HTTPServer.new(Port: port)
107+
server.mount_proc '/' do |req, res|
108+
sleep 60
109+
110+
res['Content-Type'] = 'application/json'
111+
res.body = File.read(fixture_path("plugins.json"))
112+
end
113+
114+
server
115+
end
116+
end

0 commit comments

Comments
 (0)