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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0965273870cf1d0e970171c7f3feb80e14de6050
4
- data.tar.gz: 7bd33e7972ef2a6c1f194fe4d042d2b137835f9d
3
+ metadata.gz: c6358b79d7e73fa08c4da97bdb3d195d24d08f20
4
+ data.tar.gz: 1cc144968d1f8850dabb531163ed5268ffb54cea
5
5
  SHA512:
6
- metadata.gz: b35e664047831e9afa4f5d93f0a1a58f868d65eb8b4b5b13b7b1a6bc2fab9e9c6c323cec0b88f87f228546bb954265d446ceeac7d4e44153b5ca7a805a53b280
7
- data.tar.gz: 2c155b03575e551bae84f72e8a0674857e7427486730a449c2d900de5f8d7db754ebb88e3627ed8aeda68b3b6436d6a0c56d1d29397bd34f057d47b622678e9c
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
- #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
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
 
@@ -6,24 +6,27 @@ module Opentsdb
6
6
 
7
7
  def_instance_delegators :@http, :post
8
8
 
9
- attr_reader :host, :port, :query_options
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
- @query_options = options
16
+ @query_commads = parse_queries options
16
17
  end
17
18
 
18
19
  def query
19
- [].tap do |data|
20
- parse_queries.each do |query_commad|
21
- data << post(query_uri, query_commad.to_json)
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)
@@ -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} consume: #{time} s"
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
@@ -0,0 +1,5 @@
1
+ module Opentsdb
2
+ module QueryErrors
3
+ class InvalidRateOptionError < StandardError; end
4
+ end
5
+ end
@@ -1,63 +1,70 @@
1
- # :nodoc:
2
- class QueryParam
3
- attr_accessor :aggregator, :rate, :metric, :tags, :downsample, :interval
4
- attr_accessor :start_time, :end_time, :queries
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
- def start_time
14
- @start_time > 0 ? to_ms(@start_time) : (end_time - 3_600_000) # 1 hour ago
15
- end
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
- def end_time
18
- @end_time > 0 ? to_ms(@end_time) : to_ms(Time.now)
19
- end
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
- def to_json
22
- {}.tap do |h|
23
- h[:start] = start_time
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
- def tags
30
- @tags.each do |tagk, tagv|
31
- @tags[tagk] = tagv.to_a.compact.join('|') if tagv.is_a?(Array) || tagv.is_a?(Set)
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
- def downsample
36
- "#{interval}s-#{aggregator_for}" if interval
37
- end
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
- private
41
+ def downsample
42
+ "#{interval}s-#{aggregator_for}" if interval
43
+ end
40
44
 
41
- def to_ms(time = Time.now)
42
- (time.to_f * 1000).to_i
43
- end
45
+ private
44
46
 
45
- def queries
46
- {}.tap do |qh|
47
- qh[:aggregator] = aggregator
48
- qh[:rate] = rate
49
- qh[:metric] = metric
50
- qh[:tags] = tags
51
- qh[:downsample] = downsample if downsample
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
- def aggregator_for
56
- case aggregator
57
- when 'sum', 'avg' then 'avg'
58
- when 'min' then 'mimmin'
59
- when 'max' then 'mimmax'
60
- else 'avg'
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
- temp = parts[1]
11
+ metic = parts[1]
12
12
  if parts.size == 3
13
- metric_query[:rate] = true if temp.start_with?('rate')
14
- temp = parts[2]
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
- query_metric = parse_query_metric(temp)
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 parse_query_metric(metric)
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
- rtags = {}
40
- split_for(tags, ',').each do |tag|
41
- tagk, tagv = split_for(tag, '=')
42
- next if tagk.nil? || tagv.nil?
43
- rtags[tagk] ||= []
44
- rtags[tagk].concat tagv.split('|').uniq
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
@@ -1,3 +1,3 @@
1
1
  module Opentsdb
2
- VERSION = '0.0.3'.freeze
2
+ VERSION = '0.0.4'.freeze
3
3
  end
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.3
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-24 00:00:00.000000000 Z
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: