opentsdb-ruby 0.0.3 → 0.0.4
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 +4 -4
- data/README.md +44 -9
- data/lib/opentsdb/client.rb +9 -6
- data/lib/opentsdb/http_client.rb +1 -1
- data/lib/opentsdb/query_errors.rb +5 -0
- data/lib/opentsdb/query_param.rb +56 -49
- data/lib/opentsdb/query_parser.rb +29 -14
- data/lib/opentsdb/query_rate_options.rb +16 -0
- data/lib/opentsdb/version.rb +1 -1
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c6358b79d7e73fa08c4da97bdb3d195d24d08f20
|
4
|
+
data.tar.gz: 1cc144968d1f8850dabb531163ed5268ffb54cea
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bff20bcf46714f1adff97035646bd0d3b387d086e5393866a966d7f72623e514fa2639c67963449043cd2af4131a80d05a44845edbaea41c6d36733b1d3803f8
|
7
|
+
data.tar.gz: 44663760bc00891ab53b225c90b6d5613f2156b3eef69d08816fe10cb88647f56619a184d7dafceef0d5ee34374ae9b57be50e23e26aa3848d0f5ec04c7f3610
|
data/README.md
CHANGED
@@ -26,12 +26,12 @@ Or install it yourself as:
|
|
26
26
|
### Configure opentsdb
|
27
27
|
|
28
28
|
```ruby
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
29
|
+
#config/initializers/opentsdb.rb
|
30
|
+
#require 'opentsdb'
|
31
|
+
Opentsdb.configure do |config|
|
32
|
+
config.host = 'localhost' # opentsdb server host default: localhost
|
33
|
+
config.port = 4242 # opentsdb server port default: 4242
|
34
|
+
end
|
35
35
|
```
|
36
36
|
|
37
37
|
### Usage
|
@@ -43,18 +43,53 @@ Or install it yourself as:
|
|
43
43
|
# opensted
|
44
44
|
client = Opentsdb::Client.new(params)
|
45
45
|
result = client.query # opentsdb json result
|
46
|
-
|
46
|
+
# => { status: 'ok', condition: #<Opentsdb::QueryParam: @metric="system.load.1",..., result: '[{"metric": "system.load.1", "tags": ... "dps":[...]}]}'
|
47
47
|
|
48
48
|
# complicate query params
|
49
49
|
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
50
|
client = Opentsdb::Client.new(params)
|
51
51
|
result = client.query # opentsdb json result
|
52
|
-
|
52
|
+
# => { status: 'ok', condition: #<Opentsdb::QueryParam: @metric="system.load.1",..., result: '[{"metric": "system.load.1", "tags": ... "dps":[...]}]}'
|
53
53
|
|
54
|
-
# reconfig opentsdb host port
|
54
|
+
# reconfig opentsdb host and port
|
55
55
|
params = { host: '192.168.0.100', port: 8000, q: 'avg:system.load.1{host=*}' }
|
56
56
|
client = Opentsdb::Client.new(params)
|
57
57
|
result = client.query # opentsdb json result
|
58
|
+
# => { status: 'ok', condition: #<Opentsdb::QueryParam: @metric="system.load.1",..., result: '[{"metric": "system.load.1", "tags": ... "dps":[...]}]}'
|
59
|
+
|
60
|
+
#query exception
|
61
|
+
client = Opentsdb::Client.new(q: 'avg:unknown_metric')
|
62
|
+
result = client.query # opentsdb json result
|
63
|
+
# => { status: 'error', condition: #<Opentsdb::QueryParam: @metric="system.load.1",..., result: '{"error":{"code":400,"message":"No such name for 'metrics'...}}'
|
64
|
+
|
58
65
|
```
|
59
66
|
|
67
|
+
### Contributing
|
68
|
+
|
69
|
+
#### Fork the Project
|
70
|
+
|
71
|
+
```
|
72
|
+
$ git https://github.com/cloudinsight/opentsdb-ruby.git
|
73
|
+
$ cd opentsdb-ruby
|
74
|
+
$ git remote add upstream https://github.com/cloudinsight/opentsdb-ruby.git
|
75
|
+
```
|
76
|
+
#### Create a Toptic Branch
|
77
|
+
|
78
|
+
```
|
79
|
+
$ git checkout master
|
80
|
+
$ git pull upstream master
|
81
|
+
$ git checkout -b my-feature-branch
|
82
|
+
```
|
83
|
+
#### Run Test
|
84
|
+
```
|
85
|
+
$ bundle exec rake test
|
86
|
+
```
|
87
|
+
#### Make a Pull Request
|
88
|
+
|
89
|
+
Click the `'Pull Request'` button and fill out the form.
|
90
|
+
|
91
|
+
### License
|
92
|
+
|
93
|
+
MIT License. See [LICENSE](https://github.com/cloudinsight/opentsdb-ruby/blob/master/LICENSE.md) for details.
|
94
|
+
|
60
95
|
|
data/lib/opentsdb/client.rb
CHANGED
@@ -6,24 +6,27 @@ module Opentsdb
|
|
6
6
|
|
7
7
|
def_instance_delegators :@http, :post
|
8
8
|
|
9
|
-
attr_reader :host, :port
|
9
|
+
attr_reader :host, :port
|
10
|
+
attr_accessor :query_commads
|
10
11
|
|
11
12
|
def initialize(options = {})
|
12
13
|
@host = options.delete(:host) || Opentsdb.host
|
13
14
|
@port = options.delete(:port) || Opentsdb.port
|
14
15
|
@http = HttpClient.new
|
15
|
-
@
|
16
|
+
@query_commads = parse_queries options
|
16
17
|
end
|
17
18
|
|
18
19
|
def query
|
19
|
-
[].tap do |
|
20
|
-
|
21
|
-
|
20
|
+
[].tap do |results|
|
21
|
+
query_commads.each do |query_commad|
|
22
|
+
res = post(query_uri, query_commad.to_json)
|
23
|
+
status = res.code.to_i == 200 ? 'ok' : 'error'
|
24
|
+
results << { status: status, condition: query_commad, result: res.body }
|
22
25
|
end
|
23
26
|
end
|
24
27
|
end
|
25
28
|
|
26
|
-
def parse_queries
|
29
|
+
def parse_queries(query_options = {})
|
27
30
|
[].tap do |qs|
|
28
31
|
query_options[:q].split(';').each do |q|
|
29
32
|
query = QueryParser.parse(q)
|
data/lib/opentsdb/http_client.rb
CHANGED
@@ -25,7 +25,7 @@ module Opentsdb
|
|
25
25
|
time = Benchmark.realtime do
|
26
26
|
res = yield(http)
|
27
27
|
end
|
28
|
-
Opentsdb.logger.info "Response: #{res.code}
|
28
|
+
Opentsdb.logger.info "Response Code: #{res.code} Consume: #{time} s"
|
29
29
|
res
|
30
30
|
rescue Timeout::Error, Errno::ECONNRESET, Net::HTTPBadResponse,
|
31
31
|
Net::HTTPHeaderSyntaxError, Net::ProtocolError => e
|
data/lib/opentsdb/query_param.rb
CHANGED
@@ -1,63 +1,70 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
def initialize(options = {})
|
7
|
-
@aggregator = options[:aggregator]
|
8
|
-
@rate = options[:rate] || false
|
9
|
-
@metric = options[:metric]
|
10
|
-
@tags = options[:tags] || {}
|
11
|
-
end
|
1
|
+
module Opentsdb
|
2
|
+
# :nodoc:
|
3
|
+
class QueryParam
|
4
|
+
attr_accessor :aggregator, :rate, :metric, :tags, :interval, :downsample, :rate_options, :group
|
5
|
+
attr_accessor :start_time, :end_time
|
12
6
|
|
13
|
-
|
14
|
-
|
15
|
-
|
7
|
+
def initialize(options = {})
|
8
|
+
@aggregator = options[:aggregator]
|
9
|
+
@rate = options[:rate] || false
|
10
|
+
@metric = options[:metric]
|
11
|
+
@tags = options[:tags] || {}
|
12
|
+
@rate_options = options[:rate_options]
|
13
|
+
@group = options[:group] || []
|
14
|
+
end
|
16
15
|
|
17
|
-
|
18
|
-
|
19
|
-
|
16
|
+
def start_time
|
17
|
+
@start_time > 0 ? to_ms(@start_time) : (end_time - 3_600_000) # 1 hour ago
|
18
|
+
end
|
20
19
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
h[:end] = end_time
|
25
|
-
h[:queries] = [queries]
|
26
|
-
end.to_json
|
27
|
-
end
|
20
|
+
def end_time
|
21
|
+
@end_time > 0 ? to_ms(@end_time) : to_ms(Time.now)
|
22
|
+
end
|
28
23
|
|
29
|
-
|
30
|
-
|
31
|
-
|
24
|
+
def to_json
|
25
|
+
{}.tap do |h|
|
26
|
+
h[:start] = start_time
|
27
|
+
h[:end] = end_time
|
28
|
+
h[:queries] = [queries]
|
29
|
+
end.to_json
|
32
30
|
end
|
33
|
-
end
|
34
31
|
|
35
|
-
|
36
|
-
|
37
|
-
|
32
|
+
def to_query_tags
|
33
|
+
{}.tap do |qtags|
|
34
|
+
tags.each do |tagk, tagv|
|
35
|
+
qtags[tagk] = tagv.to_a.compact.join('|') if tagv.is_a?(Array) || tagv.is_a?(Set)
|
36
|
+
qtags[tagk] = '*' if tagv.nil? || tagv.empty?
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
38
40
|
|
39
|
-
|
41
|
+
def downsample
|
42
|
+
"#{interval}s-#{aggregator_for}" if interval
|
43
|
+
end
|
40
44
|
|
41
|
-
|
42
|
-
(time.to_f * 1000).to_i
|
43
|
-
end
|
45
|
+
private
|
44
46
|
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
qh
|
51
|
-
|
47
|
+
def to_ms(time = Time.now)
|
48
|
+
(time.to_f * 1000).to_i
|
49
|
+
end
|
50
|
+
|
51
|
+
def queries
|
52
|
+
{}.tap do |qh|
|
53
|
+
qh[:aggregator] = aggregator
|
54
|
+
qh[:rate] = rate
|
55
|
+
qh[:metric] = metric
|
56
|
+
qh[:tags] = to_query_tags
|
57
|
+
qh[:downsample] = downsample if downsample
|
58
|
+
end
|
52
59
|
end
|
53
|
-
end
|
54
60
|
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
+
def aggregator_for
|
62
|
+
case aggregator
|
63
|
+
when 'sum', 'avg' then 'avg'
|
64
|
+
when 'min' then 'mimmin'
|
65
|
+
when 'max' then 'mimmax'
|
66
|
+
else 'avg'
|
67
|
+
end
|
61
68
|
end
|
62
69
|
end
|
63
70
|
end
|
@@ -8,18 +8,20 @@ module Opentsdb
|
|
8
8
|
metric_query = {}
|
9
9
|
metric_query[:aggregator] = parts[0]
|
10
10
|
metric_query[:rate] = false
|
11
|
-
|
11
|
+
metic = parts[1]
|
12
12
|
if parts.size == 3
|
13
|
-
|
14
|
-
|
13
|
+
if metic.start_with?('rate')
|
14
|
+
metric_query[:rate] = true
|
15
|
+
metric_query[:rate_options] = parse_rate(metic) if temp.index('{')
|
16
|
+
end
|
17
|
+
metic = parts[2]
|
15
18
|
end
|
16
|
-
|
17
|
-
QueryParam.new metric_query.merge(query_metric)
|
19
|
+
QueryParam.new metric_query.merge(parse_metric(metic))
|
18
20
|
end
|
19
21
|
|
20
22
|
private
|
21
23
|
|
22
|
-
def
|
24
|
+
def parse_metric(metric)
|
23
25
|
start_index = metric.index('{')
|
24
26
|
return { metric: metric } unless start_index
|
25
27
|
{}.tap do |h|
|
@@ -34,16 +36,29 @@ module Opentsdb
|
|
34
36
|
end
|
35
37
|
end
|
36
38
|
|
39
|
+
def parse_rate(spec)
|
40
|
+
return QueryRateOptions.default if spec.size == 4
|
41
|
+
raise QueryErrors::InvalidRateOptionError, "invalid specification: #{spec}" if spec.size < 6
|
42
|
+
parts = split_for(spec[5..-1], ',')
|
43
|
+
if parts.empty? || parts.size > 3
|
44
|
+
raise QueryErrors::InvalidRateOptionError, "Incrrenct numbers of values: #{spec}"
|
45
|
+
end
|
46
|
+
is_counter = parts[0] == 'counter'
|
47
|
+
max = parts[1].nil? ? 9_999_999_999 : parts[1].to_i
|
48
|
+
reset = parts[2].nil? ? 0 : parts[2].to_i
|
49
|
+
QueryRateOptions.new is_counter, max, reset
|
50
|
+
end
|
51
|
+
|
37
52
|
def parse_tags(tags)
|
38
53
|
return if tags.nil?
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
54
|
+
Hash.new { |h, k| h[k] = [] }.tap do |rtags|
|
55
|
+
split_for(tags, ',').each do |tag|
|
56
|
+
tagk, tagv = split_for(tag, '=')
|
57
|
+
next if tagk.nil? || tagv.nil?
|
58
|
+
rtags[tagk] ||= []
|
59
|
+
rtags[tagk].concat tagv.split('|').uniq
|
60
|
+
end
|
45
61
|
end
|
46
|
-
rtags
|
47
62
|
end
|
48
63
|
|
49
64
|
def parse_groups(groups_str)
|
@@ -71,7 +86,7 @@ module Opentsdb
|
|
71
86
|
pos += 1
|
72
87
|
end
|
73
88
|
result[index] = string[start...pos]
|
74
|
-
result
|
89
|
+
result.map(&:strip)
|
75
90
|
end
|
76
91
|
end
|
77
92
|
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module Opentsdb
|
2
|
+
# :nodoc:
|
3
|
+
class QueryRateOptions
|
4
|
+
attr_accessor :counter, :counter_max, :reset_value
|
5
|
+
|
6
|
+
def initialize(counter, counter_max, reset_value)
|
7
|
+
@counter = counter
|
8
|
+
@counter_max = counter_max
|
9
|
+
@reset_value = reset_value
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.default
|
13
|
+
new(false, 9_999_999_999, 0)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
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.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- lizhe
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2016-05-
|
12
|
+
date: 2016-05-26 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rake
|
@@ -155,8 +155,10 @@ files:
|
|
155
155
|
- lib/opentsdb.rb
|
156
156
|
- lib/opentsdb/client.rb
|
157
157
|
- lib/opentsdb/http_client.rb
|
158
|
+
- lib/opentsdb/query_errors.rb
|
158
159
|
- lib/opentsdb/query_param.rb
|
159
160
|
- lib/opentsdb/query_parser.rb
|
161
|
+
- lib/opentsdb/query_rate_options.rb
|
160
162
|
- lib/opentsdb/version.rb
|
161
163
|
homepage: https://github.com/cloudinsight/opentsdb-ruby
|
162
164
|
licenses:
|