BigW Consortium Gitlab

Commit ae5268ce by Pawel Chojnacki

Additional Metrics tests

parent 6a70509a
...@@ -9,7 +9,6 @@ ...@@ -9,7 +9,6 @@
queries: queries:
- query_range: 'avg(container_memory_usage_bytes{%{environment_filter}}) / 2^20' - query_range: 'avg(container_memory_usage_bytes{%{environment_filter}}) / 2^20'
label: Container memory label: Container memory
display_empty: true
unit: MiB unit: MiB
- title: "Current memory usage" - title: "Current memory usage"
required_metrics: required_metrics:
......
...@@ -17,26 +17,42 @@ module Gitlab::Prometheus::Queries ...@@ -17,26 +17,42 @@ module Gitlab::Prometheus::Queries
def query_metrics(query_context) def query_metrics(query_context)
query_processor = method(:process_query).curry[query_context] query_processor = method(:process_query).curry[query_context]
matched_metrics.map do |group| groups = matched_metrics.map do |group|
metrics = group.metrics.map do |metric| metrics = group.metrics.map do |metric|
{ {
title: metric.title, title: metric.title,
weight: metric.weight, weight: metric.weight,
y_label: metric.y_label, y_label: metric.y_label,
queries: metric.queries.map(&query_processor) queries: metric.queries.map(&query_processor).select(&method(:query_with_result))
} }
end end
{ {
group: group.name, group: group.name,
priority: group.priority, priority: group.priority,
metrics: metrics metrics: metrics.select(&method(:metric_with_any_queries))
} }
end end
groups.select(&method(:group_with_any_metrics))
end end
private private
def metric_with_any_queries(metric)
metric[:queries]&.count > 0
end
def group_with_any_metrics(group)
group[:metrics]&.count > 0
end
def query_with_result(query)
query[:result]&.any? do |item|
item&.[](:values)&.any? || item&.[](:value)&.any?
end
end
def process_query(context, query) def process_query(context, query)
query_with_result = query.dup query_with_result = query.dup
query_with_result[:result] = query_with_result[:result] =
...@@ -48,22 +64,15 @@ module Gitlab::Prometheus::Queries ...@@ -48,22 +64,15 @@ module Gitlab::Prometheus::Queries
query_with_result query_with_result
end end
def process_result(query_result)
contains_metrics = query_result.all? do |item|
item&.[](:values)&.any? || item&.[](:value)&.any?
end
contains_metrics def available_metrics
@available_metrics ||= client_label_values || []
end end
def matched_metrics def matched_metrics
label_values = client_label_values || []
Gitlab::Prometheus::MetricGroup.all
result = Gitlab::Prometheus::MetricGroup.all.map do |group| result = Gitlab::Prometheus::MetricGroup.all.map do |group|
group.metrics.select! do |metric| group.metrics.select! do |metric|
matcher = Regexp.compile(metric.detect) metric.required_metrics.all?(&available_metrics.method(:include?))
label_values.any? &matcher.method(:match)
end end
group group
end end
......
require 'spec_helper'
describe Gitlab::Prometheus::Queries::AdditionalMetricsQuery, lib: true do
include Prometheus::AdditionalMetricsQueryHelper
let(:metric_group_class) { Gitlab::Prometheus::MetricGroup }
let(:metric_class) { Gitlab::Prometheus::Metric }
let(:client) { double('prometheus_client') }
let(:environment) { create(:environment, slug: 'environment-slug') }
subject(:query_result) { described_class.new(client).query(environment.id) }
context 'with one group where two metrics is found' do
before do
allow(metric_group_class).to receive(:all).and_return([simple_metric_group])
allow(client).to receive(:label_values).and_return(metric_names)
end
context 'some querie return results' do
before do
expect(client).to receive(:query_range).with('query_range_a', any_args).and_return(query_range_result)
expect(client).to receive(:query_range).with('query_range_b', any_args).and_return(query_range_result)
expect(client).to receive(:query_range).with('query_range_empty', any_args).and_return([])
end
it 'return results only for queries with results' do
puts query_result
expected = {
group: 'name',
priority: 1,
metrics:
[
{
title: 'title', weight: nil, y_label: 'Values', queries:
[
{ query_range: 'query_range_a', result: query_range_result },
{ query_range: 'query_range_b', label: 'label', unit: 'unit', result: query_range_result }
]
}
]
}
expect(query_result).to eq([expected])
end
end
end
end
...@@ -10,7 +10,7 @@ describe Gitlab::Prometheus::Queries::MatchedMetricsQuery, lib: true do ...@@ -10,7 +10,7 @@ describe Gitlab::Prometheus::Queries::MatchedMetricsQuery, lib: true do
subject { described_class.new(client) } subject { described_class.new(client) }
context 'with one group where two metrics are found' do context 'with one group where two metrics is found' do
before do before do
allow(metric_group_class).to receive(:all).and_return([simple_metric_group]) allow(metric_group_class).to receive(:all).and_return([simple_metric_group])
allow(client).to receive(:label_values).and_return(metric_names) allow(client).to receive(:label_values).and_return(metric_names)
......
module Prometheus
module AdditionalMetricsQueryHelper
def metric_names
%w{metric_a metric_b}
end
def simple_queries
[{ query_range: 'query_range_a' }, { query_range: 'query_range_b', label: 'label', unit: 'unit' }]
end
def simple_query(suffix = 'a')
[{ query_range: "query_range_#{suffix}" }]
end
def simple_metrics
[
Gitlab::Prometheus::Metric.new('title', %w(metric_a metric_b), nil, nil, simple_queries),
Gitlab::Prometheus::Metric.new('title', %w{metric_a}, nil, nil, simple_query('empty')),
Gitlab::Prometheus::Metric.new('title', %w{metric_c}, nil, nil)
]
end
def simple_metric_group(name = 'name', metrics = simple_metrics)
Gitlab::Prometheus::MetricGroup.new(name, 1, metrics)
end
def query_result
[
{
'metric': {},
'value': [
1488772511.004,
'0.000041021495238095323'
]
}
]
end
def query_range_result
[
{
'metric': {},
'values': [
[1488758662.506, '0.00002996364761904785'],
[1488758722.506, '0.00003090239047619091']
]
}
]
end
end
end
...@@ -5,12 +5,12 @@ module Prometheus ...@@ -5,12 +5,12 @@ module Prometheus
end end
def simple_metrics(metric_name = 'metric_a') def simple_metrics(metric_name = 'metric_a')
[metric_class.new('title', %W(#{metric_name} metric_b), nil, nil), [Gitlab::Prometheus::Metric.new('title', %W(#{metric_name} metric_b), nil, nil),
metric_class.new('title', [metric_name], nil, nil)] Gitlab::Prometheus::Metric.new('title', [metric_name], nil, nil)]
end end
def simple_metric_group(name = 'name', metrics = simple_metrics) def simple_metric_group(name = 'name', metrics = simple_metrics)
metric_group_class.new(name, 1, metrics) Gitlab::Prometheus::MetricGroup.new(name, 1, metrics)
end end
def series_info_with_environment(*more_metrics) def series_info_with_environment(*more_metrics)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment