lib/hawkular/metrics/metric_api.rb in hawkular-client-2.2.1 vs lib/hawkular/metrics/metric_api.rb in hawkular-client-2.3.0

- old
+ new

@@ -32,13 +32,14 @@ # availabilities: [{:id => "avail1", :data => [{:value => "up"}]}]) def push_data(gauges: [], counters: [], availabilities: []) gauges.each { |g| default_timestamp g[:data] } counters.each { |g| default_timestamp g[:data] } availabilities.each { |g| default_timestamp g[:data] } - data = { gauges: gauges, counters: counters, availabilities: availabilities } - http_post('/metrics/data', data) + path = '/metrics/' + @legacy_api ? path << 'data' : path << 'raw' + http_post(path, data) end # Base class for accessing metric definition and data of all # types (counters, gauges, availabilities). class Metrics @@ -48,10 +49,11 @@ # of given type (one of "counters", "gauges", "availability") def initialize(client, metric_type, resource) @client = client @type = metric_type @resource = resource + @legacy_api = client.legacy_api end # Create new metric definition # @param definition [MetricDefinition or Hash] gauge/counter/availability options. # @example Create gauge metric definition using Hash @@ -77,18 +79,19 @@ # Get metric definition by id # @param id [String] # @return [MetricDefinition] def get(id) - the_id = @client.hawk_escape id + the_id = ERB::Util.url_encode id Hawkular::Metrics::MetricDefinition.new(@client.http_get("/#{@resource}/#{the_id}")) end # update tags for given metric definition # @param metric_definition [MetricDefinition] def update_tags(metric_definition) - @client.http_put("/#{@resource}/#{metric_definition.id}/tags", metric_definition.hash[:tags]) + metric_definition_id = ERB::Util.url_encode metric_definition.id + @client.http_put("/#{@resource}/#{metric_definition_id}/tags", metric_definition.hash[:tags]) end # Push metric data # @param id [String] metric definition ID # @param data [Array[Hash]] Single datapoint or array of datapoints @@ -102,13 +105,14 @@ # @example Push gague data with tags # client.gagues.push_data("gauge_id", [{:value => 0.1, :tags => {:tagName => "myMin"}}, # {:value => 99.9, :tags => {:tagName => "myMax"}}]) 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.default_timestamp data - @client.http_post("/#{@resource}/#{id}/data", data) + @client.http_post(uri, data) end # Retrieve metric datapoints # @param id [String] metric definition id # @param starts [Integer] optional timestamp (default now - 8h) @@ -147,11 +151,13 @@ # @return [Array[Hash]] datapoints # @see #push_data #push_data for datapoint detail def get_data_by_tags(tags, starts: nil, ends: nil, bucketDuration: nil) params = { tags: tags_param(tags), start: starts, end: ends, bucketDuration: bucketDuration } - resp = @client.http_get("/#{@resource}/data/?" + encode_params(params)) + 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 def tags_param(tags) tags.map { |k, v| "#{k}:#{v}" }.join(',') @@ -162,12 +168,18 @@ end private def get_data_helper(id, params) - resp = @client.http_get("/#{@resource}/#{ERB::Util.url_encode(id)}/data/?" + - encode_params(params)) + path = "/#{@resource}/#{ERB::Util.url_encode(id)}/" + if @legacy_api + path << 'data/?' + else + (params[:bucketDuration].nil? && params[:buckets].nil?) ? path << 'raw/?' : path << 'stats/?' + end + path << encode_params(params) + resp = @client.http_get(path) resp.is_a?(Array) ? resp : [] # API returns no content (empty Hash) instead of empty array end end # Class that interacts with "gauge" metric types @@ -187,11 +199,11 @@ # @example Get time periods when metric "gauge1" was under 10 in past 4 hours # before4h = client.now - (4 * 60 * 60 * 1000) # client.gauges.get_periods("gauge1", starts: before4h, threshold: 10, operation: "lte") def get_periods(id, starts: nil, ends: nil, threshold: nil, operation: nil) params = { start: starts, end: ends, threshold: threshold, op: operation } - @client.http_get("/#{@resource}/#{id}/periods?" + encode_params(params)) + @client.http_get("/#{@resource}/#{ERB::Util.url_encode(id)}/periods?" + encode_params(params)) end end # Class that interacts with "counter" metric types class Counters < Metrics @@ -207,9 +219,10 @@ # @param bucket_duration [String] optional interval (default no # aggregation) # @return [Array[Hash]] rate points def get_rate(id, starts: nil, ends: nil, bucket_duration: nil) path = "/#{@resource}/#{ERB::Util.url_encode(id)}/rate" + path << '/stats' unless bucket_duration.nil? && @legacy_api params = { start: starts, end: ends, bucketDuration: bucket_duration } resp = @client.http_get(path + '?' + encode_params(params)) # API returns no content (empty Hash) instead of empty array resp.is_a?(Array) ? resp : [] end