opentsdb-ruby 0.0.6 → 0.0.7

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: 2a99bf6c5e5081e76e7acaf6eb4be0da7fd1fff8
4
- data.tar.gz: 908b257a99ee6a17acc14a306418ee0c3eebcaac
3
+ metadata.gz: a1285259961020932f0391df4f5cd77dbf56b499
4
+ data.tar.gz: a01d76c47e616c708612ca0ebcf4cf33e8d76f07
5
5
  SHA512:
6
- metadata.gz: 3dcf66e50ca1b1a0ec62882d743c78c5dcf967696931975cf65a4e1219346562fd485284151a9b51bd69b9f8aaeab4114d336aafcd8bfa029fe49967b762d3c5
7
- data.tar.gz: 4dbc3ea4fe4caeb35ffcd660d336e0c45244814d0d8f252bf0c8823ce193130888fd222ca5e0938072e276b8b6db91e50329160ea6c4023815602c5648b1ed21
6
+ metadata.gz: 3d5665f76f11f40d806197e7c7ed1cab4ed5ba098d2d4644b1e6c263e958e89bb95681dea62519788a53dd76b3a2aa8aaf49079ef5c96afaf7cc709a302f36e1
7
+ data.tar.gz: f9258a781d687b1933d32c667424194a4d6cdf74c563ca0d5a870650076afb9bf55789b4101a1ea7f8d76ec851405f12bdb13741b7ef00e1d0e5c4025d90d617
data/README.md CHANGED
@@ -3,22 +3,27 @@
3
3
  Ruby client for OpenTSDB HTTP Query API.
4
4
 
5
5
  [![Gem Version](http://img.shields.io/gem/v/opentsdb-ruby.svg)](https://rubygems.org/gems/opentsdb-ruby) [![Build Status](https://travis-ci.org/cloudinsight/opentsdb-ruby.png)](https://travis-ci.org/cloudinsight/opentsdb-ruby) [![Code Climate](https://codeclimate.com/github/cloudinsight/opentsdb-ruby/badges/gpa.svg)](https://codeclimate.com/github/cloudinsight/opentsdb-ruby) [![Test Coverage](https://codeclimate.com/github/cloudinsight/opentsdb-ruby/badges/coverage.svg)](https://codeclimate.com/github/cloudinsight/opentsdb-ruby/coverage)
6
+ [![MIT licensed](https://img.shields.io/badge/license-MIT-blue.svg)](./LICENSE.md)
6
7
 
7
8
  ## Installation
8
9
 
9
10
  Add this line to your application's Gemfile:
10
11
 
11
12
  ```ruby
12
- gem 'opentsdb-ruby', require 'opentsdb'
13
+ gem 'opentsdb-ruby', require 'opentsdb'
13
14
  ```
14
15
 
15
16
  And then execute:
16
17
 
17
- $ bundle
18
+ ```shell
19
+ $ bundle
20
+ ```
18
21
 
19
22
  Or install it yourself as:
20
23
 
21
- $ gem install opentsdb-ruby
24
+ ```shell
25
+ $ gem install opentsdb-ruby
26
+ ```
22
27
 
23
28
 
24
29
  ## Quick Start Guide
@@ -26,66 +31,67 @@ Or install it yourself as:
26
31
  ### Configure opentsdb
27
32
 
28
33
  ```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
34
+ #config/initializers/opentsdb.rb
35
+ #require 'opentsdb'
36
+ Opentsdb.configure do |config|
37
+ config.host = 'localhost' # opentsdb server host default: localhost
38
+ config.port = 4242 # opentsdb server port default: 4242
39
+ end
35
40
  ```
36
41
 
37
42
  ### Usage
38
43
 
39
44
  ```ruby
40
-
41
- # define simple query params
42
- params = { begin: Time.now.ago(1.hour), q: 'avg:system.load.1{host=*}' }
43
- client = Opentsdb::Client.new(params)
44
- result = client.query
45
- # => { status: 'ok', condition: #<Opentsdb::QueryParam: @metric="system.load.1",..., result: '[{"metric": "system.load.1", "tags": ... "dps":[...]}]}'
46
-
47
- # complicate query params
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 }
49
- client = Opentsdb::Client.new(params)
50
- result = client.query
51
- # => { status: 'ok', condition: #<Opentsdb::QueryParam: @metric="system.load.1",..., result: '[{"metric": "system.load.1", "tags": ... "dps":[...]}]}'
52
-
53
- # reconfig opentsdb host and port
54
- params = { host: '192.168.0.100', port: 8000, q: 'avg:system.load.1{host=*}' }
55
- client = Opentsdb::Client.new(params)
56
- result = client.query
57
- # => { status: 'ok', condition: #<Opentsdb::QueryParam: @metric="system.load.1",..., result: '[{"metric": "system.load.1", "tags": ... "dps":[...]}]}'
58
-
59
- #query exception
60
- client = Opentsdb::Client.new(q: 'avg:unknown_metric')
61
- result = client.query
62
- # => { status: 'error', condition: #<Opentsdb::QueryParam: @metric="system.load.1",..., result: '{"error":{"code":400,"message":"No such name for 'metrics'...}}'
63
-
45
+ # define simple query params
46
+ params = { begin: Time.now.ago(1.hour), q: 'avg:system.load.1{host=*}' }
47
+ client = Opentsdb::Client.new(params)
48
+ result = client.query
49
+ # => { status: 'ok', condition: #<Opentsdb::QueryParam: @metric="system.load.1",..., result: '[{"metric": "system.load.1", "tags": ... "dps":[...]}]}'
50
+
51
+ # complicate query params
52
+ 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 }
53
+ client = Opentsdb::Client.new(params)
54
+ result = client.query
55
+ # => { status: 'ok', condition: #<Opentsdb::QueryParam: @metric="system.load.1",..., result: '[{"metric": "system.load.1", "tags": ... "dps":[...]}]}'
56
+
57
+ # reconfig opentsdb host and port
58
+ params = { host: '192.168.0.100', port: 8000, q: 'avg:system.load.1{host=*}' }
59
+ client = Opentsdb::Client.new(params)
60
+ result = client.query
61
+ # => { status: 'ok', condition: #<Opentsdb::QueryParam: @metric="system.load.1",..., result: '[{"metric": "system.load.1", "tags": ... "dps":[...]}]}'
62
+
63
+ #query exception
64
+ client = Opentsdb::Client.new(q: 'avg:unknown_metric')
65
+ result = client.query
66
+ # => { status: 'error', condition: #<Opentsdb::QueryParam: @metric="system.load.1",..., result: '{"error":{"code":400,"message":"No such name for 'metrics'...}}'
64
67
  ```
65
68
 
66
69
  ### Contributing
67
70
 
68
71
  #### Fork the Project
69
72
 
70
- ```
71
- $ git https://github.com/cloudinsight/opentsdb-ruby.git
72
- $ cd opentsdb-ruby
73
- $ git remote add upstream https://github.com/cloudinsight/opentsdb-ruby.git
74
- ```
73
+ ```shell
74
+ $ git https://github.com/cloudinsight/opentsdb-ruby.git
75
+ $ cd opentsdb-ruby
76
+ $ git remote add upstream https://github.com/cloudinsight/opentsdb-ruby.git
77
+ ```
75
78
  #### Create a Toptic Branch
76
79
 
77
- ```
78
- $ git checkout master
79
- $ git pull upstream master
80
- $ git checkout -b my-feature-branch
81
- ```
80
+ ```shell
81
+ $ git checkout master
82
+ $ git pull upstream master
83
+ $ git checkout -b my-feature-branch
84
+ ```
85
+
82
86
  #### Run Test
83
- ```
84
- $ bundle exec rake test
85
- ```
87
+
88
+ ```shell
89
+ $ bundle exec rake test
90
+ ```
91
+
86
92
  #### Make a Pull Request
87
93
 
88
- Click the `'Pull Request'` button and fill out the form.
94
+ Click the `'Pull Request'` button and fill out the form.
89
95
 
90
96
  ### License
91
97
 
@@ -2,7 +2,7 @@
2
2
  module Opentsdb
3
3
  #:nodoc:
4
4
  class << self
5
- attr_accessor :host, :port
5
+ attr_accessor :host, :port, :logger
6
6
 
7
7
  def configure
8
8
  yield self
@@ -18,7 +18,11 @@ module Opentsdb
18
18
  def query
19
19
  [].tap do |results|
20
20
  query_commads.each do |query_commad|
21
+ start = Time.now
22
+ Opentsdb.logger.debug "[OpenTSDB] query: #{query_commad.to_json}"
21
23
  res = post query_commad.to_json
24
+ Opentsdb.logger.debug "[OpenTSDB] result: #{res.body}"
25
+ Opentsdb.logger.info "[OpenTSDB] consume: #{Time.now - start} s"
22
26
  status = res.status.to_i == 200 ? 'ok' : 'error'
23
27
  results << { status: status, condition: query_commad, result: res.body }
24
28
  end
@@ -14,6 +14,7 @@ module Opentsdb
14
14
  req.headers = headers
15
15
  req.body = body
16
16
  req.options.timeout = 5
17
+ req.options.open_timeout = 5
17
18
  end
18
19
  end
19
20
 
@@ -26,8 +27,8 @@ module Opentsdb
26
27
  def connection
27
28
  @connection ||= begin
28
29
  ::Faraday.new(url: url) do |faraday|
29
- faraday.request :url_encoded # form-encode POST params
30
- faraday.response :logger # log requests to STDOUT
30
+ faraday.request :url_encoded # form-encode POST params
31
+ faraday.response :logger # log requests to STDOUT
31
32
  faraday.adapter auto_detect_adapter
32
33
  end
33
34
  end
@@ -2,17 +2,19 @@ module Opentsdb
2
2
  # :nodoc:
3
3
  class QueryParam
4
4
  attr_accessor :aggregator, :rate, :metric, :tags, :interval, :downsample, :rate_options, :group
5
+ attr_accessor :excluding_tags
5
6
  attr_accessor :start_time, :end_time
6
7
 
7
8
  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
- @start_time = 0
15
- @end_time = 0
9
+ @aggregator = options[:aggregator]
10
+ @rate = options[:rate] || false
11
+ @metric = options[:metric]
12
+ @tags = options[:tags] || {}
13
+ @excluding_tags = options[:excluding_tags] || {}
14
+ @rate_options = options[:rate_options]
15
+ @group = options[:group] || []
16
+ @start_time = 0
17
+ @end_time = 0
16
18
  end
17
19
 
18
20
  def start_time
@@ -27,7 +27,7 @@ module Opentsdb
27
27
  {}.tap do |h|
28
28
  h[:metric] = metric[0...start_index]
29
29
  end_index = metric.index('}')
30
- h[:tags] = parse_tags metric[(start_index + 1)...end_index].strip
30
+ h[:tags], h[:excluding_tags] = parse_tags metric[(start_index + 1)...end_index].strip
31
31
  groups_str = metric[(end_index + 1)..-1]
32
32
  if groups_str.size > 5 # length of by{} is 4
33
33
  h[:group] = parse_groups groups_str
@@ -51,14 +51,22 @@ module Opentsdb
51
51
 
52
52
  def parse_tags(tags)
53
53
  return if tags.nil?
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
54
+ over_tags = Hash.new { |h, k| h[k] = [] }
55
+ excluding_tags = Hash.new { |h, k| h[k] = [] }
56
+ split_for(tags, ',').each do |tag|
57
+ if tag =~ /!=/
58
+ append_tags(excluding_tags, tag, '!=')
59
+ else
60
+ append_tags(over_tags, tag)
60
61
  end
61
62
  end
63
+ [over_tags, excluding_tags]
64
+ end
65
+
66
+ def append_tags(tags, tag, token = '=')
67
+ tagk, tagv = tag.split(token)
68
+ return if tagk.nil? || tagv.nil?
69
+ tags[tagk].concat tagv.split('|').uniq
62
70
  end
63
71
 
64
72
  def parse_groups(groups_str)
@@ -1,3 +1,3 @@
1
1
  module Opentsdb
2
- VERSION = '0.0.6'.freeze
2
+ VERSION = '0.0.7'.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.6
4
+ version: 0.0.7
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-06-13 00:00:00.000000000 Z
12
+ date: 2016-12-28 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: faraday