diff options
author | Pawel Chojnacki <pawel@chojnacki.ws> | 2017-06-05 11:32:56 +0200 |
---|---|---|
committer | Pawel Chojnacki <pawel@chojnacki.ws> | 2017-06-05 11:37:07 +0200 |
commit | ae5268ce8cc533be4086a11d9d89fa726136d59d (patch) | |
tree | e61b5177d712bf8a6807efce3cf32da55863abbc | |
parent | 6a70509a2763717e592c603249855bfb43519d2f (diff) | |
download | gitlab-ce-ae5268ce8cc533be4086a11d9d89fa726136d59d.tar.gz |
Additional Metrics tests
6 files changed, 126 insertions, 18 deletions
diff --git a/config/additional_metrics.yml b/config/additional_metrics.yml index d84db1530a8..daecde49570 100644 --- a/config/additional_metrics.yml +++ b/config/additional_metrics.yml @@ -9,7 +9,6 @@ queries: - query_range: 'avg(container_memory_usage_bytes{%{environment_filter}}) / 2^20' label: Container memory - display_empty: true unit: MiB - title: "Current memory usage" required_metrics: diff --git a/lib/gitlab/prometheus/queries/additional_metrics_query.rb b/lib/gitlab/prometheus/queries/additional_metrics_query.rb index fd7f072834d..d21a3978e25 100644 --- a/lib/gitlab/prometheus/queries/additional_metrics_query.rb +++ b/lib/gitlab/prometheus/queries/additional_metrics_query.rb @@ -17,26 +17,42 @@ module Gitlab::Prometheus::Queries def query_metrics(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| { title: metric.title, weight: metric.weight, y_label: metric.y_label, - queries: metric.queries.map(&query_processor) + queries: metric.queries.map(&query_processor).select(&method(:query_with_result)) } end { group: group.name, priority: group.priority, - metrics: metrics + metrics: metrics.select(&method(:metric_with_any_queries)) } end + + groups.select(&method(:group_with_any_metrics)) end 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) query_with_result = query.dup query_with_result[:result] = @@ -48,22 +64,15 @@ module Gitlab::Prometheus::Queries query_with_result 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 def matched_metrics - label_values = client_label_values || [] - Gitlab::Prometheus::MetricGroup.all - result = Gitlab::Prometheus::MetricGroup.all.map do |group| group.metrics.select! do |metric| - matcher = Regexp.compile(metric.detect) - label_values.any? &matcher.method(:match) + metric.required_metrics.all?(&available_metrics.method(:include?)) end group end diff --git a/spec/lib/gitlab/prometheus/queries/additional_metrics_query_spec.rb b/spec/lib/gitlab/prometheus/queries/additional_metrics_query_spec.rb new file mode 100644 index 00000000000..c7e2dbc12ec --- /dev/null +++ b/spec/lib/gitlab/prometheus/queries/additional_metrics_query_spec.rb @@ -0,0 +1,49 @@ +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 diff --git a/spec/lib/gitlab/prometheus/queries/matched_metrics_query_spec.rb b/spec/lib/gitlab/prometheus/queries/matched_metrics_query_spec.rb index d46de56f520..390fff568cc 100644 --- a/spec/lib/gitlab/prometheus/queries/matched_metrics_query_spec.rb +++ b/spec/lib/gitlab/prometheus/queries/matched_metrics_query_spec.rb @@ -10,7 +10,7 @@ describe Gitlab::Prometheus::Queries::MatchedMetricsQuery, lib: true do 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 allow(metric_group_class).to receive(:all).and_return([simple_metric_group]) allow(client).to receive(:label_values).and_return(metric_names) diff --git a/spec/support/prometheus/additional_metrics_query_helper.rb b/spec/support/prometheus/additional_metrics_query_helper.rb new file mode 100644 index 00000000000..d80beb066ff --- /dev/null +++ b/spec/support/prometheus/additional_metrics_query_helper.rb @@ -0,0 +1,51 @@ +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 diff --git a/spec/support/prometheus/matched_metrics_query_helper.rb b/spec/support/prometheus/matched_metrics_query_helper.rb index ecaf85e3338..86e874fb295 100644 --- a/spec/support/prometheus/matched_metrics_query_helper.rb +++ b/spec/support/prometheus/matched_metrics_query_helper.rb @@ -5,12 +5,12 @@ module Prometheus end def simple_metrics(metric_name = 'metric_a') - [metric_class.new('title', %W(#{metric_name} metric_b), nil, nil), - metric_class.new('title', [metric_name], nil, nil)] + [Gitlab::Prometheus::Metric.new('title', %W(#{metric_name} metric_b), nil, nil), + Gitlab::Prometheus::Metric.new('title', [metric_name], nil, nil)] end def simple_metric_group(name = 'name', metrics = simple_metrics) - metric_group_class.new(name, 1, metrics) + Gitlab::Prometheus::MetricGroup.new(name, 1, metrics) end def series_info_with_environment(*more_metrics) |