influxdb 0.0.17 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/influxdb/client.rb +70 -28
- data/lib/influxdb/errors.rb +1 -1
- data/lib/influxdb/version.rb +1 -1
- data/lib/influxdb/worker.rb +10 -6
- data/spec/influxdb/client_spec.rb +25 -12
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b18af835198acf64a60f0b61b3464e79dcbf98e8
|
4
|
+
data.tar.gz: 068c5f701cfe5a346778017cfdb72ae622ecaa37
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 63fe3e1f05b0a61c685a2fc8d536b5d343311c315d5c3ae9502a8fe1b1526657e544087afa330570c840d7bcdca85d28785cb654034b2832206b5397a902f9eb
|
7
|
+
data.tar.gz: 6e885b8eac8803845c3779d801e1eac99c5d90836de81f3f70ed92a8cf0400e9cd333790a6f303a13cbacf298c64606f2185f312e6532f293f70a893e4b623c8
|
data/lib/influxdb/client.rb
CHANGED
@@ -5,7 +5,14 @@ require 'json'
|
|
5
5
|
|
6
6
|
module InfluxDB
|
7
7
|
class Client
|
8
|
-
attr_accessor :
|
8
|
+
attr_accessor :hosts,
|
9
|
+
:port,
|
10
|
+
:username,
|
11
|
+
:password,
|
12
|
+
:database,
|
13
|
+
:time_precision,
|
14
|
+
:use_ssl
|
15
|
+
|
9
16
|
attr_accessor :queue, :worker
|
10
17
|
|
11
18
|
include InfluxDB::Logger
|
@@ -34,13 +41,21 @@ module InfluxDB
|
|
34
41
|
def initialize *args
|
35
42
|
@database = args.first if args.first.is_a? String
|
36
43
|
opts = args.last.is_a?(Hash) ? args.last : {}
|
37
|
-
@
|
44
|
+
@hosts = opts[:hosts] || opts[:host] || ["localhost"]
|
38
45
|
@port = opts[:port] || 8086
|
39
46
|
@username = opts[:username] || "root"
|
40
47
|
@password = opts[:password] || "root"
|
41
|
-
@
|
42
|
-
@http.use_ssl = opts[:use_ssl]
|
48
|
+
@use_ssl = opts[:use_ssl] || false
|
43
49
|
@time_precision = opts[:time_precision] || "s"
|
50
|
+
@initial_delay = opts[:initial_delay] || 0.01
|
51
|
+
@max_delay = opts[:max_delay] || 30
|
52
|
+
@open_timeout = opts[:write_timeout] || 5
|
53
|
+
@read_timeout = opts[:read_timeout] || 300
|
54
|
+
@async = opts[:async] || false
|
55
|
+
|
56
|
+
unless @hosts.is_a? Array
|
57
|
+
@hosts = [@hosts]
|
58
|
+
end
|
44
59
|
end
|
45
60
|
|
46
61
|
## allow options, e.g. influxdb.create_database('foo', replicationFactor: 3)
|
@@ -111,7 +126,7 @@ module InfluxDB
|
|
111
126
|
get full_url("continuous_queries")
|
112
127
|
end
|
113
128
|
|
114
|
-
def write_point(name, data, async
|
129
|
+
def write_point(name, data, async=@async, time_precision=@time_precision)
|
115
130
|
data = data.is_a?(Array) ? data : [data]
|
116
131
|
columns = data.reduce(:merge).keys.sort {|a,b| a.to_s <=> b.to_s}
|
117
132
|
payload = {:name => name, :points => [], :columns => columns}
|
@@ -123,14 +138,14 @@ module InfluxDB
|
|
123
138
|
end
|
124
139
|
|
125
140
|
if async
|
126
|
-
@worker = InfluxDB::Worker.new if @worker.nil?
|
141
|
+
@worker = InfluxDB::Worker.new(self) if @worker.nil?
|
127
142
|
@worker.queue.push(payload)
|
128
143
|
else
|
129
144
|
_write([payload], time_precision)
|
130
145
|
end
|
131
146
|
end
|
132
147
|
|
133
|
-
def _write(payload, time_precision
|
148
|
+
def _write(payload, time_precision=@time_precision)
|
134
149
|
url = full_url("db/#{@database}/series", "time_precision=#{time_precision}")
|
135
150
|
data = JSON.generate(payload)
|
136
151
|
post(url, data)
|
@@ -161,36 +176,63 @@ module InfluxDB
|
|
161
176
|
end
|
162
177
|
|
163
178
|
def get(url)
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
179
|
+
connect_with_retry do |http|
|
180
|
+
response = http.request(Net::HTTP::Get.new(url))
|
181
|
+
if response.kind_of? Net::HTTPSuccess
|
182
|
+
return JSON.parse(response.body)
|
183
|
+
elsif response.kind_of? Net::HTTPUnauthorized
|
184
|
+
raise InfluxDB::AuthenticationError.new response.body
|
185
|
+
else
|
186
|
+
raise InfluxDB::Error.new response.body
|
187
|
+
end
|
171
188
|
end
|
172
189
|
end
|
173
190
|
|
174
191
|
def post(url, data)
|
175
192
|
headers = {"Content-Type" => "application/json"}
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
193
|
+
connect_with_retry do |http|
|
194
|
+
response = http.request(Net::HTTP::Post.new(url, headers), data)
|
195
|
+
if response.kind_of? Net::HTTPSuccess
|
196
|
+
return response
|
197
|
+
elsif response.kind_of? Net::HTTPUnauthorized
|
198
|
+
raise InfluxDB::AuthenticationError.new response.body
|
199
|
+
else
|
200
|
+
raise InfluxDB::Error.new response.body
|
201
|
+
end
|
183
202
|
end
|
184
203
|
end
|
185
204
|
|
186
205
|
def delete(url)
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
206
|
+
connect_with_retry do |http|
|
207
|
+
response = http.request(Net::HTTP::Delete.new(url))
|
208
|
+
if response.kind_of? Net::HTTPSuccess
|
209
|
+
return response
|
210
|
+
elsif response.kind_of? Net::HTTPUnauthorized
|
211
|
+
raise InfluxDB::AuthenticationError.new response.body
|
212
|
+
else
|
213
|
+
raise InfluxDB::Error.new response.body
|
214
|
+
end
|
215
|
+
end
|
216
|
+
end
|
217
|
+
|
218
|
+
def connect_with_retry(&block)
|
219
|
+
hosts = @hosts.dup
|
220
|
+
delay = @initial_delay
|
221
|
+
|
222
|
+
begin
|
223
|
+
hosts.push(host = hosts.shift)
|
224
|
+
http = Net::HTTP.new(host, @port)
|
225
|
+
http.open_timeout = @open_timeout
|
226
|
+
http.read_timeout = @read_timeout
|
227
|
+
http.use_ssl = @use_ssl
|
228
|
+
block.call(http)
|
229
|
+
rescue StandardError => e
|
230
|
+
log :error, "Failed to contact host #{host}: #{e.inspect} - retrying in #{delay}s."
|
231
|
+
log :info, "Queue size is #{@queue.length}." unless @queue.nil?
|
232
|
+
|
233
|
+
sleep delay
|
234
|
+
delay = [@max_delay, delay * 2].min
|
235
|
+
retry
|
194
236
|
end
|
195
237
|
end
|
196
238
|
|
data/lib/influxdb/errors.rb
CHANGED
data/lib/influxdb/version.rb
CHANGED
data/lib/influxdb/worker.rb
CHANGED
@@ -4,16 +4,18 @@ require "uri"
|
|
4
4
|
|
5
5
|
module InfluxDB
|
6
6
|
class Worker
|
7
|
+
attr_reader :client
|
7
8
|
attr_accessor :queue
|
8
9
|
|
9
10
|
include InfluxDB::Logger
|
10
11
|
|
11
|
-
MAX_POST_POINTS =
|
12
|
+
MAX_POST_POINTS = 1000
|
12
13
|
NUM_WORKER_THREADS = 3
|
13
|
-
SLEEP_INTERVAL =
|
14
|
+
SLEEP_INTERVAL = 5
|
14
15
|
|
15
|
-
def initialize
|
16
|
+
def initialize(client)
|
16
17
|
@queue = InfluxDB::MaxQueue.new
|
18
|
+
@client = client
|
17
19
|
spawn_threads!
|
18
20
|
end
|
19
21
|
|
@@ -38,8 +40,8 @@ module InfluxDB
|
|
38
40
|
end
|
39
41
|
|
40
42
|
while true
|
41
|
-
sleep SLEEP_INTERVAL
|
42
43
|
self.check_background_queue(thread_num)
|
44
|
+
sleep SLEEP_INTERVAL
|
43
45
|
end
|
44
46
|
end
|
45
47
|
end
|
@@ -53,12 +55,14 @@ module InfluxDB
|
|
53
55
|
|
54
56
|
while data.size < MAX_POST_POINTS && !@queue.empty?
|
55
57
|
p = @queue.pop(true) rescue next;
|
56
|
-
log :debug, "Found data in the queue! (#{p[:n]})"
|
57
58
|
data.push p
|
58
59
|
end
|
59
60
|
|
61
|
+
return if data.empty?
|
62
|
+
|
60
63
|
begin
|
61
|
-
|
64
|
+
log :debug, "Found data in the queue! (#{data.length} points)"
|
65
|
+
@client._write(data)
|
62
66
|
rescue => e
|
63
67
|
puts "Cannot write data: #{e.inspect}"
|
64
68
|
end
|
@@ -14,18 +14,18 @@ describe InfluxDB::Client do
|
|
14
14
|
|
15
15
|
@influxdb.should be_a InfluxDB::Client
|
16
16
|
@influxdb.database.should be_nil
|
17
|
-
@influxdb.
|
17
|
+
@influxdb.hosts.should == ["localhost"]
|
18
18
|
@influxdb.port.should == 8086
|
19
19
|
@influxdb.username.should == "root"
|
20
20
|
@influxdb.password.should == "root"
|
21
|
-
@influxdb.
|
21
|
+
@influxdb.use_ssl.should be_false
|
22
22
|
@influxdb.time_precision.should == "s"
|
23
23
|
end
|
24
24
|
end
|
25
25
|
|
26
26
|
describe "with no database specified" do
|
27
27
|
it "should be initialized with a nil database and the specified options" do
|
28
|
-
@influxdb = InfluxDB::Client.new :
|
28
|
+
@influxdb = InfluxDB::Client.new :host => "host",
|
29
29
|
:port => "port",
|
30
30
|
:username => "username",
|
31
31
|
:password => "password",
|
@@ -33,7 +33,7 @@ describe InfluxDB::Client do
|
|
33
33
|
|
34
34
|
@influxdb.should be_a InfluxDB::Client
|
35
35
|
@influxdb.database.should be_nil
|
36
|
-
@influxdb.
|
36
|
+
@influxdb.hosts.should == ["host"]
|
37
37
|
@influxdb.port.should == "port"
|
38
38
|
@influxdb.username.should == "username"
|
39
39
|
@influxdb.password.should == "password"
|
@@ -47,7 +47,7 @@ describe InfluxDB::Client do
|
|
47
47
|
|
48
48
|
@influxdb.should be_a(InfluxDB::Client)
|
49
49
|
@influxdb.database.should == "database"
|
50
|
-
@influxdb.
|
50
|
+
@influxdb.hosts.should == ["localhost"]
|
51
51
|
@influxdb.port.should == 8086
|
52
52
|
@influxdb.username.should == "root"
|
53
53
|
@influxdb.password.should == "root"
|
@@ -57,7 +57,7 @@ describe InfluxDB::Client do
|
|
57
57
|
|
58
58
|
describe "with both a database and options specified" do
|
59
59
|
it "should be initialized with the specified database and options" do
|
60
|
-
@influxdb = InfluxDB::Client.new "database", :
|
60
|
+
@influxdb = InfluxDB::Client.new "database", :host => "host",
|
61
61
|
:port => "port",
|
62
62
|
:username => "username",
|
63
63
|
:password => "password",
|
@@ -65,7 +65,7 @@ describe InfluxDB::Client do
|
|
65
65
|
|
66
66
|
@influxdb.should be_a(InfluxDB::Client)
|
67
67
|
@influxdb.database.should == "database"
|
68
|
-
@influxdb.
|
68
|
+
@influxdb.hosts.should == ["host"]
|
69
69
|
@influxdb.port.should == "port"
|
70
70
|
@influxdb.username.should == "username"
|
71
71
|
@influxdb.password.should == "password"
|
@@ -75,15 +75,28 @@ describe InfluxDB::Client do
|
|
75
75
|
|
76
76
|
describe "with ssl option specified" do
|
77
77
|
it "should be initialized with ssl enabled" do
|
78
|
-
@influxdb = InfluxDB::Client.new
|
78
|
+
@influxdb = InfluxDB::Client.new :use_ssl => true
|
79
79
|
|
80
80
|
@influxdb.should be_a InfluxDB::Client
|
81
81
|
@influxdb.database.should be_nil
|
82
|
-
@influxdb.
|
82
|
+
@influxdb.hosts.should == ["localhost"]
|
83
|
+
@influxdb.port.should == 8086
|
84
|
+
@influxdb.username.should == "root"
|
85
|
+
@influxdb.password.should == "root"
|
86
|
+
@influxdb.use_ssl.should be_true
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
describe "with multiple hosts specified" do
|
91
|
+
it "should be initialized with ssl enabled" do
|
92
|
+
@influxdb = InfluxDB::Client.new :hosts => ["1.1.1.1", "2.2.2.2"]
|
93
|
+
|
94
|
+
@influxdb.should be_a InfluxDB::Client
|
95
|
+
@influxdb.database.should be_nil
|
96
|
+
@influxdb.hosts.should == ["1.1.1.1", "2.2.2.2"]
|
83
97
|
@influxdb.port.should == 8086
|
84
98
|
@influxdb.username.should == "root"
|
85
99
|
@influxdb.password.should == "root"
|
86
|
-
@influxdb.instance_variable_get(:@http).use_ssl?.should == true
|
87
100
|
end
|
88
101
|
end
|
89
102
|
end
|
@@ -349,7 +362,7 @@ describe InfluxDB::Client do
|
|
349
362
|
data = {:name => "juan", :age => 87, :time => time_in_seconds}
|
350
363
|
|
351
364
|
@influxdb.write_point("seriez", data).should be_a(Net::HTTPOK)
|
352
|
-
end
|
365
|
+
end
|
353
366
|
|
354
367
|
it "should POST to add points with time field with precision defined in call of write function" do
|
355
368
|
time_in_milliseconds = (Time.now.to_f * 1000).to_i
|
@@ -367,7 +380,7 @@ describe InfluxDB::Client do
|
|
367
380
|
data = {:name => "juan", :age => 87, :time => time_in_milliseconds}
|
368
381
|
|
369
382
|
@influxdb.write_point("seriez", data, false, "m").should be_a(Net::HTTPOK)
|
370
|
-
end
|
383
|
+
end
|
371
384
|
end
|
372
385
|
|
373
386
|
describe "#execute_queries" do
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: influxdb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Todd Persen
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-03-
|
11
|
+
date: 2014-03-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: json
|