-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
483 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
require 'hawkular/base_client' | ||
require 'hawkular/inventory/entities' | ||
require 'ostruct' | ||
|
||
module Hawkular::Prometheus | ||
class Alerter < Hawkular::BaseClient | ||
def initialize(entrypoint, credentials = {}, options = {}) | ||
@entrypoint = normalize_entrypoint_url entrypoint, 'hawkular/alerter' | ||
super(@entrypoint, credentials, options) | ||
end | ||
|
||
def prometheus_entrypoint | ||
rest_client('/prometheus/endpoint').get | ||
end | ||
end | ||
|
||
# Interface to talk with the Prometheus server used for Middleware Manager | ||
# @param entrypoint [String] base url of Hawkular Services | ||
class Client < Hawkular::BaseClient | ||
def initialize(entrypoint, credentials = {}, options = {}) | ||
prometheus_entrypoint = Alerter.new(entrypoint, credentials, options).prometheus_entrypoint | ||
@entrypoint = normalize_entrypoint_url prometheus_entrypoint, 'api/v1' | ||
super(@entrypoint, credentials, options) | ||
end | ||
|
||
def query(metrics: [], time: nil) | ||
results = [] | ||
metrics.each do |metric| | ||
query = metric['expression'] | ||
response = http_get "/query?time=#{time}&query=#{query}" | ||
result = response['data']['result'].empty? ? {} : response['data']['result'].first | ||
result['metric'] = metric | ||
results << result | ||
end | ||
puts "DELETEME p8s - query #{results}" | ||
results | ||
end | ||
|
||
def query_range(metrics: [], starts: nil, ends: nil, step: nil) | ||
results = [] | ||
metrics.each do |metric| | ||
query = metric['expression'] | ||
response = http_get "/query_range?start=#{starts}&end=#{ends}&step=#{step}&query=#{query}" | ||
result = response['data']['result'].empty? ? {} : response['data']['result'].first | ||
result['metric'] = metric | ||
results << result | ||
end | ||
puts "DELETEME p8s - query_range #{results}" | ||
results | ||
end | ||
|
||
def up_time(feed_id: nil, starts: nil, ends: nil, step: nil) | ||
query = "up{feed_id=\"#{feed_id}\"}" | ||
response = http_get "/query_range?start=#{starts}&end=#{ends}&step=#{step}&query=#{query}" | ||
puts "DELETEME p8s - up_time feed_id #{feed_id} #{response['data']['result']}" | ||
response['data']['result'] | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
require_relative '../vcr/vcr_setup' | ||
require_relative '../spec_helper' | ||
|
||
module Hawkular::Prometheus::RSpec | ||
HAWKULAR_BASE = 'http://localhost:8080/' | ||
creds = {} | ||
options = {} | ||
|
||
describe 'Prometheus/Queries' do | ||
let(:cassette_name) do |example| | ||
description = example.description | ||
description | ||
end | ||
|
||
around(:each) do |example| | ||
record('Prometheus/Queries', credentials, cassette_name, example: example) | ||
end | ||
|
||
before(:each) do | ||
@client = Hawkular::Prometheus::Client.new(HAWKULAR_BASE, creds, options) | ||
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-21T09:00:00Z', | ||
ends: '2017-11-21T11:00:00Z', | ||
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 a metrics instant' 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(metrics: metrics, | ||
time: '2017-11-22T09:00:00Z') | ||
first = results.first | ||
second = results.last | ||
expect(first['metric']['displayName']).to eq 'Heap Used' | ||
expect(first['value'].size).to be > 0 | ||
expect(second['metric']['displayName']).to eq 'NonHeap Used' | ||
expect(second['value'].size).to be > 0 | ||
end | ||
|
||
it 'Should fetch up time' do | ||
results = @client.up_time(feed_id: 'attilan', | ||
starts: '2017-11-15T00:00:00Z', | ||
ends: '2017-11-22T23:00:00Z', | ||
step: '60m') | ||
expect(results.size).to be > 0 | ||
end | ||
end | ||
end |
122 changes: 122 additions & 0 deletions
122
spec/vcr_cassettes/Prometheus/Queries/Templates/Should_fetch_a_metrics_instant.yml
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.