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.
- checksums.yaml +4 -4
- data/.gitignore +17 -0
- data/.rubocop.yml +14 -0
- data/.travis.yml +5 -0
- data/CHANGES.rdoc +6 -0
- data/Gemfile +1 -1
- data/README.rdoc +2 -1
- data/Rakefile +6 -3
- data/hawkularclient.gemspec +17 -11
- data/lib/hawkularclient.rb +91 -98
- data/lib/metrics/metric_api.rb +83 -66
- data/lib/metrics/tenant_api.rb +3 -4
- data/lib/metrics/types.rb +9 -11
- data/lib/metrics/version.rb +1 -1
- data/spec/{endpoint.yml.example → endpoint.yml} +0 -0
- data/spec/integration/metric_spec.rb +223 -131
- data/spec/spec_helper.rb +14 -10
- data/spec/unit/client_spec.rb +24 -19
- data/spec/vcr/vcr_setup.rb +16 -0
- data/spec/vcr_cassettes/Availability_metrics/Should_create_Availability_definition_using_MetricDefinition_parameter.yml +87 -0
- data/spec/vcr_cassettes/Availability_metrics/Should_create_and_return_Availability_using_Hash_parameter.yml +87 -0
- data/spec/vcr_cassettes/Availability_metrics/Should_push_metric_data_to_non-existing_Availability.yml +126 -0
- data/spec/vcr_cassettes/Availability_metrics/Should_update_tags_for_Availability_definition.yml +210 -0
- data/spec/vcr_cassettes/Counter_metrics/Should_create_and_return_counter_using_Hash_parameter.yml +87 -0
- data/spec/vcr_cassettes/Counter_metrics/Should_create_counter_definition_using_MetricDefinition_parameter.yml +87 -0
- data/spec/vcr_cassettes/Counter_metrics/Should_push_metric_data_to_existing_counter.yml +249 -0
- data/spec/vcr_cassettes/Counter_metrics/Should_push_metric_data_to_non-existing_counter.yml +126 -0
- data/spec/vcr_cassettes/Gauge_metrics/Should_create_gauge_definition_using_Hash.yml +87 -0
- data/spec/vcr_cassettes/Gauge_metrics/Should_create_gauge_definition_using_MetricDefinition.yml +87 -0
- data/spec/vcr_cassettes/Gauge_metrics/Should_push_metric_data_to_existing_gauge.yml +249 -0
- data/spec/vcr_cassettes/Gauge_metrics/Should_push_metric_data_to_non-existing_gauge.yml +126 -0
- data/spec/vcr_cassettes/Gauge_metrics/Should_return_periods.yml +85 -0
- data/spec/vcr_cassettes/Gauge_metrics/Should_update_tags_for_gauge_definition.yml +210 -0
- data/spec/vcr_cassettes/Mixed_metrics/Should_send_mixed_metric_request.yml +284 -0
- data/spec/vcr_cassettes/Mixed_metrics/Should_send_mixed_metric_request_of_a_single_type.yml +249 -0
- data/spec/vcr_cassettes/Simple/Should_be_Cool.yml +208 -0
- data/spec/vcr_cassettes/Tenants/Should_create_and_return_tenant.yml +79 -0
- metadata +111 -25
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6b21f0f14d7546fe626efb1cb3209c1085d22124
|
4
|
+
data.tar.gz: 9b92719c7441a05ce9cfa81a0d0f9b0cfae55553
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
data/.travis.yml
ADDED
data/CHANGES.rdoc
CHANGED
data/Gemfile
CHANGED
@@ -1,2 +1,2 @@
|
|
1
|
-
source
|
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
|
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
|
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 =
|
6
|
+
task.pattern = './spec/**/*_spec.rb'
|
6
7
|
task.rspec_opts = ['--color']
|
7
8
|
end
|
8
9
|
|
9
|
-
|
10
|
+
RuboCop::RakeTask.new
|
11
|
+
|
12
|
+
task default: [:rubocop, :spec]
|
data/hawkularclient.gemspec
CHANGED
@@ -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 =
|
7
|
+
gem.name = 'hawkular-client'
|
8
8
|
gem.version = Hawkular::Metrics::VERSION
|
9
|
-
gem.authors = ['Libor Zoubek']
|
10
|
-
gem.email =
|
11
|
-
gem.homepage =
|
12
|
-
gem.summary =
|
13
|
-
gem.
|
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 = [
|
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('
|
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 <<
|
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
|
data/lib/hawkularclient.rb
CHANGED
@@ -1,40 +1,37 @@
|
|
1
1
|
|
2
2
|
|
3
|
-
require
|
4
|
-
require
|
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",
|
12
|
-
#
|
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
|
19
|
-
require
|
20
|
-
require
|
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
|
-
|
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
|
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",
|
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',
|
56
|
+
def initialize(entrypoint = 'http://localhost:8080/hawkular/metrics',
|
57
|
+
credentials = {},
|
58
|
+
options = {})
|
59
59
|
@entrypoint = entrypoint
|
60
|
-
@credentials = {
|
60
|
+
@credentials = {
|
61
|
+
username: nil,
|
62
|
+
password: nil,
|
63
|
+
token: nil
|
64
|
+
}.merge(credentials)
|
61
65
|
@options = {
|
62
|
-
:
|
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
|
66
|
-
@counters = Client::Counters
|
67
|
-
@gauges = Client::Gauges
|
68
|
-
@avail = Client::Availability
|
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
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
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
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
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
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
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
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
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(
|
141
|
-
.merge(
|
142
|
-
.merge(
|
143
|
-
|
144
|
-
|
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
|
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
|
-
|
159
|
-
|
160
|
-
|
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
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
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
|
data/lib/metrics/metric_api.rb
CHANGED
@@ -1,15 +1,15 @@
|
|
1
|
+
require 'erb'
|
2
|
+
|
1
3
|
module Hawkular::Metrics
|
2
4
|
class Client
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
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}]},
|
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}]}],
|
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
|
-
|
26
|
-
}
|
27
|
-
|
28
|
-
|
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
|
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
|
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,
|
51
|
+
# client.gauges.create({:id => "id", :dataRetention => 90,
|
52
|
+
# :tags => {:tag1 => "value1"}, :tenantId => "your tenant id"})
|
54
53
|
def create(definition)
|
55
|
-
if definition.
|
54
|
+
if definition.is_a?(Hawkular::Metrics::MetricDefinition)
|
56
55
|
definition = definition.hash
|
57
56
|
end
|
58
|
-
@client.http_post('/'
|
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
|
-
|
66
|
-
|
67
|
-
|
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
|
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},
|
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"}},
|
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
|
-
|
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 = {:
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
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
|
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
|
139
|
-
|
140
|
-
|
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
|
-
#
|
155
|
-
# @param id [String] metric
|
156
|
-
|
157
|
-
|
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
|