opentsdb-ruby 0.0.6 → 0.0.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +53 -47
- data/lib/opentsdb.rb +1 -1
- data/lib/opentsdb/client.rb +4 -0
- data/lib/opentsdb/faraday.rb +3 -2
- data/lib/opentsdb/query_param.rb +10 -8
- data/lib/opentsdb/query_parser.rb +15 -7
- data/lib/opentsdb/version.rb +1 -1
- 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: a1285259961020932f0391df4f5cd77dbf56b499
|
4
|
+
data.tar.gz: a01d76c47e616c708612ca0ebcf4cf33e8d76f07
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
13
|
+
gem 'opentsdb-ruby', require 'opentsdb'
|
13
14
|
```
|
14
15
|
|
15
16
|
And then execute:
|
16
17
|
|
17
|
-
|
18
|
+
```shell
|
19
|
+
$ bundle
|
20
|
+
```
|
18
21
|
|
19
22
|
Or install it yourself as:
|
20
23
|
|
21
|
-
|
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
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
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
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
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
|
-
|
72
|
-
|
73
|
-
|
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
|
-
|
79
|
-
|
80
|
-
|
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
|
-
|
85
|
-
|
87
|
+
|
88
|
+
```shell
|
89
|
+
$ bundle exec rake test
|
90
|
+
```
|
91
|
+
|
86
92
|
#### Make a Pull Request
|
87
93
|
|
88
|
-
|
94
|
+
Click the `'Pull Request'` button and fill out the form.
|
89
95
|
|
90
96
|
### License
|
91
97
|
|
data/lib/opentsdb.rb
CHANGED
data/lib/opentsdb/client.rb
CHANGED
@@ -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
|
data/lib/opentsdb/faraday.rb
CHANGED
@@ -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
|
30
|
-
faraday.response :logger
|
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
|
data/lib/opentsdb/query_param.rb
CHANGED
@@ -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
|
9
|
-
@rate
|
10
|
-
@metric
|
11
|
-
@tags
|
12
|
-
@
|
13
|
-
@
|
14
|
-
@
|
15
|
-
@
|
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] = [] }
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
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)
|
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.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-
|
12
|
+
date: 2016-12-28 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: faraday
|