class Hawkular::Metrics::Client::Metrics
Base class for accessing metric definition and data of all types (counters, gauges, availabilities).
Public Class Methods
@param client [Client] @param metric_type [String] metric type (one of “counter”, “gauge”, “availability”) @param resource [String] REST resource name for accessing metrics
of given type (one of "counters", "gauges", "availability")
# File lib/hawkular/metrics/metric_api.rb 113 def initialize(client, metric_type, resource) 114 @client = client 115 @type = metric_type 116 @resource = resource 117 @legacy_api = client.legacy_api 118 end
Public Instance Methods
Create new metric definition @param definition [MetricDefinition or Hash] gauge/counter/availability options. @example Create gauge metric definition using Hash
client = Hawkular::Metrics::client::new client.gauges.create({:id => "id", :dataRetention => 90, :tags => {:tag1 => "value1"}, :tenantId => "your tenant id"})
# File lib/hawkular/metrics/metric_api.rb 126 def create(definition) 127 if definition.is_a?(Hawkular::Metrics::MetricDefinition) 128 definition = definition.hash 129 end 130 @client.http_post('/' + @resource, definition) 131 end
# File lib/hawkular/metrics/metric_api.rb 230 def encode_params(params) 231 URI.encode_www_form(params.reject { |_k, v| v.nil? }) 232 end
Get metric definition by id @param id [String] @return [MetricDefinition]
# File lib/hawkular/metrics/metric_api.rb 149 def get(id) 150 the_id = ERB::Util.url_encode id 151 Hawkular::Metrics::MetricDefinition.new(@client.http_get("/#{@resource}/#{the_id}")) 152 end
Retrieve metric datapoints @param id [String] metric definition id @param starts [Integer] optional timestamp (default now - 8h) @param ends [Integer] optional timestamp (default now) @param buckets [Integer] optional desired number of buckets over the specified timerange @param bucketDuration [String] optional interval (default no aggregation) @param percentiles [String] optional percentiles to calculate @param limit [Integer] optional limit the number of data points returned @param order [String] optional Data point sort order, based on timestamp (ASC, DESC) @return [Array] datapoints @see push_data
push_data
for datapoint detail
# File lib/hawkular/metrics/metric_api.rb 193 def get_data(id, starts: nil, ends: nil, bucketDuration: nil, # rubocop:disable Naming/VariableName 194 buckets: nil, percentiles: nil, limit: nil, order: nil) 195 params = { start: starts, end: ends, bucketDuration: bucketDuration, buckets: buckets, 196 percentiles: percentiles, limit: limit, order: order } 197 get_data_helper(id, params) 198 end
Push metric data @param id [String] metric definition ID @param data [Array] Single datapoint or array of datapoints @example Push counter data with timestamp
client = Hawkular::Metics::Client::new now = Integer(Time::now.to_f * 1000) client.counters.push_data("counter id", [{:value => 1, :timestamp => now - 1000}, {:value => 2, :timestamp => now}])
@example Push single availability without timestamp
client.avail.push_data("avail_id", {:value => "up"})
@example Push gague data with tags
client.gagues.push_data("gauge_id", [{:value => 0.1, :tags => {:tagName => "myMin"}}, {:value => 99.9, :tags => {:tagName => "myMax"}}])
# File lib/hawkular/metrics/metric_api.rb 174 def push_data(id, data) 175 data = [data] unless data.is_a?(Array) 176 uri = "/#{@resource}/#{ERB::Util.url_encode(id)}/" 177 uri << (@legacy_api ? 'data' : 'raw') 178 @client.default_timestamp data 179 @client.http_post(uri, data) 180 end
Query metric definitions by tags @param tags [Hash] @param options [Hash] Additional options to configure @option options [Boolean] :timestamps If timestamps should be included on the response. Defaults to true @return [Array]
# File lib/hawkular/metrics/metric_api.rb 138 def query(tags = nil, options = {}) 139 timestamps = (options.key?(:timestamps) ? options[:timestamps] : true).to_s 140 tags_filter = tags.nil? ? '' : "&tags=#{@client.tags_param(tags)}" 141 @client.http_get("/metrics/?timestamps=#{timestamps}&type=#{@type}#{tags_filter}").map do |g| 142 Hawkular::Metrics::MetricDefinition.new(g) 143 end 144 end
Retrieve raw data for multiple metrics. @param ids [Array] metric definition ids @param starts [Integer] optional timestamp (default now - 8h) @param ends [Integer] optional timestamp (default now) @param limit [Integer] optional limit the number of data points returned @param order [String] optional Data point sort order, based on timestamp (ASC, DESC) @return [Array] named datapoints
# File lib/hawkular/metrics/metric_api.rb 207 def raw_data(ids, starts: nil, ends: nil, limit: nil, order: nil) 208 params = { ids: ids, start: starts, end: ends, limit: limit, order: order } 209 @client.http_post("/#{@resource}/raw/query", params) 210 end
Private Instance Methods
# File lib/hawkular/metrics/metric_api.rb 236 def get_data_helper(id, params) 237 path = "/#{@resource}/#{ERB::Util.url_encode(id)}/" 238 path << if @legacy_api 239 'data/?' 240 elsif params[:bucketDuration].nil? && params[:buckets].nil? 241 'raw/?' 242 else 243 'stats/?' 244 end 245 246 path << encode_params(params) 247 resp = @client.http_get(path) 248 resp.is_a?(Array) ? resp : [] # API returns no content (empty Hash) instead of empty array 249 end