Skip to content

Commit ec712c5

Browse files
Merge pull request #4 from HatsuneMiku3939/fetaure/add-remote-timeout
add initial implementation of remote timeout
2 parents 4a1ee2a + 7b64e53 commit ec712c5

File tree

3 files changed

+123
-33
lines changed

3 files changed

+123
-33
lines changed

README.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ $ bundle
3939
4040
tag test
4141
url http://www.google.com
42-
interval 1
42+
interval 1s
4343
4444
status_only true
4545
</source>
@@ -67,7 +67,7 @@ $ bundle
6767
6868
tag fluentd.status
6969
url http://localhost:24220/api/plugins.json
70-
interval 1
70+
interval 1s
7171
</source>
7272
7373
<match fluentd.status>
@@ -88,7 +88,7 @@ $ bundle
8888
8989
tag es.cluster.health
9090
url http://localhost:9200/_cluster/health
91-
interval 1
91+
interval 1s
9292
</source>
9393
9494
<match es.cluster.health>
@@ -111,14 +111,18 @@ The tag of the event.
111111

112112
The url of remote server.
113113

114-
### interval (integer) (required)
114+
### interval (time) (required)
115115

116-
The second interval time between periodic request.
116+
The interval time between periodic request.
117117

118118
### status_only (bool) (optional, default: false)
119119

120120
If atatus_only is true, body is not parsed.
121121

122+
### timeout (integer) (optional, default: 10)
123+
124+
Timeout second of each request.
125+
122126
## In case of remote error
123127

124128
### Can receive response from remote

lib/fluent/plugin/in_http_pull.rb

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ def initialize
3535
config_param :interval, :time
3636
desc 'status_only'
3737
config_param :status_only, :bool, default: false
38+
desc 'timeout second of each request'
39+
config_param :timeout, :integer, default: 10
3840

3941
def configure(conf)
4042
super
@@ -50,14 +52,18 @@ def on_timer
5052
log = { "url" => @url }
5153

5254
begin
53-
res = RestClient.get(@url)
55+
res = RestClient::Request.execute(method: :get,
56+
url: @url,
57+
timeout: @timeout)
5458
log["status"] = res.code
5559
log["body"] = res.body
56-
rescue RestClient::ExceptionWithResponse => err
57-
log["status"] = err.code
58-
log["error"] = err.message
59-
rescue Exception => err
60-
log["status"] = 0
60+
rescue StandardError => err
61+
if err.respond_to? :code
62+
log["status"] = err.code
63+
else
64+
log["status"] = 0
65+
end
66+
6167
log["error"] = err.message
6268
end
6369

test/plugin/test_in_http_pull.rb

Lines changed: 102 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,59 +4,91 @@
44
require 'ostruct'
55

66
class HttpPullInputTest < Test::Unit::TestCase
7-
TEST_INTERVAL_3_CONFIG = %[
8-
tag test
9-
url http://127.0.0.1
7+
setup do
8+
Fluent::Test.setup
9+
end
10+
11+
sub_test_case "default value of each options" do
12+
TEST_DEFAULT_VALUE_CONFIG = %[
13+
tag test
14+
url http://127.0.0.1
15+
interval 3s
16+
]
1017

11-
interval 3
12-
status_only true
13-
]
18+
test 'status_only' do
19+
d = create_driver TEST_DEFAULT_VALUE_CONFIG
20+
assert_equal("test", d.instance.tag)
1421

15-
TEST_INTERVAL_5_CONFIG = %[
16-
tag test
17-
url http://127.0.0.1
22+
assert_equal(false, d.instance.status_only)
23+
end
1824

19-
interval 5
20-
]
25+
test 'timeout' do
26+
d = create_driver TEST_DEFAULT_VALUE_CONFIG
27+
assert_equal("test", d.instance.tag)
2128

22-
setup do
23-
Fluent::Test.setup
29+
assert_equal(10, d.instance.timeout)
30+
end
2431
end
2532

2633
sub_test_case "success case with status only" do
34+
TEST_INTERVAL_3_CONFIG = %[
35+
tag test
36+
url http://127.0.0.1
37+
timeout 10
38+
39+
interval 3s
40+
status_only true
41+
]
42+
43+
TEST_INTERVAL_5_CONFIG = %[
44+
tag test
45+
url http://127.0.0.1
46+
timeout 10
47+
48+
interval 5s
49+
]
50+
2751
setup do
28-
mock(RestClient).get("http://127.0.0.1").times(2) do
29-
OpenStruct.new({code: 200, body: '{"status": "OK"}'})
30-
end
52+
mock(RestClient::Request).
53+
execute(method: :get,
54+
url: "http://127.0.0.1",
55+
timeout: 10).
56+
times(2) do
57+
OpenStruct.new({code: 200, body: '{"status": "OK"}'})
58+
end
3159
end
3260

3361
test 'interval 3 with status_only' do
3462
d = create_driver TEST_INTERVAL_3_CONFIG
35-
assert_equal "test", d.instance.tag
63+
assert_equal("test", d.instance.tag)
64+
assert_equal(3, d.instance.interval)
3665

3766
d.run(timeout: 5) do
3867
sleep 7
3968
end
40-
assert_equal 2, d.events.size
69+
assert_equal(2, d.events.size)
4170

4271
d.events.each do |tag, time, record|
4372
assert_equal("test", tag)
73+
4474
assert_equal({"url"=>"http://127.0.0.1","status"=>200}, record)
4575
assert(time.is_a?(Fluent::EventTime))
4676
end
4777
end
4878

4979
test 'interval 5' do
5080
d = create_driver TEST_INTERVAL_5_CONFIG
51-
assert_equal "test", d.instance.tag
81+
assert_equal("test", d.instance.tag)
82+
assert_equal(5, d.instance.interval)
5283

5384
d.run(timeout: 7) do
5485
sleep 11
5586
end
56-
assert_equal 2, d.events.size
87+
assert_equal(2, d.events.size)
5788

5889
d.events.each do |tag, time, record|
5990
assert_equal("test", tag)
91+
6092
assert_equal({"url"=>"http://127.0.0.1","status"=>200, "message"=>{"status"=>"OK"}}, record)
6193
assert(time.is_a?(Fluent::EventTime))
6294
end
@@ -71,14 +103,62 @@ class HttpPullInputTest < Test::Unit::TestCase
71103
]
72104
test "connection refused by remote" do
73105
d = create_driver TEST_REFUSED_CONFIG
74-
d.run(timeout: 2) { sleep 3 }
106+
assert_equal("test", d.instance.tag)
75107

76-
assert_equal 3, d.events.size
108+
d.run(timeout: 2) do
109+
sleep 3
110+
end
111+
112+
assert_equal(3, d.events.size)
77113
d.events.each do |tag, time, record|
78114
assert_equal("test", tag)
115+
79116
assert_equal("http://127.0.0.1:5927", record["url"])
117+
assert(time.is_a?(Fluent::EventTime))
118+
80119
assert_equal(0, record["status"])
120+
assert_not_nil(record["error"])
121+
end
122+
end
123+
end
124+
125+
sub_test_case "fail when remote timeout" do
126+
TEST_TIMEOUT_FAIL_CONFIG = %[
127+
tag test
128+
url http://127.0.0.1
129+
timeout 2
130+
131+
interval 3
132+
]
133+
134+
setup do
135+
mock(RestClient::Request).
136+
execute(method: :get,
137+
url: "http://127.0.0.1",
138+
timeout: 2).
139+
times(2) do
140+
sleep 2
141+
raise RestClient::Exceptions::Timeout.new
142+
end
143+
end
144+
145+
test "timeout" do
146+
d = create_driver TEST_TIMEOUT_FAIL_CONFIG
147+
assert_equal("test", d.instance.tag)
148+
149+
d.run(timeout: 5) do
150+
sleep 7
151+
end
152+
assert_equal(2, d.events.size)
153+
154+
d.events.each do |tag, time, record|
155+
assert_equal("test", tag)
156+
157+
assert_equal("http://127.0.0.1", record["url"])
81158
assert(time.is_a?(Fluent::EventTime))
159+
160+
assert_equal(0, record["status"])
161+
assert_not_nil(record["error"])
82162
end
83163
end
84164
end

0 commit comments

Comments
 (0)