fluent-plugin-http-pull 0.2.0.pre → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 198fe9ad32356188705d13ad28b5587e956a9dae
4
- data.tar.gz: 1bd9d9eb636748fbd24e5ea58b687138990d3021
3
+ metadata.gz: e57e28927445599a7804fccaf99431766cc812f4
4
+ data.tar.gz: dd25e97f2e55ff1a7f6ab4f4ef857b7ad4d6bc53
5
5
  SHA512:
6
- metadata.gz: d28286e0838a18b75954d315beea25d9da6831fff29b28d2ad7d5471505e7e758ceff63e5dc85dbd7cf2069325bb0692582c7a7d0ec7e07561a49d43d22faf37
7
- data.tar.gz: 55fe7c398310a9fbc4596a57753a43e3970a8c5091507396bcda160e3765a4e7c223a5f333e2b5d4e94e7b09df08505e18f0fd628bc7717892c207b01a47151d
6
+ metadata.gz: '08788a78bfec1da171d1781a866974ffce3066dc82ab72a34fed00171d1ead7fb8429856fe468a1ca0c11e3f2719c4de75e9a414e77b78e81df0b28c35387a20'
7
+ data.tar.gz: 5073957aa4e3ebe5f19c39d33a08eded5fb138cc71649ceb6b51159ac883ee32c8ad79f8797d469ac9431521b5a4764c48eb8d22a5545c6da9369cec35169a6b
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ TODO
2
+ *.swp
3
+ .bundle
4
+ Gemfile.lock
data/README.md CHANGED
@@ -39,7 +39,7 @@ $ bundle
39
39
 
40
40
  tag test
41
41
  url http://www.google.com
42
- interval 1
42
+ interval 1s
43
43
 
44
44
  status_only true
45
45
  </source>
@@ -67,7 +67,7 @@ $ bundle
67
67
 
68
68
  tag fluentd.status
69
69
  url http://localhost:24220/api/plugins.json
70
- interval 1
70
+ interval 1s
71
71
  </source>
72
72
 
73
73
  <match fluentd.status>
@@ -88,7 +88,7 @@ $ bundle
88
88
 
89
89
  tag es.cluster.health
90
90
  url http://localhost:9200/_cluster/health
91
- interval 1
91
+ interval 1s
92
92
  </source>
93
93
 
94
94
  <match es.cluster.health>
@@ -111,14 +111,18 @@ The tag of the event.
111
111
 
112
112
  The url of remote server.
113
113
 
114
- ### interval (integer) (required)
114
+ ### interval (time) (required)
115
115
 
116
- The second interval time between periodic request.
116
+ The interval time between periodic request.
117
117
 
118
118
  ### status_only (bool) (optional, default: false)
119
119
 
120
120
  If atatus_only is true, body is not parsed.
121
121
 
122
+ ### timeout (integer) (optional, default: 10)
123
+
124
+ Timeout second of each request.
125
+
122
126
  ## In case of remote error
123
127
 
124
128
  ### Can receive response from remote
@@ -3,7 +3,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
3
 
4
4
  Gem::Specification.new do |spec|
5
5
  spec.name = "fluent-plugin-http-pull"
6
- spec.version = "0.2.0.pre"
6
+ spec.version = "0.2.0"
7
7
  spec.authors = ["filepang"]
8
8
  spec.email = ["filepang@gmail.com"]
9
9
 
@@ -21,62 +21,60 @@ module Fluent
21
21
  class HttpPullInput < Fluent::Plugin::Input
22
22
  Fluent::Plugin.register_input("http_pull", self)
23
23
 
24
+ helpers :timer
25
+
26
+ def initialize
27
+ super
28
+ end
29
+
24
30
  desc 'The tag of the event.'
25
31
  config_param :tag, :string
26
-
27
32
  desc 'The uri of monitoring target'
28
33
  config_param :url, :string
29
-
30
- desc 'The second interval time between periodic request'
31
- config_param :interval, :integer
32
-
34
+ desc 'The interval time between periodic request'
35
+ config_param :interval, :time
33
36
  desc 'status_only'
34
37
  config_param :status_only, :bool, default: false
38
+ desc 'timeout second of each request'
39
+ config_param :timeout, :integer, default: 10
35
40
 
36
41
  def configure(conf)
37
42
  super
38
-
39
- @shutdown = false
40
43
  end
41
44
 
42
45
  def start
43
- @thread = Thread.new(&method(:run))
44
- end
45
-
46
- def run
47
- while !@shutdown
48
- ts = Time.now.to_f
46
+ super
49
47
 
50
- log = { "url" => @url }
48
+ timer_execute(:in_http_pull, @interval, &method(:on_timer))
49
+ end
51
50
 
52
- begin
53
- res = RestClient.get(@url)
54
- log["status"] = res.code
55
- log["body"] = res.body
56
- rescue RestClient::ExceptionWithResponse => err
51
+ def on_timer
52
+ log = { "url" => @url }
53
+
54
+ begin
55
+ res = RestClient::Request.execute(method: :get,
56
+ url: @url,
57
+ timeout: @timeout)
58
+ log["status"] = res.code
59
+ log["body"] = res.body
60
+ rescue StandardError => err
61
+ if err.respond_to? :code
57
62
  log["status"] = err.code
58
- log["error"] = err.message
59
- rescue Exception => err
63
+ else
60
64
  log["status"] = 0
61
- log["error"] = err.message
62
65
  end
63
66
 
64
- log["message"] = JSON.parse(log["body"]) if !@status_only && log["body"] != nil
65
- log.delete("body")
66
-
67
- router.emit(@tag, Engine.now, log)
67
+ log["error"] = err.message
68
+ end
68
69
 
69
- te = Time.now.to_f
70
- delay = @interval - (te - ts)
71
- delay = delay > 0 ? delay : 0
70
+ log["message"] = JSON.parse(log["body"]) if !@status_only && log["body"] != nil
71
+ log.delete("body")
72
72
 
73
- sleep delay
74
- end
73
+ router.emit(@tag, Engine.now, log)
75
74
  end
76
75
 
77
76
  def shutdown
78
- @shutdown = true
79
- @thread.join if @thread
77
+ super
80
78
  end
81
79
 
82
80
  end
@@ -4,43 +4,73 @@ require "fluent/plugin/in_http_pull.rb"
4
4
  require 'ostruct'
5
5
 
6
6
  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
10
 
11
- interval 3
12
- status_only true
13
- ]
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
+ ]
14
17
 
15
- TEST_INTERVAL_5_CONFIG = %[
16
- tag test
17
- url http://127.0.0.1
18
+ test 'status_only' do
19
+ d = create_driver TEST_DEFAULT_VALUE_CONFIG
20
+ assert_equal("test", d.instance.tag)
18
21
 
19
- interval 5
20
- ]
22
+ assert_equal(false, d.instance.status_only)
23
+ end
21
24
 
22
- setup do
23
- Fluent::Test.setup
25
+ test 'timeout' do
26
+ d = create_driver TEST_DEFAULT_VALUE_CONFIG
27
+ assert_equal("test", d.instance.tag)
28
+
29
+ assert_equal(10, d.instance.timeout)
30
+ end
24
31
  end
25
32
 
26
33
  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
+
27
51
  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
31
59
  end
32
60
 
33
61
  test 'interval 3 with status_only' do
34
62
  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)
36
65
 
37
66
  d.run(timeout: 5) do
38
- sleep 5
67
+ sleep 7
39
68
  end
40
- assert_equal 2, d.events.size
69
+ assert_equal(2, d.events.size)
41
70
 
42
71
  d.events.each do |tag, time, record|
43
72
  assert_equal("test", tag)
73
+
44
74
  assert_equal({"url"=>"http://127.0.0.1","status"=>200}, record)
45
75
  assert(time.is_a?(Fluent::EventTime))
46
76
  end
@@ -48,15 +78,17 @@ class HttpPullInputTest < Test::Unit::TestCase
48
78
 
49
79
  test 'interval 5' do
50
80
  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)
52
83
 
53
84
  d.run(timeout: 7) do
54
- sleep 7
85
+ sleep 11
55
86
  end
56
- assert_equal 2, d.events.size
87
+ assert_equal(2, d.events.size)
57
88
 
58
89
  d.events.each do |tag, time, record|
59
90
  assert_equal("test", tag)
91
+
60
92
  assert_equal({"url"=>"http://127.0.0.1","status"=>200, "message"=>{"status"=>"OK"}}, record)
61
93
  assert(time.is_a?(Fluent::EventTime))
62
94
  end
@@ -71,14 +103,62 @@ class HttpPullInputTest < Test::Unit::TestCase
71
103
  ]
72
104
  test "connection refused by remote" do
73
105
  d = create_driver TEST_REFUSED_CONFIG
74
- d.run(timeout: 2) { sleep 3 }
106
+ assert_equal("test", d.instance.tag)
107
+
108
+ d.run(timeout: 2) do
109
+ sleep 3
110
+ end
75
111
 
76
- assert_equal 3, d.events.size
112
+ assert_equal(3, d.events.size)
77
113
  d.events.each do |tag, time, record|
78
114
  assert_equal("test", tag)
115
+
79
116
  assert_equal("http://127.0.0.1:5927", record["url"])
117
+ assert(time.is_a?(Fluent::EventTime))
118
+
80
119
  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"])
81
158
  assert(time.is_a?(Fluent::EventTime))
159
+
160
+ assert_equal(0, record["status"])
161
+ assert_not_nil(record["error"])
82
162
  end
83
163
  end
84
164
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fluent-plugin-http-pull
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0.pre
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - filepang
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-08-29 00:00:00.000000000 Z
11
+ date: 2017-08-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -105,6 +105,7 @@ executables: []
105
105
  extensions: []
106
106
  extra_rdoc_files: []
107
107
  files:
108
+ - ".gitignore"
108
109
  - ".travis.yml"
109
110
  - Gemfile
110
111
  - LICENSE
@@ -129,9 +130,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
129
130
  version: '0'
130
131
  required_rubygems_version: !ruby/object:Gem::Requirement
131
132
  requirements:
132
- - - ">"
133
+ - - ">="
133
134
  - !ruby/object:Gem::Version
134
- version: 1.3.1
135
+ version: '0'
135
136
  requirements: []
136
137
  rubyforge_project:
137
138
  rubygems_version: 2.5.2