Class: Hawkular::Metrics::Client::Metrics

Inherits:
Object
  • Object
show all
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

Availability, Counters, Gauges, Strings

Instance Method Summary collapse

Constructor Details

#initialize(client, metric_type, resource) ⇒ Metrics

Returns a new instance of Metrics

Parameters:

  • client (Client)
  • metric_type (String)

    metric type (one of “counter”, “gauge”, “availability”)

  • resource (String)

    REST resource name for accessing metrics of given type (one of “counters”, “gauges”, “availability”)



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

Examples:

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"})

Parameters:



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

Parameters:

  • id (String)

Returns:



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

Parameters:

  • id (String)

    metric definition id

  • starts (Integer)

    optional timestamp (default now - 8h)

  • ends (Integer)

    optional timestamp (default now)

  • buckets (Integer)

    optional desired number of buckets over the specified timerange

  • bucketDuration (String)

    optional interval (default no aggregation)

  • percentiles (String)

    optional percentiles to calculate

  • limit (Integer)

    optional limit the number of data points returned

  • order (String)

    optional Data point sort order, based on timestamp (ASC, DESC)

Returns:

  • (Array[Hash])

    datapoints

See Also:



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

Parameters:

  • tags (Hash)
  • starts (Integer)

    optional timestamp (default now - 8h)

  • ends (Integer)

    optional timestamp (default now)

  • bucketDuration (String)

    optional interval (default no aggregation)

  • buckets (Integer)

    optional number of buckets

Returns:

  • (Array[Hash])

    datapoints

See Also:



217
218
219
220
221
222
223
224
# File 'lib/hawkular/metrics/metric_api.rb', line 217

def get_data_by_tags(tags, starts: nil, ends: nil, bucketDuration: nil, buckets:nil)
  params = { tags: @client.tags_param(tags), 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

Examples:

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}])

Push single availability without timestamp

client.avail.push_data("avail_id", {:value => "up"})

Push gague data with tags

client.gagues.push_data("gauge_id", [{:value => 0.1, :tags => {:tagName => "myMin"}},
                                     {:value => 99.9, :tags => {:tagName => "myMax"}}])

Parameters:

  • id (String)

    metric definition ID

  • data (Array[Hash])

    Single datapoint or array of datapoints



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.default_timestamp data
  @client.http_post(uri, data)
end

#query(tags = nil, options = {}) ⇒ Array[MetricDefinition]

Query metric definitions by tags

Parameters:

  • tags (Hash) (defaults to: nil)
  • options (Hash) (defaults to: {})

    Additional options to configure

Options Hash (options):

  • :timestamps (Boolean)

    If timestamps should be included on the response. Defaults to true

Returns:



135
136
137
138
139
140
141
# File 'lib/hawkular/metrics/metric_api.rb', line 135

def query(tags = nil, options = {})
  timestamps = (options.key?(:timestamps) ? options[:timestamps] : true).to_s
  tags_filter = tags.nil? ? '' : "&tags=#{@client.tags_param(tags)}"
  @client.http_get("/metrics/?timestamps=#{timestamps}&type=#{@type}#{tags_filter}").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.

Parameters:

  • ids (Array[String])

    metric definition ids

  • starts (Integer)

    optional timestamp (default now - 8h)

  • ends (Integer)

    optional timestamp (default now)

  • limit (Integer)

    optional limit the number of data points returned

  • order (String)

    optional Data point sort order, based on timestamp (ASC, DESC)

Returns:

  • (Array[Hash])

    named datapoints



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

Parameters:



153
154
155
156
# File 'lib/hawkular/metrics/metric_api.rb', line 153

def update_tags(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