hawkular-client 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (38) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +17 -0
  3. data/.rubocop.yml +14 -0
  4. data/.travis.yml +5 -0
  5. data/CHANGES.rdoc +6 -0
  6. data/Gemfile +1 -1
  7. data/README.rdoc +2 -1
  8. data/Rakefile +6 -3
  9. data/hawkularclient.gemspec +17 -11
  10. data/lib/hawkularclient.rb +91 -98
  11. data/lib/metrics/metric_api.rb +83 -66
  12. data/lib/metrics/tenant_api.rb +3 -4
  13. data/lib/metrics/types.rb +9 -11
  14. data/lib/metrics/version.rb +1 -1
  15. data/spec/{endpoint.yml.example → endpoint.yml} +0 -0
  16. data/spec/integration/metric_spec.rb +223 -131
  17. data/spec/spec_helper.rb +14 -10
  18. data/spec/unit/client_spec.rb +24 -19
  19. data/spec/vcr/vcr_setup.rb +16 -0
  20. data/spec/vcr_cassettes/Availability_metrics/Should_create_Availability_definition_using_MetricDefinition_parameter.yml +87 -0
  21. data/spec/vcr_cassettes/Availability_metrics/Should_create_and_return_Availability_using_Hash_parameter.yml +87 -0
  22. data/spec/vcr_cassettes/Availability_metrics/Should_push_metric_data_to_non-existing_Availability.yml +126 -0
  23. data/spec/vcr_cassettes/Availability_metrics/Should_update_tags_for_Availability_definition.yml +210 -0
  24. data/spec/vcr_cassettes/Counter_metrics/Should_create_and_return_counter_using_Hash_parameter.yml +87 -0
  25. data/spec/vcr_cassettes/Counter_metrics/Should_create_counter_definition_using_MetricDefinition_parameter.yml +87 -0
  26. data/spec/vcr_cassettes/Counter_metrics/Should_push_metric_data_to_existing_counter.yml +249 -0
  27. data/spec/vcr_cassettes/Counter_metrics/Should_push_metric_data_to_non-existing_counter.yml +126 -0
  28. data/spec/vcr_cassettes/Gauge_metrics/Should_create_gauge_definition_using_Hash.yml +87 -0
  29. data/spec/vcr_cassettes/Gauge_metrics/Should_create_gauge_definition_using_MetricDefinition.yml +87 -0
  30. data/spec/vcr_cassettes/Gauge_metrics/Should_push_metric_data_to_existing_gauge.yml +249 -0
  31. data/spec/vcr_cassettes/Gauge_metrics/Should_push_metric_data_to_non-existing_gauge.yml +126 -0
  32. data/spec/vcr_cassettes/Gauge_metrics/Should_return_periods.yml +85 -0
  33. data/spec/vcr_cassettes/Gauge_metrics/Should_update_tags_for_gauge_definition.yml +210 -0
  34. data/spec/vcr_cassettes/Mixed_metrics/Should_send_mixed_metric_request.yml +284 -0
  35. data/spec/vcr_cassettes/Mixed_metrics/Should_send_mixed_metric_request_of_a_single_type.yml +249 -0
  36. data/spec/vcr_cassettes/Simple/Should_be_Cool.yml +208 -0
  37. data/spec/vcr_cassettes/Tenants/Should_create_and_return_tenant.yml +79 -0
  38. metadata +111 -25
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0c2bd7524ca6889a513ffac38121ecd21626d26c
4
- data.tar.gz: c8a90d9d4a3b042baf12c8fd046601d5442ccce1
3
+ metadata.gz: 6b21f0f14d7546fe626efb1cb3209c1085d22124
4
+ data.tar.gz: 9b92719c7441a05ce9cfa81a0d0f9b0cfae55553
5
5
  SHA512:
6
- metadata.gz: d3015ffa0cb3c714b9141a9b6783539b115c556a617911e931bc5905fd7e10bfa37727bd927a9e629e0021027495c01be5c62af8158810091624ce0a5790a6bb
7
- data.tar.gz: 64541ba6774f5f67582586acafd3c806861a51b1dc6452848716c8c552ada2db434b2a3c5be37cfd6aef059023d2fe9873a6b9c5f30ec5f706683701dbfe9006
6
+ metadata.gz: edd53f956611e6df5bba5bafccbbcdbebf2d0228e054216eaf838fd5876566550fb463c4717206121bc90803a1b10ae7b663fbfb3ef66e8ff25c36cc899c060f
7
+ data.tar.gz: 603b389c5e1712544c3511ba1f07b27b2a44860fb6cdf9972fc4763254c1b0d0f1cde6b769ce3fb2109cd6b140a31bf797e4bcffe690dbaf552466d99f91b52a
data/.gitignore CHANGED
@@ -8,6 +8,7 @@
8
8
  /test/tmp/
9
9
  /test/version_tmp/
10
10
  /tmp/
11
+ spec/vcr.log
11
12
 
12
13
  ## Specific to RubyMotion:
13
14
  .dat*
@@ -36,3 +37,19 @@ build/
36
37
 
37
38
  # test config
38
39
  spec/endpoint.yml
40
+
41
+ # IDE files
42
+ .project
43
+ .classpath
44
+ .settings
45
+ .idea
46
+ *.ipr
47
+ *.iml
48
+ *.iws
49
+ nb-configuration.xml
50
+
51
+ # KDE
52
+ .directory
53
+
54
+ # OSX
55
+ .DS_Store
data/.rubocop.yml ADDED
@@ -0,0 +1,14 @@
1
+ MethodLength:
2
+ Enabled: false
3
+ ClassLength:
4
+ Enabled: false
5
+ ModuleLength:
6
+ Enabled: false
7
+ Metrics/AbcSize:
8
+ Enabled: false
9
+ Documentation:
10
+ Enabled: false
11
+ Style/ClassAndModuleChildren:
12
+ Enabled: false
13
+ Metrics/LineLength:
14
+ Max: 100
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
4
+ # - 1.9.3
5
+ # - jruby-19mode
data/CHANGES.rdoc CHANGED
@@ -3,3 +3,9 @@
3
3
  This document describes the relevant changes beween releases of the
4
4
  _hawkular-client_ project.
5
5
 
6
+
7
+ === V 0.1.1
8
+
9
+ * Add VCR tests
10
+ * Metrics::Client.new now requries a hash for credentials
11
+ * Code cleanup and enforcement via RuboCop
data/Gemfile CHANGED
@@ -1,2 +1,2 @@
1
- source "http://rubygems.org"
1
+ source 'http://rubygems.org'
2
2
  gemspec
data/README.rdoc CHANGED
@@ -15,7 +15,8 @@ A Ruby Hawkular Client.
15
15
  == Running the tests
16
16
 
17
17
  * Copy the file spec/endpoint.yml.example to spec/endpoint.yml
18
- * Edit the endpoint properties (user, password and host name)
18
+ * Edit the endpoint properties in the file `spec/endpoint.yml`(user, password and host
19
+ name)
19
20
  * From command line run => rake spec
20
21
 
21
22
  == Logging
data/Rakefile CHANGED
@@ -1,9 +1,12 @@
1
- require "bundler/gem_tasks"
1
+ require 'bundler/gem_tasks'
2
2
  require 'rspec/core/rake_task'
3
+ require 'rubocop/rake_task'
3
4
 
4
5
  RSpec::Core::RakeTask.new(:spec) do |task|
5
- task.pattern = "./spec/**/*_spec.rb"
6
+ task.pattern = './spec/**/*_spec.rb'
6
7
  task.rspec_opts = ['--color']
7
8
  end
8
9
 
9
- task :default => :spec
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: [:rubocop, :spec]
@@ -4,27 +4,33 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
  require 'metrics/version'
5
5
 
6
6
  Gem::Specification.new do |gem|
7
- gem.name = "hawkular-client"
7
+ gem.name = 'hawkular-client'
8
8
  gem.version = Hawkular::Metrics::VERSION
9
- gem.authors = ['Libor Zoubek']
10
- gem.email = ['lzoubek@redhat.com']
11
- gem.homepage = "https://github.com/hawkular/hawkular-client-ruby"
12
- gem.summary = %q{A Ruby client for Hawkular}
13
- gem.description = <<-EOS
9
+ gem.authors = ['Libor Zoubek', 'Heiko W. Rupp', 'Federico Simoncelli']
10
+ gem.email = %w(lzoubek@redhat.com hrupp@redhat.com)
11
+ gem.homepage = 'https://github.com/hawkular/hawkular-client-ruby'
12
+ gem.summary = 'A Ruby client for Hawkular'
13
+ gem.license = 'ASL 2.0'
14
+ gem.required_ruby_version = '>= 2.0.0'
15
+ gem.description = <<-EOS
14
16
  A Ruby client for Hawkular
15
17
  EOS
16
18
 
17
- gem.files = `git ls-files`.split($/)
18
- gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
19
+ gem.files = `git ls-files -z`.split("\x0")
20
+ gem.executables = gem.files.grep(%r{^bin/}).map { |f| File.basename(f) }
19
21
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
20
- gem.require_paths = ["lib"]
22
+ gem.require_paths = ['lib']
21
23
 
22
24
  gem.add_runtime_dependency('rest-client')
23
25
  gem.add_development_dependency('shoulda')
24
26
  gem.add_development_dependency('rspec-rails', '~> 2.6')
25
27
  gem.add_development_dependency('rake')
26
- gem.add_development_dependency('yardoc')
28
+ gem.add_development_dependency('yard')
29
+ gem.add_development_dependency('webmock')
30
+ gem.add_development_dependency('vcr')
31
+ gem.add_development_dependency('rubocop', '= 0.34.2')
27
32
 
28
- gem.rdoc_options << '--title' << gem.name << '--main' << 'README.rdoc' << '--line-numbers' << '--inline-source'
33
+ gem.rdoc_options << '--title' << gem.name <<
34
+ '--main' << 'README.rdoc' << '--line-numbers' << '--inline-source'
29
35
  gem.extra_rdoc_files = ['README.rdoc', 'CHANGES.rdoc']
30
36
  end
@@ -1,40 +1,37 @@
1
1
 
2
2
 
3
- require "json"
4
- require "rest-client"
3
+ require 'json'
4
+ require 'rest-client'
5
5
 
6
6
  module Hawkular
7
7
  # Metrics module provides access to Hawkular Metrics REST API
8
8
  # @see http://www.hawkular.org/docs/rest/rest-metrics.html Hawkular Metrics REST API Documentation
9
9
  # @example Create Hawkular-Metrics client and start pushing some metric data
10
10
  # # create client instance
11
- # client = Hawkular::Metrics::Client::new("http://server","username","password",{"tenant" => "your tenant ID"})
12
- # # push gauge metric data for metric called "myGauge" (no need to create metric definition unless you wish to specify data retention)
11
+ # client = Hawkular::Metrics::Client::new("http://server","username",
12
+ # "password",{"tenant" => "your tenant ID"})
13
+ # # push gauge metric data for metric called "myGauge" (no need to create metric definition
14
+ # # unless you wish to specify data retention)
13
15
  # client.gauges.push_data("myGauge", {:value => 3.1415925})
14
16
  module Metrics
15
17
  end
16
18
  end
17
19
 
18
- require "metrics/types"
19
- require "metrics/tenant_api"
20
- require "metrics/metric_api"
21
-
20
+ require 'metrics/types'
21
+ require 'metrics/tenant_api'
22
+ require 'metrics/metric_api'
22
23
 
23
24
  module Hawkular::Metrics
24
-
25
25
  class HawkularException < StandardError
26
26
  def initialize(message)
27
27
  @message = message
28
28
  super
29
29
  end
30
30
 
31
- def message
32
- @message
33
- end
31
+ attr_reader :message
34
32
  end
35
33
 
36
34
  class Client
37
-
38
35
  # @!visibility private
39
36
  attr_reader :credentials, :entrypoint, :options
40
37
  # @return [Tenants] access tenants API
@@ -48,79 +45,83 @@ module Hawkular::Metrics
48
45
 
49
46
  # Construct a new Hawkular Metrics client class.
50
47
  # optional parameters
51
- # @param entrypoint [String]
52
- # @param username [String]
53
- # @param password [String]
48
+ # @param entrypoint [String] Base url of the Hawkular (metrics) server
49
+ # @param credentials Hash of username, password, token(optional)
54
50
  # @param options [Hash{String=>String}] client options
55
51
  # @example initialize with Hawkular-tenant option
56
- # Hawkular::Metrics::Client::new("http://server","username","password",{"tenant" => "your tenant ID"})
52
+ # Hawkular::Metrics::Client::new("http://server",
53
+ # {username:"username",password:"password"},
54
+ # {"tenant" => "your tenant ID"})
57
55
  #
58
- def initialize(entrypoint='http://localhost:8080/hawkular/metrics',username=nil, password=nil, options={})
56
+ def initialize(entrypoint = 'http://localhost:8080/hawkular/metrics',
57
+ credentials = {},
58
+ options = {})
59
59
  @entrypoint = entrypoint
60
- @credentials = { :username => username, :password => password }
60
+ @credentials = {
61
+ username: nil,
62
+ password: nil,
63
+ token: nil
64
+ }.merge(credentials)
61
65
  @options = {
62
- :tenant => nil,
66
+ tenant: nil,
67
+ ssl_ca_file: nil,
68
+ verify_ssl: OpenSSL::SSL::VERIFY_PEER,
69
+ ssl_client_cert: nil,
70
+ ssl_client_key: nil,
71
+ headers: {}
63
72
  }.merge(options)
64
73
 
65
- @tenants = Client::Tenants::new self
66
- @counters = Client::Counters::new self
67
- @gauges = Client::Gauges::new self
68
- @avail = Client::Availability::new self
74
+ @tenants = Client::Tenants.new self
75
+ @counters = Client::Counters.new self
76
+ @gauges = Client::Gauges.new self
77
+ @avail = Client::Availability.new self
69
78
  end
70
79
 
71
-
72
- def http_get(suburl, headers={})
73
- begin
74
- res = rest_client(suburl).get(http_headers(headers))
75
- puts "#{res}\n" if ENV['HAWKULARCLIENT_LOG_RESPONSE']
76
- res.empty? ? {} : JSON.parse(res)
77
- rescue
78
- handle_fault $!
79
- end
80
+ def http_get(suburl, headers = {})
81
+ res = rest_client(suburl).get(http_headers(headers))
82
+ puts "#{res}\n" if ENV['HAWKULARCLIENT_LOG_RESPONSE']
83
+ res.empty? ? {} : JSON.parse(res)
84
+ rescue
85
+ handle_fault $ERROR_INFO
80
86
  end
81
87
 
82
-
83
- def http_post(suburl, hash, headers={})
84
- begin
85
- body = JSON.generate(hash)
86
- res = rest_client(suburl).post(body, http_headers(headers))
87
- puts "#{res}\n" if ENV['HAWKULARCLIENT_LOG_RESPONSE']
88
- res.empty? ? {} : JSON.parse(res)
89
- rescue
90
- handle_fault $!
91
- end
88
+ def http_post(suburl, hash, headers = {})
89
+ body = JSON.generate(hash)
90
+ res = rest_client(suburl).post(body, http_headers(headers))
91
+ puts "#{res}\n" if ENV['HAWKULARCLIENT_LOG_RESPONSE']
92
+ res.empty? ? {} : JSON.parse(res)
93
+ rescue
94
+ handle_fault $ERROR_INFO
92
95
  end
93
96
 
94
- def http_put(suburl, hash, headers={})
95
- begin
96
- body = JSON.generate(hash)
97
- res = rest_client(suburl).put(body, http_headers(headers))
98
- puts "#{res}\n" if ENV['HAWKULARCLIENT_LOG_RESPONSE']
99
- res.empty? ? {} : JSON.parse(res)
100
- rescue
101
- handle_fault $!
102
- end
97
+ def http_put(suburl, hash, headers = {})
98
+ body = JSON.generate(hash)
99
+ res = rest_client(suburl).put(body, http_headers(headers))
100
+ puts "#{res}\n" if ENV['HAWKULARCLIENT_LOG_RESPONSE']
101
+ res.empty? ? {} : JSON.parse(res)
102
+ rescue
103
+ handle_fault $ERROR_INFO
103
104
  end
104
105
 
105
- def http_delete(suburl, headers={})
106
- begin
107
- res = rest_client(suburl).delete(http_headers(headers))
108
- puts "#{res}\n" if ENV['HAWKULARCLIENT_LOG_RESPONSE']
109
- res.empty? ? {} : JSON.parse(res)
110
- rescue
111
- handle_fault $!
112
- end
106
+ def http_delete(suburl, headers = {})
107
+ res = rest_client(suburl).delete(http_headers(headers))
108
+ puts "#{res}\n" if ENV['HAWKULARCLIENT_LOG_RESPONSE']
109
+ res.empty? ? {} : JSON.parse(res)
110
+ rescue
111
+ handle_fault $ERROR_INFO
113
112
  end
114
113
 
115
-
116
-
117
114
  # @!visibility private
118
115
  def rest_client(suburl)
119
116
  options[:timeout] = ENV['HAWKULARCLIENT_REST_TIMEOUT'] if ENV['HAWKULARCLIENT_REST_TIMEOUT']
117
+ options[:ssl_ca_file] = @options[:ssl_ca_file]
118
+ options[:verify_ssl] = @options[:verify_ssl]
119
+ options[:ssl_client_cert] = @options[:ssl_client_cert]
120
+ options[:ssl_client_key] = @options[:ssl_client_key]
121
+ options[:user] = @credentials[:username]
122
+ options[:password] = @credentials[:password]
120
123
  # strip @endpoint in case suburl is absolute
121
- if suburl.match(/^http/)
122
- suburl = suburl[@entrypoint.length,suburl.length]
123
- end
124
+ suburl = suburl[@entrypoint.length, suburl.length] if suburl.match(/^http/)
124
125
  RestClient::Resource.new(@entrypoint, options)[suburl]
125
126
  end
126
127
 
@@ -136,52 +137,44 @@ module Hawkular::Metrics
136
137
  end
137
138
 
138
139
  # @!visibility private
139
- def http_headers(headers ={})
140
- {}.merge(auth_header)
141
- .merge(tenant_header)
142
- .merge({
143
- :content_type => 'application/json',
144
- :accept => 'application/json',
145
- })
140
+ def http_headers(headers = {})
141
+ {}.merge(tenant_header)
142
+ .merge(token_header)
143
+ .merge(@options[:headers])
144
+ .merge(content_type: 'application/json',
145
+ accept: 'application/json')
146
146
  .merge(headers)
147
147
  end
148
148
 
149
149
  # timestamp of current time
150
150
  # @return [Integer] timestamp
151
151
  def now
152
- Integer(Time::now.to_f * 1000)
152
+ Integer(Time.now.to_f * 1000)
153
153
  end
154
154
 
155
155
  private
156
156
 
157
+ def token_header
158
+ @credentials[:token].nil? ? {} : { 'Authorization' => "Bearer #{@credentials[:token]}" }
159
+ end
157
160
 
158
- def tenant_header
159
- @options[:tenant].nil? ? {} : { :'Hawkular-Tenant' => @options[:tenant], "tenantId" => @options[:tenant] }
160
- end
161
-
162
-
163
- def auth_header
164
- if @credentials[:username].nil? and @credentials[:password].nil?
165
- return {}
166
- end
167
- # This is the method for strict_encode64:
168
- encoded_credentials = ["#{@credentials[:username]}:#{@credentials[:password]}"].pack("m0").gsub(/\n/,'')
169
- {:authorization => "Basic " + encoded_credentials }
170
- end
161
+ def tenant_header
162
+ @options[:tenant].nil? ? {} : { :'Hawkular-Tenant' => @options[:tenant],
163
+ 'tenantId' => @options[:tenant] }
164
+ end
171
165
 
172
- def handle_fault(f)
173
- if defined? f.http_body and !f.http_body.nil?
174
- begin
175
- fault = "#{f.errorMsg}\n%s\n" % JSON.parse(f.http_body)["errorMsg"]
176
- rescue
177
- fault = f.http_body
178
- raise HawkularException::new(fault)
179
- end
180
- else
181
- raise f
166
+ def handle_fault(f)
167
+ if f.respond_to?(:http_body) && !f.http_body.nil?
168
+ begin
169
+ json_body = JSON.parse(f.http_body)
170
+ fault_message = json_body['errorMsg'] || f.http_body
171
+ rescue JSON::ParserError
172
+ fault_message = f.http_body
182
173
  end
183
-
174
+ fail HawkularException, fault_message
175
+ else
176
+ fail f
184
177
  end
178
+ end
185
179
  end
186
-
187
180
  end
@@ -1,15 +1,15 @@
1
+ require 'erb'
2
+
1
3
  module Hawkular::Metrics
2
4
  class Client
3
-
4
- # @!visibility private
5
- def default_timestamp(array)
6
- n = now
7
- array.each { |p|
8
- p[:timestamp] ||= n
9
- }
10
- array
5
+ # @!visibility private
6
+ def default_timestamp(array)
7
+ n = now
8
+ array.each do |p|
9
+ p[:timestamp] ||= n
11
10
  end
12
-
11
+ array
12
+ end
13
13
 
14
14
  # Push data for multiple metrics of all supported types
15
15
  # @param gauges [Array]
@@ -17,29 +17,27 @@ module Hawkular::Metrics
17
17
  # @param availabilities [Array]
18
18
  # @example push datapoints of 2 counter metrics
19
19
  # client = Hawkular::Metrics::client::new
20
- # client.push_data(counters: [{:id => "counter1", :data => [{:value => 1}, {:value => 2}]},{:id => "counter2", :data => [{:value => 1}, {:value => 3}]}])
20
+ # client.push_data(counters: [{:id => "counter1", :data => [{:value => 1}, {:value => 2}]},
21
+ # {:id => "counter2", :data => [{:value => 1}, {:value => 3}]}])
21
22
  # @example push gauge and availability datapoints
22
- # client.push_data(gauges: [{:id => "gauge1", :data => [{:value => 1}, {:value => 2}]}], availabilities: [{:id => "avail1", :data => [{:value => "up"}]}])
23
+ # client.push_data(gauges: [{:id => "gauge1", :data => [{:value => 1}, {:value => 2}]}],
24
+ # availabilities: [{:id => "avail1", :data => [{:value => "up"}]}])
23
25
  def push_data(gauges: [], counters: [], availabilities: [])
24
- gauges.each { |g|
25
- default_timestamp g[:data]
26
- }
27
- counters.each { |g|
28
- default_timestamp g[:data]
29
- }
30
- availabilities.each { |g|
31
- default_timestamp g[:data]
32
- }
33
- data = {:gauges => gauges, :counters => counters, :availabilities => availabilities}
34
- http_post("/metrics/data", data)
26
+ gauges.each { |g| default_timestamp g[:data] }
27
+ counters.each { |g| default_timestamp g[:data] }
28
+ availabilities.each { |g| default_timestamp g[:data] }
29
+
30
+ data = { gauges: gauges, counters: counters, availabilities: availabilities }
31
+ http_post('/metrics/data', data)
35
32
  end
36
33
 
37
- # Base class for accessing metric definition and data of all types (counters, gauges, availabilities)
34
+ # Base class for accessing metric definition and data of all
35
+ # types (counters, gauges, availabilities).
38
36
  class Metrics
39
-
40
37
  # @param client [Client]
41
38
  # @param metricType [String] metric type (one of "counter", "gauge", "availability")
42
- # @param resource [String] REST resource name for accessing metrics of given type (one of "counters", "gauges", "availability")
39
+ # @param resource [String] REST resource name for accessing metrics
40
+ # of given type (one of "counters", "gauges", "availability")
43
41
  def initialize(client, metricType, resource)
44
42
  @client = client
45
43
  @type = metricType
@@ -50,37 +48,35 @@ module Hawkular::Metrics
50
48
  # @param definition [MetricDefinition or Hash] gauge/counter/availability options.
51
49
  # @example Create gauge metric definition using Hash
52
50
  # client = Hawkular::Metrics::client::new
53
- # client.gauges.create({:id => "id", :dataRetention => 90, :tags => {:tag1 => "value1"}, :tenantId => "your tenant id"})
51
+ # client.gauges.create({:id => "id", :dataRetention => 90,
52
+ # :tags => {:tag1 => "value1"}, :tenantId => "your tenant id"})
54
53
  def create(definition)
55
- if definition.kind_of?(Hawkular::Metrics::MetricDefinition)
54
+ if definition.is_a?(Hawkular::Metrics::MetricDefinition)
56
55
  definition = definition.hash
57
56
  end
58
- @client.http_post('/'+@resource, definition)
57
+ @client.http_post('/' + @resource, definition)
59
58
  end
60
59
 
61
60
  # Query metric definitions by tags
62
61
  # @param tags [Hash]
63
62
  # @return [Array[MetricDefinition]]
64
63
  def query(tags)
65
- tags = tags.map do |k,v|
66
- "#{k}:#{v}"
67
- end
68
- @client.http_get("/metrics/?type=#{@type}&tags=#{tags.join(',')}").map do |g|
69
- Hawkular::Metrics::MetricDefinition::new(g)
70
- end
64
+ @client.http_get("/metrics/?type=#{@type}&tags=#{tags_param(tags)}").map do |g|
65
+ Hawkular::Metrics::MetricDefinition.new(g)
66
+ end
71
67
  end
72
68
 
73
69
  # Get metric definition by id
74
70
  # @param id [String]
75
71
  # @return [MetricDefinition]
76
72
  def get(id)
77
- Hawkular::Metrics::MetricDefinition::new(@client.http_get("/#{@resource}/#{id}"))
73
+ Hawkular::Metrics::MetricDefinition.new(@client.http_get("/#{@resource}/#{id}"))
78
74
  end
79
75
 
80
76
  # update tags for given metric definition
81
77
  # @param metricDefinition [MetricDefinition]
82
78
  def update_tags(metricDefinition)
83
- @client.http_put("/#{@resource}/#{metricDefinition.id}/tags",metricDefinition.hash[:tags])
79
+ @client.http_put("/#{@resource}/#{metricDefinition.id}/tags", metricDefinition.hash[:tags])
84
80
  end
85
81
 
86
82
  # Push metric data
@@ -89,15 +85,15 @@ module Hawkular::Metrics
89
85
  # @example Push counter data with timestamp
90
86
  # client = Hawkular::Metics::Client::new
91
87
  # now = Integer(Time::now.to_f * 1000)
92
- # client.counters.push_data("counter id", [{:value => 1, :timestamp => now - 1000}, {:value => 2, :timestamp => now}])
88
+ # client.counters.push_data("counter id", [{:value => 1, :timestamp => now - 1000},
89
+ # {:value => 2, :timestamp => now}])
93
90
  # @example Push single availability without timestamp
94
91
  # client.avail.push_data("avail_id", {:value => "up"})
95
92
  # @example Push gague data with tags
96
- # client.gagues.push_data("gauge_id", [{:value => 0.1, :tags => {:tagName => "myMin"}},{:value => 99.9, :tags => {:tagName => "myMax"}}])
93
+ # client.gagues.push_data("gauge_id", [{:value => 0.1, :tags => {:tagName => "myMin"}},
94
+ # {:value => 99.9, :tags => {:tagName => "myMax"}}])
97
95
  def push_data(id, data)
98
- if !data.kind_of?(Array)
99
- data = [data]
100
- end
96
+ data = [data] unless data.is_a?(Array)
101
97
 
102
98
  @client.default_timestamp data
103
99
  @client.http_post("/#{@resource}/#{id}/data", data)
@@ -107,20 +103,41 @@ module Hawkular::Metrics
107
103
  # @param id [String] metric definition id
108
104
  # @param starts [Integer] optional timestamp (default now - 8h)
109
105
  # @param ends [Integer] optional timestamp (default now)
106
+ # @param bucketDuration [String] optional interval (default no aggregation)
110
107
  # @return [Array[Hash]] datapoints
111
108
  # @see #push_data #push_data for datapoint detail
112
- def get_data(id, starts: nil, ends: nil)
113
- params = {:start => starts, :end => ends}
114
- params.delete_if { |k, v| v.nil? }
115
- resp = @client.http_get("/#{@resource}/#{id}/data/?"+URI.encode_www_form(params))
116
- return [] if !resp.kind_of?(Array) # API returns no content (empty Hash) instead of empty array
117
- resp
109
+ def get_data(id, starts: nil, ends: nil, bucketDuration: nil)
110
+ params = { start: starts, end: ends, bucketDuration: bucketDuration }
111
+ resp = @client.http_get("/#{@resource}/#{ERB::Util.url_encode(id)}/data/?" +
112
+ encode_params(params))
113
+ resp.is_a?(Array) ? resp : [] # API returns no content (empty Hash) instead of empty array
114
+ end
115
+
116
+ # Retrieve metric datapoints by tags
117
+ # @param tags [Hash]
118
+ # @param starts [Integer] optional timestamp (default now - 8h)
119
+ # @param ends [Integer] optional timestamp (default now)
120
+ # @param bucketDuration [String] optional interval (default no aggregation)
121
+ # @return [Array[Hash]] datapoints
122
+ # @see #push_data #push_data for datapoint detail
123
+ def get_data_by_tags(tags, starts: nil, ends: nil, bucketDuration: nil)
124
+ params = { tags: tags_param(tags), start: starts,
125
+ end: ends, bucketDuration: bucketDuration }
126
+ resp = @client.http_get("/#{@resource}/data/?" + encode_params(params))
127
+ resp.is_a?(Array) ? resp : [] # API returns no content (empty Hash) instead of empty array
128
+ end
129
+
130
+ def tags_param(tags)
131
+ tags.map { |k, v| "#{k}:#{v}" }.join(',')
132
+ end
133
+
134
+ def encode_params(params)
135
+ URI.encode_www_form(params.select { |_k, v| !v.nil? })
118
136
  end
119
137
  end
120
138
 
121
139
  # Class that interracts with "gauge" metric types
122
140
  class Gauges < Metrics
123
-
124
141
  # @param client [Client]
125
142
  def initialize(client)
126
143
  super(client, 'gauge', 'gauges')
@@ -131,46 +148,46 @@ module Hawkular::Metrics
131
148
  # @param starts [Integer] timestamp (default now - 8h)
132
149
  # @param ends [Integer] timestamp (default now)
133
150
  # @param threshold [Numeric]
134
- # @param operation [String] A comparison operation to perform between values and the threshold. Supported operations include "ge", "gte", "lt", "lte", and "eq"
151
+ # @param operation [String] A comparison operation to perform between values and the
152
+ # threshold. Supported operations include "ge", "gte", "lt", "lte", and "eq"
135
153
  # @example Get time periods when metric "gauge1" was under 10 in past 4 hours
136
154
  # before4h = client.now - (4 * 60 * 60 * 1000)
137
155
  # client.gauges.get_periods("gauge1", starts: before4h, threshold: 10, operation: "lte")
138
- def get_periods(id, starts: nil, ends: nil, threshold:, operation:)
139
- params = {:start => starts, :end => ends, :threshold => threshold, :op => operation}
140
- params.delete_if { |k, v| v.nil? }
141
- @client.http_get("/#{@resource}/#{id}/periods?"+URI.encode_www_form(params))
156
+ def get_periods(id, starts: nil, ends: nil, threshold: nil, operation: nil)
157
+ params = { start: starts, end: ends, threshold: threshold, op: operation }
158
+ @client.http_get("/#{@resource}/#{id}/periods?" + encode_params(params))
142
159
  end
143
-
144
160
  end
145
161
 
146
162
  # Class that interracts with "counter" metric types
147
163
  class Counters < Metrics
148
-
149
164
  # @param client [Client]
150
165
  def initialize(client)
151
166
  super(client, 'counter', 'counters')
152
167
  end
153
168
 
154
- # get rate for given metric
155
- # @param id [String] metric ID
156
- def get_rate(id:)
157
- @client.http_get("/#{@resource}/#{id}/rate")
169
+ # Retrieve metric rate points
170
+ # @param id [String] metric definition id
171
+ # @param starts [Integer] optional timestamp (default now - 8h)
172
+ # @param ends [Integer] optional timestamp (default now)
173
+ # @param bucketDuration [String] optional interval (default no
174
+ # aggregation)
175
+ # @return [Array[Hash]] rate points
176
+ def get_rate(id, starts: nil, ends: nil, bucketDuration: nil)
177
+ path = "/#{@resource}/#{ERB::Util.url_encode(id)}/rate"
178
+ params = { start: starts, end: ends, bucketDuration: bucketDuration }
179
+ resp = @client.http_get(path + '?' + encode_params(params))
180
+ # API returns no content (empty Hash) instead of empty array
181
+ resp.is_a?(Array) ? resp : []
158
182
  end
159
183
  end
160
184
 
161
185
  # Class that interracts with "availability" metric types
162
186
  class Availability < Metrics
163
-
164
187
  # @param client [Client]
165
188
  def initialize(client)
166
189
  super(client, 'availability', 'availability')
167
190
  end
168
-
169
191
  end
170
-
171
-
172
- private
173
-
174
-
175
192
  end
176
193
  end