Class: Hawkular::Metrics::Client::Metrics
- Inherits:
-
Object
- Object
- Hawkular::Metrics::Client::Metrics
- Defined in:
- lib/hawkular/metrics/metric_api.rb
Overview
Base class for accessing metric definition and data of all types (counters, gauges, availabilities).
Direct Known Subclasses
Instance Method Summary collapse
-
#create(definition) ⇒ Object
Create new metric definition.
- #encode_params(params) ⇒ Object
-
#get(id) ⇒ MetricDefinition
Get metric definition by id.
-
#get_data(id, starts: nil, ends: nil, bucketDuration: nil, buckets: nil, percentiles: nil, limit: nil, order: nil) ⇒ Array[Hash]
Retrieve metric datapoints.
-
#get_data_by_tags(tags, starts: nil, ends: nil, bucketDuration: nil, buckets: nil) ⇒ Array[Hash]
Retrieve metric datapoints by tags.
-
#initialize(client, metric_type, resource) ⇒ Metrics
constructor
A new instance of Metrics.
-
#push_data(id, data) ⇒ Object
Push metric data.
-
#query(tags = nil, options = {}) ⇒ Array[MetricDefinition]
Query metric definitions by tags.
-
#raw_data(ids, starts: nil, ends: nil, limit: nil, order: nil) ⇒ Array[Hash]
Retrieve raw data for multiple metrics.
-
#update_tags(metric_definition) ⇒ Object
update tags for given metric definition.
Constructor Details
#initialize(client, metric_type, resource) ⇒ Metrics
Returns a new instance of Metrics
110 111 112 113 114 115 |
# File 'lib/hawkular/metrics/metric_api.rb', line 110 def initialize(client, metric_type, resource) @client = client @type = metric_type @resource = resource @legacy_api = client.legacy_api end |
Instance Method Details
#create(definition) ⇒ Object
Create new metric definition
123 124 125 126 127 128 |
# File 'lib/hawkular/metrics/metric_api.rb', line 123 def create(definition) if definition.is_a?(Hawkular::Metrics::MetricDefinition) definition = definition.hash end @client.http_post('/' + @resource, definition) end |
#encode_params(params) ⇒ Object
226 227 228 |
# File 'lib/hawkular/metrics/metric_api.rb', line 226 def encode_params(params) URI.encode_www_form(params.select { |_k, v| !v.nil? }) end |
#get(id) ⇒ MetricDefinition
Get metric definition by id
146 147 148 149 |
# File 'lib/hawkular/metrics/metric_api.rb', line 146 def get(id) the_id = ERB::Util.url_encode id Hawkular::Metrics::MetricDefinition.new(@client.http_get("/#{@resource}/#{the_id}")) end |
#get_data(id, starts: nil, ends: nil, bucketDuration: nil, buckets: nil, percentiles: nil, limit: nil, order: nil) ⇒ Array[Hash]
Retrieve metric datapoints
190 191 192 193 194 195 |
# File 'lib/hawkular/metrics/metric_api.rb', line 190 def get_data(id, starts: nil, ends: nil, bucketDuration: nil, buckets: nil, percentiles: nil, limit: nil, order: nil) params = { start: starts, end: ends, bucketDuration: bucketDuration, buckets: buckets, percentiles: percentiles, limit: limit, order: order } get_data_helper(id, params) end |
#get_data_by_tags(tags, starts: nil, ends: nil, bucketDuration: nil, buckets: nil) ⇒ Array[Hash]
Retrieve metric datapoints by tags
217 218 219 220 221 222 223 224 |
# File 'lib/hawkular/metrics/metric_api.rb', line 217 def (, starts: nil, ends: nil, bucketDuration: nil, buckets:nil) params = { tags: @client.(), start: starts, end: ends, bucketDuration: bucketDuration, buckets: buckets } path = "/#{@resource}/" @legacy_api ? path << 'data/?' : path << 'stats/?' resp = @client.http_get(path + encode_params(params)) resp.is_a?(Array) ? resp : [] # API returns no content (empty Hash) instead of empty array end |
#push_data(id, data) ⇒ Object
Push metric data
171 172 173 174 175 176 177 |
# File 'lib/hawkular/metrics/metric_api.rb', line 171 def push_data(id, data) data = [data] unless data.is_a?(Array) uri = "/#{@resource}/#{ERB::Util.url_encode(id)}/" @legacy_api ? uri << 'data' : uri << 'raw' @client. data @client.http_post(uri, data) end |
#query(tags = nil, options = {}) ⇒ Array[MetricDefinition]
Query metric definitions by tags
135 136 137 138 139 140 141 |
# File 'lib/hawkular/metrics/metric_api.rb', line 135 def query( = nil, = {}) = (.key?(:timestamps) ? [:timestamps] : true).to_s = .nil? ? '' : "&tags=#{@client.()}" @client.http_get("/metrics/?timestamps=#{}&type=#{@type}#{}").map do |g| Hawkular::Metrics::MetricDefinition.new(g) end end |
#raw_data(ids, starts: nil, ends: nil, limit: nil, order: nil) ⇒ Array[Hash]
Retrieve raw data for multiple metrics.
204 205 206 207 |
# File 'lib/hawkular/metrics/metric_api.rb', line 204 def raw_data(ids, starts: nil, ends: nil, limit: nil, order: nil) params = { ids: ids, start: starts, end: ends, limit: limit, order: order } @client.http_post("/#{@resource}/raw/query", params) end |
#update_tags(metric_definition) ⇒ Object
update tags for given metric definition
153 154 155 156 |
# File 'lib/hawkular/metrics/metric_api.rb', line 153 def (metric_definition) metric_definition_id = ERB::Util.url_encode metric_definition.id @client.http_put("/#{@resource}/#{metric_definition_id}/tags", metric_definition.hash[:tags]) end |