opentsdb-consumer 0.1.1 → 0.2.0

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: 120fe03858090bf837aea765cb2b8b8f56bdee40
4
- data.tar.gz: 512c438dd6b56f325829f2b6267c5993d7a55827
3
+ metadata.gz: 0c0623e847a6d3b545478b8f88ac2713f6c59ce8
4
+ data.tar.gz: 2582a841255df62953b4bf9449abe1d88c650491
5
5
  SHA512:
6
- metadata.gz: 6b5aa02ca4b1d2b906f2307eee3bb2054f79685a443805677d59675bacb5e44533fc46acbfaf2ed676f1e99fd7fcfb409f0f0122e33c31f51e890a91e7d70916
7
- data.tar.gz: 0dabff64a9cfc3668df20b7c061724cf801ca83fa2138b7ca064bcbffe96421ba33f46c265fddfb3affff308e694f717338cbb51853e5af390dd120771384b7a
6
+ metadata.gz: 6502e41191581754ae67e3a40096fea1ed1b8b6c764358d0d6bd2b3a7a5234b2bbc354754c596fb6e3b983d8c4cc7024fc8e5857028429668f5ec827285d57f7
7
+ data.tar.gz: c9ad627a9fc31950b5a5742469093e51a98a9d844029518e8dac69303d0c349924665607b185154646e9ade46d2f73044dd4cd7135d366538fdbd9d9086f02a6
data/Gemfile CHANGED
@@ -1,4 +1,4 @@
1
- source 'http://rubygems.org'
1
+ source 'https://rubygems.org'
2
2
 
3
3
  gem 'excon', '~> 0.45.0'
4
4
 
data/Gemfile.lock CHANGED
@@ -1,5 +1,5 @@
1
1
  GEM
2
- remote: http://rubygems.org/
2
+ remote: https://rubygems.org/
3
3
  specs:
4
4
  addressable (2.3.8)
5
5
  builder (3.2.2)
data/README.md CHANGED
@@ -20,7 +20,7 @@ gem 'opentsdb-consumer'
20
20
  ```ruby
21
21
  client = OpenTSDBConsumer::Client.new host: 'metrics.yourdomain.com', port: 4242
22
22
  metric = OpenTSDBConsumer::Metric.new name: 'my.metric', rate: true, aggregator: 'avg'
23
- OpenTSDBConsumer::Query.new(metric, client).run start: '24h-ago'
23
+ OpenTSDBConsumer::Query.new([metric], client).run start: '24h-ago'
24
24
  ```
25
25
 
26
26
  ## Contributing to opentsdb-consumer
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.1
1
+ 0.2.0
@@ -1,19 +1,20 @@
1
1
  require 'excon'
2
+ require 'forwardable'
2
3
 
3
4
  module OpenTSDBConsumer
4
5
  class Client
6
+ extend Forwardable
5
7
  attr_reader :host, :port, :connection
6
8
 
9
+ delegate get: :connection
10
+ delegate post: :connection
11
+
7
12
  def initialize(host: 'localhost', port: 4242, options: {})
8
13
  @host = host
9
14
  @port = port
10
15
  @connection = Excon.new url, options
11
16
  end
12
17
 
13
- def get(params)
14
- connection.get params
15
- end
16
-
17
18
  def url
18
19
  "http://#{host}:#{port}/api/query"
19
20
  end
@@ -1,17 +1,28 @@
1
1
  module OpenTSDBConsumer
2
2
  class Metric
3
- attr_reader :name, :aggregator, :rate, :tags
3
+ attr_reader :name, :aggregator, :rate, :downsample, :tags
4
4
 
5
- def initialize(name: nil, aggregator: 'sum', rate: false, tags: {})
5
+ def initialize(name: nil, aggregator: 'sum', rate: false, downsample: '10m-avg', tags: {})
6
6
  @name = name
7
7
  @aggregator = aggregator
8
8
  @rate = rate
9
+ @downsample = downsample
9
10
  @tags = tags
10
11
  end
11
12
 
12
13
  def to_s
13
14
  query = tags.any? ? "{#{tags_to_query}}" : ''
14
- [aggregator, rate_to_query, name].compact.join(':') + query
15
+ [aggregator, downsample, rate_to_query, name].compact.join(':') + query
16
+ end
17
+
18
+ def to_h
19
+ {
20
+ aggregator: aggregator,
21
+ downsample: downsample,
22
+ metric: name,
23
+ rate: rate,
24
+ tags: tags,
25
+ }
15
26
  end
16
27
 
17
28
  private
@@ -2,15 +2,15 @@ require 'json'
2
2
 
3
3
  module OpenTSDBConsumer
4
4
  class Query
5
- attr_accessor :metric, :client
5
+ attr_accessor :metrics, :client
6
6
 
7
- def initialize(metric, client)
8
- @metric = metric
7
+ def initialize(metrics, client)
8
+ @metrics = [metrics].flatten
9
9
  @client = client
10
10
  end
11
11
 
12
12
  def run(start: '1h-ago')
13
- response = client.get query: { m: metric, start: start }
13
+ response = client.post body: request_body(start)
14
14
  parsed_body = JSON.parse(response.body)
15
15
  return OpenTSDBConsumer::Result.build(parsed_body) if response.status < 400
16
16
 
@@ -20,6 +20,10 @@ module OpenTSDBConsumer
20
20
 
21
21
  private
22
22
 
23
+ def request_body(start)
24
+ { start: start, queries: metrics.map(&:to_h) }.to_json
25
+ end
26
+
23
27
  def error_for_response(response_message)
24
28
  case response_message
25
29
  when /^No such name for 'tagv'/
@@ -2,16 +2,16 @@
2
2
  # DO NOT EDIT THIS FILE DIRECTLY
3
3
  # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
4
  # -*- encoding: utf-8 -*-
5
- # stub: opentsdb-consumer 0.1.1 ruby lib
5
+ # stub: opentsdb-consumer 0.2.0 ruby lib
6
6
 
7
7
  Gem::Specification.new do |s|
8
8
  s.name = "opentsdb-consumer"
9
- s.version = "0.1.1"
9
+ s.version = "0.2.0"
10
10
 
11
11
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
12
12
  s.require_paths = ["lib"]
13
13
  s.authors = ["Philippe H\u{e4}ssig"]
14
- s.date = "2015-11-10"
14
+ s.date = "2015-11-18"
15
15
  s.description = "Client library to consume metrics from OpenTSDB"
16
16
  s.email = "phil@nine.ch"
17
17
  s.executables = ["opentsdb-consumer"]
@@ -6,23 +6,32 @@ RSpec.describe OpenTSDBConsumer::Metric do
6
6
  let(:aggregator) { 'avg' }
7
7
  let(:rate) { false }
8
8
  let(:tags) { {} }
9
- let(:metric) { described_class.new name: name, aggregator: aggregator, rate: rate, tags: tags }
9
+ let(:downsample) { '1h-avg' }
10
+ let(:metric) do
11
+ described_class.new name: name, aggregator: aggregator, rate: rate, tags: tags, downsample: downsample
12
+ end
10
13
 
11
14
  describe '#to_s' do
12
15
  subject { metric.to_s }
13
16
 
14
17
  context 'without tags' do
15
- it { should eq 'avg:my.metric' }
18
+ it { should eq 'avg:1h-avg:my.metric' }
16
19
  end
17
20
 
18
21
  context 'with tags' do
19
22
  let(:tags) { { host: 'metrics01', cpu: 0 } }
20
- it { should eq 'avg:my.metric{host=metrics01,cpu=0}' }
23
+ it { should eq 'avg:1h-avg:my.metric{host=metrics01,cpu=0}' }
21
24
  end
22
25
 
23
26
  context 'with rate' do
24
27
  let(:rate) { true }
25
- it { should eq 'avg:rate:my.metric' }
28
+ it { should eq 'avg:1h-avg:rate:my.metric' }
26
29
  end
27
30
  end
31
+
32
+ describe '#to_h' do
33
+ subject { metric.to_h }
34
+
35
+ it { should eq(aggregator: aggregator, downsample: downsample, metric: name, rate: rate, tags: tags) }
36
+ end
28
37
  end
@@ -2,30 +2,33 @@ require 'spec_helper'
2
2
 
3
3
  RSpec.describe OpenTSDBConsumer::Query do
4
4
  let(:metric) { OpenTSDBConsumer::Metric.new name: 'my.metric' }
5
- let(:client) { instance_double 'OpenTSDBConsumer::Client', get: mock_response }
5
+ let(:client) { instance_double 'OpenTSDBConsumer::Client', post: mock_response }
6
6
  let(:query) { described_class.new metric, client }
7
7
  let(:status) { 200 }
8
8
  let(:response_body) { '[{}]' }
9
9
  let(:mock_response) { instance_double 'Excon::Response', status: status, body: response_body }
10
+ let(:expected_query) do
11
+ { aggregator: 'sum', downsample: '10m-avg', metric: 'my.metric', rate: false, tags: {} }
12
+ end
10
13
 
11
14
  describe '#run' do
12
15
  context 'successfuly' do
13
16
  it 'queries the server for the given metric' do
14
- expect(client).to receive(:get).
15
- with(query: { m: metric, start: '1h-ago' }).
17
+ expect(client).to receive(:post).
18
+ with(body: { start: '1h-ago', queries: [expected_query] }.to_json).
16
19
  and_return(mock_response)
17
20
  query.run
18
21
  end
19
22
 
20
23
  it 'allows to set a start time' do
21
- expect(client).to receive(:get).
22
- with(query: { m: metric, start: '24h-ago' }).
24
+ expect(client).to receive(:post).
25
+ with(body: { start: '24h-ago', queries: [expected_query] }.to_json).
23
26
  and_return(mock_response)
24
27
  query.run start: '24h-ago'
25
28
  end
26
29
 
27
30
  it 'returns the parsed body' do
28
- expect(client).to receive(:get).and_return(mock_response)
31
+ expect(client).to receive(:post).and_return(mock_response)
29
32
  expect(query.run).to be_a OpenTSDBConsumer::Result
30
33
  end
31
34
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: opentsdb-consumer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Philippe Hässig
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-11-10 00:00:00.000000000 Z
11
+ date: 2015-11-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: excon