Skip to content

Commit

Permalink
Merge pull request #254 from lucasponce/fix_specs
Browse files Browse the repository at this point in the history
Addition on Prometheus API and fix specs
  • Loading branch information
israel-hdez authored Dec 5, 2017
2 parents 45d006c + 9bbdf54 commit f149df1
Show file tree
Hide file tree
Showing 5 changed files with 435 additions and 62 deletions.
6 changes: 6 additions & 0 deletions lib/hawkular/prometheus/prometheus_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ def prometheus_entrypoint
# Interface to talk with the Prometheus server used for Middleware Manager
# @param entrypoint [String] base url of Hawkular Services
class Client < Hawkular::BaseClient
attr_reader :entrypoint

def initialize(entrypoint, credentials = {}, options = {})
prometheus_entrypoint = Alerter.new(entrypoint, credentials, options).prometheus_entrypoint
@entrypoint = normalize_entrypoint_url prometheus_entrypoint, 'api/v1'
Expand Down Expand Up @@ -56,5 +58,9 @@ def up_time(feed_id: nil, starts: nil, ends: nil, step: nil)
response['data']['result'].first['values']
end
end

def ping
http_get '/query?query=up'
end
end
end
84 changes: 49 additions & 35 deletions spec/integration/prometheus_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@

module Hawkular::Prometheus::RSpec
HAWKULAR_BASE = 'http://localhost:8080/'
creds = {}
creds = {
username: 'jdoe',
password: 'password'
}
options = {}

describe 'Prometheus/Queries' do
Expand All @@ -12,60 +15,71 @@ module Hawkular::Prometheus::RSpec
description
end

around(:each) do |example|
record('Prometheus/Queries', credentials, cassette_name, example: example)
let(:inventory_client) do
Hawkular::Inventory::Client.new(HAWKULAR_BASE, creds, options)
end

let(:prometheus_client) do
Hawkular::Prometheus::Client.new(HAWKULAR_BASE, creds, options)
end

before(:each) do
@client = Hawkular::Prometheus::Client.new(HAWKULAR_BASE, creds, options)
around(:each) do |example|
record('Prometheus/Queries', credentials, cassette_name, example: example)
end

it 'Should fetch a metrics range' do
metrics = [{ 'displayName' => 'Heap Used',
'family' => 'jvm_memory_bytes_used',
'unit' => 'BYTES',
'expression' => 'jvm_memory_bytes_used{area="heap",feed_id="attilan"}',
'labels' => { area: 'heap', feed_id: 'attilan' } },
{ 'displayName' => 'NonHeap Used',
'family' => 'jvm_memory_bytes_used',
'unit' => 'BYTES',
'expression' => 'jvm_memory_bytes_used{area="nonheap",feed_id="attilan"}',
'labels' => { area: 'nonheap', feed_id: 'attilan' } }]

results = @client.query_range(metrics: metrics,
starts: '2017-11-28T10:00:00Z',
ends: '2017-11-28T11:00:00Z',
step: '5s')
metrics = inventory_client
.resources_for_type('WildFly Server WF10')
.first
.metrics
.select { |metric| ['Heap Used', 'NonHeap Used'].include?(metric.name) }
.map(&:to_h)

now = '2017-11-05T11:25:00Z'
before = '2017-11-05T11:20:00Z'

results = prometheus_client.query_range(metrics: metrics,
starts: before,
ends: now,
step: '5s')

first = results.first
second = results.last
expect(first['metric']['displayName']).to eq 'Heap Used'
expect(first['values'].size).to be > 0
expect(second['metric']['displayName']).to eq 'NonHeap Used'
expect(second['values'].size).to be > 0
end

it 'Should fetch up time' do
results = @client.up_time(feed_id: 'attilan',
starts: '2017-11-28T10:00:00Z',
ends: '2017-11-28T15:00:00Z',
step: '60m')
expect(results.size).to be > 0
feed_id = inventory_client
.resources_for_type('WildFly Server WF10')
.first
.feed

now = '2017-11-05T11:25:00Z'
before = '2017-11-05T11:20:00Z'

results = prometheus_client.up_time(feed_id: feed_id,
starts: before,
ends: now,
step: '5s')
expect(results).to be_truthy
end

it 'Should fetch a metrics instant' do
metrics = [{ 'displayName' => 'Server Availability',
'family' => 'wildfly_server_availability',
'unit' => 'AVAILABILITY',
'expression' => 'wildfly_server_availability{feed_id="attilan"}',
'labels' => { feed_id: 'attilan' } }]
metrics = inventory_client
.resources_for_type('WildFly Server WF10')
.first
.metrics
.select { |metric| ['Server Availability'].include?(metric.name) }
.map(&:to_h)

now = '2017-11-05T11:25:00Z'

results = @client.query(metrics: metrics,
time: '2017-11-28T10:00:00Z')
results = prometheus_client.query(metrics: metrics,
time: now)

first = results.first
expect(first['metric']['displayName']).to eq 'Server Availability'
expect(first['value'].size).to be > 0
end
end
end

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit f149df1

Please sign in to comment.