Class: Hawkular::Metrics::Client

Inherits:
BaseClient show all
Defined in:
lib/hawkular/metrics/metric_api.rb,
lib/hawkular/metrics/tenant_api.rb,
lib/hawkular/metrics/metrics_client.rb

Overview

Client to access the Hawkular_metrics subsystem

Defined Under Namespace

Classes: Availability, Counters, Gauges, Metrics, Strings, Tenants

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from BaseClient

#admin_header, #base_64_credentials, #generate_query_params, #http_delete, #http_get, #http_post, #http_put, #normalize_entrypoint_url, #now, #url_with_websocket_scheme

Methods included from ClientUtils

#hawk_escape, #hawk_escape_id

Constructor Details

#initialize(entrypoint, credentials = {}, options = {}) ⇒ Client

Construct a new Hawkular Metrics client class. optional parameters

Examples:

initialize with Hawkular-tenant option

Hawkular::Metrics::Client::new("http://server",
  {username:"username",password:"password"},
                       {"tenant" => "your tenant ID"})

Parameters:

  • entrypoint (String)

    Base url of the Hawkular (metrics) server

  • credentials (defaults to: {})

    Hash of username, password, token(optional)

  • options (Hash{String=>String}) (defaults to: {})

    client options



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/hawkular/metrics/metrics_client.rb', line 56

def initialize(entrypoint,
               credentials = {},
               options = {})
  entrypoint = normalize_entrypoint_url entrypoint, 'hawkular/metrics'
  super(entrypoint, credentials, options)
  check_version
  @tenants = Client::Tenants.new self
  @counters = Client::Counters.new self
  @gauges = Client::Gauges.new self
  @avail = Client::Availability.new self
  @strings = Client::Strings.new self
end

Instance Attribute Details

#availAvailability (readonly)

Returns access counters API

Returns:



31
32
33
# File 'lib/hawkular/metrics/metrics_client.rb', line 31

def avail
  @avail
end

#countersCounters (readonly)

Returns access counters API

Returns:



25
26
27
# File 'lib/hawkular/metrics/metrics_client.rb', line 25

def counters
  @counters
end

#gaugesGauges (readonly)

Returns access gauges API

Returns:

  • (Gauges)

    access gauges API



27
28
29
# File 'lib/hawkular/metrics/metrics_client.rb', line 27

def gauges
  @gauges
end

#legacy_apiboolean (readonly)

Returns if it's using the legacy API or not

Returns:

  • (boolean)

    if it's using the legacy API or not



34
35
36
# File 'lib/hawkular/metrics/metrics_client.rb', line 34

def legacy_api
  @legacy_api
end

#stringsStrings (readonly)

Returns access strings API

Returns:



29
30
31
# File 'lib/hawkular/metrics/metrics_client.rb', line 29

def strings
  @strings
end

#tenantsTenants (readonly)

Returns access tenants API

Returns:



23
24
25
# File 'lib/hawkular/metrics/metrics_client.rb', line 23

def tenants
  @tenants
end

Instance Method Details

#check_versionObject



36
37
38
39
40
41
42
43
44
# File 'lib/hawkular/metrics/metrics_client.rb', line 36

def check_version
  version_status_hash = fetch_version_and_status
  fail_version_msg = 'Unable to determine implementation version for metrics'
  fail fail_version_msg if version_status_hash['Implementation-Version'].nil?
  version = version_status_hash['Implementation-Version']
  major, minor = version.scan(/\d+/).map(&:to_i)
  fail fail_version_msg if major.nil? || minor.nil?
  @legacy_api = (major == 0 && minor < 16)
end

#data_by_tags(tags, buckets: nil, bucketDuration: nil, start: nil, ends: nil) ⇒ Array[Hash]

Retrieve all types of metrics datapoints by tags

Parameters:

  • tags (Hash)
  • buckets (Integer)

    optional number of buckets

  • bucketDuration (String)

    optional interval (default no aggregation)

  • starts (Integer)

    optional timestamp (default now - 8h)

  • ends (Integer)

    optional timestamp (default now)

Returns:

  • (Array[Hash])

    datapoints



26
27
28
29
30
31
32
# File 'lib/hawkular/metrics/metric_api.rb', line 26

def data_by_tags(tags, buckets: nil, bucketDuration:nil, start:nil, ends: nil)
  data = {
    tags: tags_param(tags), buckets: buckets, bucketDuration: bucketDuration, start: start, end: ends
  }

  http_post('metrics/stats/query', data)
end

#fetch_version_and_statusHash{String=>String}

Return version and status information for the used version of Hawkular-Metrics

Returns:

  • (Hash{String=>String})

    ('Implementation-Version', 'Built-From-Git-SHA1', 'Status')



37
38
39
# File 'lib/hawkular/metrics/metric_api.rb', line 37

def fetch_version_and_status
  http_get('/status')
end

#push_data(gauges: [], counters: [], availabilities: [], strings: []) ⇒ Object

Push data for multiple metrics of all supported types

Examples:

push datapoints of 2 counter metrics

client = Hawkular::Metrics::client::new
client.push_data(counters: [{:id => "counter1", :data => [{:value => 1}, {:value => 2}]},
                    {:id => "counter2", :data => [{:value => 1}, {:value => 3}]}])

push gauge and availability datapoints

client.push_data(gauges: [{:id => "gauge1", :data => [{:value => 1}, {:value => 2}]}],
                       availabilities: [{:id => "avail1", :data => [{:value => "up"}]}])

Parameters:

  • gauges (Array)
  • counters (Array)
  • availabilities (Array)


52
53
54
55
56
57
58
59
60
61
# File 'lib/hawkular/metrics/metric_api.rb', line 52

def push_data(gauges: [], counters: [], availabilities: [], strings: [])
  gauges.each { |g| default_timestamp g[:data] }
  counters.each { |g| default_timestamp g[:data] }
  availabilities.each { |g| default_timestamp g[:data] }
  strings.each { |g| default_timestamp g[:data] }
  data = { gauges: gauges, counters: counters, availabilities: availabilities, strings: strings }
  path = '/metrics/'
  @legacy_api ? path << 'data' : path << 'raw'
  http_post(path, data)
end

#query_stats(gauge_ids: [], counter_ids: [], avail_ids: [], rates: false, starts: nil, ends: nil, bucket_duration: '3600s') ⇒ Hash

Fetch stats for multiple metrics of all supported types

Examples:

client = Hawkular::Metrics::client::new
client.query_stats(
  gauge_ids: ['G1', 'G2'],
  counter_ids: ['C2', 'C3'],
  avail_ids: ['A2', 'A3'],
  starts: 200,
  ends: 500,
  bucket_duration: '150ms'
)

Parameters:

  • gauge_ids (Array[String])

    list of gauge ids

  • counter_ids (Array[String])

    list of counter ids

  • avail_ids (Array[String])

    list of availability ids

  • rates (Boolean)

    flag to include rates for gauges and counters metrics

  • starts (Integer)

    optional timestamp (default now - 8h)

  • ends (Integer)

    optional timestamp (default now)

  • bucket_duration (String)

    optional interval (default 3600s)

Returns:

  • (Hash)

    stats grouped per type



82
83
84
85
86
87
88
89
# File 'lib/hawkular/metrics/metric_api.rb', line 82

def query_stats(gauge_ids: [], counter_ids: [], avail_ids: [], rates: false, starts: nil, ends: nil,
                bucket_duration: '3600s')
  path = '/metrics/stats/query'
  metrics = { gauge: gauge_ids, counter: counter_ids, availability: avail_ids }
  data = { metrics: metrics, start: starts, end: ends, bucketDuration: bucket_duration }
  data['types'] = %w(gauge gauge_rate counter counter_rate availability) if rates
  http_post(path, data)
end

#tagsHash{String=>String}

Fetch all tags for metrics definitions

Returns:

  • (Hash{String=>String})


93
94
95
96
97
98
99
100
101
# File 'lib/hawkular/metrics/metric_api.rb', line 93

def tags
  tags = []
  http_get('/metrics/').map do |g|
    g['tags'].map do |k, v|
      tags << { k => v }
    end unless g['tags'].nil?
  end
  tags.uniq!
end

#tags_param(tags) ⇒ Object



15
16
17
# File 'lib/hawkular/metrics/metric_api.rb', line 15

def tags_param(tags)
  tags.map { |k, v| "#{k}:#{v}" }.join(',')
end