opentsdb-ruby 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +4 -5
- data/lib/opentsdb.rb +1 -0
- data/lib/opentsdb/client.rb +6 -7
- data/lib/opentsdb/faraday.rb +53 -0
- data/lib/opentsdb/query_param.rb +4 -1
- data/lib/opentsdb/query_parser.rb +5 -5
- data/lib/opentsdb/version.rb +1 -1
- metadata +17 -3
- data/lib/opentsdb/http_client.rb +0 -36
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: afae53299d7cd506a3088b34755f75adb7c3bc00
|
4
|
+
data.tar.gz: 7ed07f6b3a199ede80d502546d6dce94b115cf5b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7f64d2a0df96568f8061934e503a3720cd798cee218cd47bbce431420c4c312ee3de574022cb807fbc05b8932db10cc36a6c54279f998763f811c52e54b6b8b4
|
7
|
+
data.tar.gz: b75805bed80f6be6d6d11b2c864a10d1494f0df09b539bc3a06c2de9671f042d6970139ee3d975449f04b6006a5aed3596492e74d2f15247991d3ccb1bc1ed0a
|
data/README.md
CHANGED
@@ -40,26 +40,25 @@ Or install it yourself as:
|
|
40
40
|
|
41
41
|
# define simple query params
|
42
42
|
params = { begin: Time.now.ago(1.hour), q: 'avg:system.load.1{host=*}' }
|
43
|
-
# opensted
|
44
43
|
client = Opentsdb::Client.new(params)
|
45
|
-
result = client.query
|
44
|
+
result = client.query
|
46
45
|
# => { status: 'ok', condition: #<Opentsdb::QueryParam: @metric="system.load.1",..., result: '[{"metric": "system.load.1", "tags": ... "dps":[...]}]}'
|
47
46
|
|
48
47
|
# complicate query params
|
49
48
|
params = { begin: Time.now.ago(1.hour), end: Time.now, q: 'avg:system.load.1{host=server1, host=server2, tagk=tagv}by{host}', interval: 360 }
|
50
49
|
client = Opentsdb::Client.new(params)
|
51
|
-
result = client.query
|
50
|
+
result = client.query
|
52
51
|
# => { status: 'ok', condition: #<Opentsdb::QueryParam: @metric="system.load.1",..., result: '[{"metric": "system.load.1", "tags": ... "dps":[...]}]}'
|
53
52
|
|
54
53
|
# reconfig opentsdb host and port
|
55
54
|
params = { host: '192.168.0.100', port: 8000, q: 'avg:system.load.1{host=*}' }
|
56
55
|
client = Opentsdb::Client.new(params)
|
57
|
-
result = client.query
|
56
|
+
result = client.query
|
58
57
|
# => { status: 'ok', condition: #<Opentsdb::QueryParam: @metric="system.load.1",..., result: '[{"metric": "system.load.1", "tags": ... "dps":[...]}]}'
|
59
58
|
|
60
59
|
#query exception
|
61
60
|
client = Opentsdb::Client.new(q: 'avg:unknown_metric')
|
62
|
-
result = client.query
|
61
|
+
result = client.query
|
63
62
|
# => { status: 'error', condition: #<Opentsdb::QueryParam: @metric="system.load.1",..., result: '{"error":{"code":400,"message":"No such name for 'metrics'...}}'
|
64
63
|
|
65
64
|
```
|
data/lib/opentsdb.rb
CHANGED
data/lib/opentsdb/client.rb
CHANGED
@@ -1,10 +1,9 @@
|
|
1
|
-
require_relative 'http_client'
|
2
1
|
module Opentsdb
|
3
2
|
# ruby client for OpenTsdb HTTP API
|
4
3
|
class Client
|
5
4
|
extend Forwardable
|
6
5
|
|
7
|
-
def_instance_delegators :@
|
6
|
+
def_instance_delegators :@faraday, :post
|
8
7
|
|
9
8
|
attr_reader :host, :port
|
10
9
|
attr_accessor :query_commads
|
@@ -12,15 +11,15 @@ module Opentsdb
|
|
12
11
|
def initialize(options = {})
|
13
12
|
@host = options.delete(:host) || Opentsdb.host
|
14
13
|
@port = options.delete(:port) || Opentsdb.port
|
15
|
-
@
|
14
|
+
@faraday = Opentsdb::Faraday.new(query_url)
|
16
15
|
@query_commads = parse_queries options
|
17
16
|
end
|
18
17
|
|
19
18
|
def query
|
20
19
|
[].tap do |results|
|
21
20
|
query_commads.each do |query_commad|
|
22
|
-
res = post
|
23
|
-
status = res.
|
21
|
+
res = post query_commad.to_json
|
22
|
+
status = res.status.to_i == 200 ? 'ok' : 'error'
|
24
23
|
results << { status: status, condition: query_commad, result: res.body }
|
25
24
|
end
|
26
25
|
end
|
@@ -40,8 +39,8 @@ module Opentsdb
|
|
40
39
|
|
41
40
|
private
|
42
41
|
|
43
|
-
def
|
44
|
-
|
42
|
+
def query_url
|
43
|
+
"http://#{host}:#{port}/api/query"
|
45
44
|
end
|
46
45
|
end
|
47
46
|
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'faraday'
|
2
|
+
module Opentsdb
|
3
|
+
# :nodoc:
|
4
|
+
class Faraday
|
5
|
+
attr_reader :url
|
6
|
+
|
7
|
+
def initialize(url, options = {})
|
8
|
+
@url = url
|
9
|
+
@options = options
|
10
|
+
end
|
11
|
+
|
12
|
+
def post(body)
|
13
|
+
connection.post do |req|
|
14
|
+
req.headers = headers
|
15
|
+
req.body = body
|
16
|
+
req.options.timeout = 5
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def headers
|
23
|
+
{ 'Content-Type' => 'application/json; charset=UTF-8' }
|
24
|
+
end
|
25
|
+
|
26
|
+
def connection
|
27
|
+
@connection ||= begin
|
28
|
+
::Faraday.new(url: url) do |faraday|
|
29
|
+
faraday.request :url_encoded # form-encode POST params
|
30
|
+
faraday.response :logger # log requests to STDOUT
|
31
|
+
faraday.adapter auto_detect_adapter
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def auto_detect_adapter
|
37
|
+
case
|
38
|
+
when defined?(::Patron)
|
39
|
+
:partron
|
40
|
+
when defined?(::Excon)
|
41
|
+
:excon
|
42
|
+
when defined?(::Typhoeus)
|
43
|
+
:typhoeus
|
44
|
+
when defined?(::HTTPClient)
|
45
|
+
:httpclient
|
46
|
+
when defined?(::Net::HTTP::Persistent)
|
47
|
+
:net_http_persistent
|
48
|
+
else
|
49
|
+
::Faraday.default_adapter
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
data/lib/opentsdb/query_param.rb
CHANGED
@@ -11,6 +11,8 @@ module Opentsdb
|
|
11
11
|
@tags = options[:tags] || {}
|
12
12
|
@rate_options = options[:rate_options]
|
13
13
|
@group = options[:group] || []
|
14
|
+
@start_time = 0
|
15
|
+
@end_time = 0
|
14
16
|
end
|
15
17
|
|
16
18
|
def start_time
|
@@ -45,7 +47,8 @@ module Opentsdb
|
|
45
47
|
private
|
46
48
|
|
47
49
|
def to_ms(time = Time.now)
|
48
|
-
|
50
|
+
time = time.is_a?(Fixnum) ? time : time.to_i
|
51
|
+
time.to_s.ljust(13, '0').to_i # ms.to_size = 13
|
49
52
|
end
|
50
53
|
|
51
54
|
def queries
|
@@ -8,15 +8,15 @@ module Opentsdb
|
|
8
8
|
metric_query = {}
|
9
9
|
metric_query[:aggregator] = parts[0]
|
10
10
|
metric_query[:rate] = false
|
11
|
-
|
11
|
+
metric = parts[1]
|
12
12
|
if parts.size == 3
|
13
|
-
if
|
13
|
+
if metric.start_with?('rate')
|
14
14
|
metric_query[:rate] = true
|
15
|
-
metric_query[:rate_options] = parse_rate(
|
15
|
+
metric_query[:rate_options] = parse_rate(metric) if metric.index('{')
|
16
16
|
end
|
17
|
-
|
17
|
+
metric = parts[2]
|
18
18
|
end
|
19
|
-
QueryParam.new metric_query.merge(parse_metric(
|
19
|
+
QueryParam.new metric_query.merge(parse_metric(metric))
|
20
20
|
end
|
21
21
|
|
22
22
|
private
|
data/lib/opentsdb/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: opentsdb-ruby
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- lizhe
|
@@ -9,8 +9,22 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2016-
|
12
|
+
date: 2016-06-08 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: faraday
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - ~>
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: 0.9.2
|
21
|
+
type: :runtime
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ~>
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: 0.9.2
|
14
28
|
- !ruby/object:Gem::Dependency
|
15
29
|
name: rake
|
16
30
|
requirement: !ruby/object:Gem::Requirement
|
@@ -154,7 +168,7 @@ files:
|
|
154
168
|
- README.md
|
155
169
|
- lib/opentsdb.rb
|
156
170
|
- lib/opentsdb/client.rb
|
157
|
-
- lib/opentsdb/
|
171
|
+
- lib/opentsdb/faraday.rb
|
158
172
|
- lib/opentsdb/query_errors.rb
|
159
173
|
- lib/opentsdb/query_param.rb
|
160
174
|
- lib/opentsdb/query_parser.rb
|
data/lib/opentsdb/http_client.rb
DELETED
@@ -1,36 +0,0 @@
|
|
1
|
-
require 'net/http'
|
2
|
-
require 'benchmark'
|
3
|
-
module Opentsdb
|
4
|
-
# :nodoc:
|
5
|
-
class HttpClient
|
6
|
-
def post(uri, body)
|
7
|
-
send_http(uri) do |http|
|
8
|
-
Opentsdb.logger.debug "Http post body: #{body}"
|
9
|
-
req = Net::HTTP::Post.new(uri, headers)
|
10
|
-
req.body = body
|
11
|
-
http.request(req)
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
|
-
private
|
16
|
-
|
17
|
-
def headers
|
18
|
-
{ 'Content-Type' => 'application/json; charset=UTF-8' }
|
19
|
-
end
|
20
|
-
|
21
|
-
def send_http(uri)
|
22
|
-
Opentsdb.logger.info "Http request uri: #{uri}"
|
23
|
-
http = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https')
|
24
|
-
res = nil
|
25
|
-
time = Benchmark.realtime do
|
26
|
-
res = yield(http)
|
27
|
-
end
|
28
|
-
Opentsdb.logger.info "Response Code: #{res.code} Consume: #{time} s"
|
29
|
-
res
|
30
|
-
rescue Timeout::Error, Errno::ECONNRESET, Net::HTTPBadResponse,
|
31
|
-
Net::HTTPHeaderSyntaxError, Net::ProtocolError => e
|
32
|
-
Opentsdb.logger.error "Http request error: #{e}"
|
33
|
-
false
|
34
|
-
end
|
35
|
-
end
|
36
|
-
end
|