summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/gitlab/prometheus/queries/matched_metrics_query.rb7
-rw-r--r--spec/db/production/settings.rb.orig16
-rw-r--r--spec/lib/gitlab/prometheus/queries/matched_metrics_query_spec.rb125
-rw-r--r--spec/support/prometheus/matched_metrics_query_helper.rb33
4 files changed, 146 insertions, 35 deletions
diff --git a/lib/gitlab/prometheus/queries/matched_metrics_query.rb b/lib/gitlab/prometheus/queries/matched_metrics_query.rb
index a5e1f6a3fde..fc97bca486c 100644
--- a/lib/gitlab/prometheus/queries/matched_metrics_query.rb
+++ b/lib/gitlab/prometheus/queries/matched_metrics_query.rb
@@ -23,11 +23,10 @@ module Gitlab::Prometheus::Queries
metrics_groups.each do |group|
groups[group] ||= { active_metrics: 0, metrics_missing_requirements: 0 }
- metrics = group.metrics.flat_map(&:required_metrics)
- active_metrics = metrics.count(&lookup.method(:has_key?))
+ active_metrics = group.metrics.count { |metric| metric.required_metrics.all?(&lookup.method(:has_key?)) }
groups[group][:active_metrics] += active_metrics
- groups[group][:metrics_missing_requirements] += metrics.count - active_metrics
+ groups[group][:metrics_missing_requirements] += group.metrics.count - active_metrics
end
groups
@@ -48,7 +47,7 @@ module Gitlab::Prometheus::Queries
end
def has_matching_label(series_info)
- series_info.has_key?('environment')
+ series_info.key?('environment')
end
def available_metrics
diff --git a/spec/db/production/settings.rb.orig b/spec/db/production/settings.rb.orig
new file mode 100644
index 00000000000..3cbb173c4cc
--- /dev/null
+++ b/spec/db/production/settings.rb.orig
@@ -0,0 +1,16 @@
+require 'spec_helper'
+
+describe 'seed production settings', lib: true do
+ include StubENV
+
+ context 'GITLAB_SHARED_RUNNERS_REGISTRATION_TOKEN is set in the environment' do
+ before do
+ stub_env('GITLAB_SHARED_RUNNERS_REGISTRATION_TOKEN', '013456789')
+ end
+
+ it 'writes the token to the database' do
+ load(File.join(__dir__, '../../../db/fixtures/production/010_settings.rb'))
+ expect(ApplicationSetting.current.runners_registration_token).to eq('013456789')
+ 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 0aee9d37889..d46de56f520 100644
--- a/spec/lib/gitlab/prometheus/queries/matched_metrics_query_spec.rb
+++ b/spec/lib/gitlab/prometheus/queries/matched_metrics_query_spec.rb
@@ -1,50 +1,113 @@
require 'spec_helper'
describe Gitlab::Prometheus::Queries::MatchedMetricsQuery, lib: true do
- let(:environment) { create(:environment, slug: 'environment-slug') }
- let(:deployment) { create(:deployment, environment: environment) }
+ include Prometheus::MatchedMetricsQueryHelper
+
+ let(:metric_group_class) { Gitlab::Prometheus::MetricGroup }
+ let(:metric_class) { Gitlab::Prometheus::Metric }
let(:client) { double('prometheus_client') }
+
subject { described_class.new(client) }
- around do |example|
- time_without_subsecond_values = Time.local(2008, 9, 1, 12, 0, 0)
- Timecop.freeze(time_without_subsecond_values) { example.run }
- end
+ context 'with one group where two metrics are 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
- let(:metric_group_class) { Gitlab::Prometheus::MetricGroup }
- let(:metric_class) { Gitlab::Prometheus::Metric }
+ context 'both metrics in the group pass requirements' do
+ before do
+ allow(client).to receive(:series).and_return(series_info_with_environment)
+ end
- let(:simple_metrics) do
- [
- metric_class.new('title', ['metrica', 'metricb'], '1', 'y_label', [{ :query_range => 'avg' }])
- ]
- end
+ it 'responds with both metrics as actve' do
+ expect(subject.query).to eq([{ group: 'name', priority: 1, active_metrics: 2, metrics_missing_requirements: 0 }])
+ end
+ end
- let(:simple_metric_group) do
- metric_group_class.new('name', 1, simple_metrics)
- end
+ context 'none of the metrics pass requirements' do
+ before do
+ allow(client).to receive(:series).and_return(series_info_without_environment)
+ end
- let(:xx) do
- [{
- '__name__': 'metrica',
- 'environment': 'mattermost'
- },
- {
- '__name__': 'metricb',
- 'environment': 'mattermost'
- }]
+ it 'responds with both metrics missing requirements' do
+ expect(subject.query).to eq([{ group: 'name', priority: 1, active_metrics: 0, metrics_missing_requirements: 2 }])
+ end
+ end
+
+ context 'no series information found about the metrics' do
+ before do
+ allow(client).to receive(:series).and_return(empty_series_info)
+ end
+
+ it 'responds with both metrics missing requirements' do
+ expect(subject.query).to eq([{ group: 'name', priority: 1, active_metrics: 0, metrics_missing_requirements: 2 }])
+ end
+ end
+
+ context 'one of the series info was not found' do
+ before do
+ allow(client).to receive(:series).and_return(partialy_empty_series_info)
+ end
+ it 'responds with one active and one missing metric' do
+ expect(subject.query).to eq([{ group: 'name', priority: 1, active_metrics: 1, metrics_missing_requirements: 1 }])
+ end
+ end
end
- before do
- allow(metric_group_class).to receive(:all).and_return([simple_metric_group])
+ context 'with one group where only one metric 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_a')
+ end
+
+ context 'both metrics in the group pass requirements' do
+ before do
+ allow(client).to receive(:series).and_return(series_info_with_environment)
+ end
+
+ it 'responds with one metrics as active and no missing requiremens' do
+ expect(subject.query).to eq([{ group: 'name', priority: 1, active_metrics: 1, metrics_missing_requirements: 0 }])
+ end
+ end
- allow(client).to receive(:label_values).and_return(['metrica', 'metricb'])
- allow(client).to receive(:series).and_return(xx)
+ context 'no metrics in group pass requirements' do
+ before do
+ allow(client).to receive(:series).and_return(series_info_without_environment)
+ end
+
+ it 'responds with one metrics as active and no missing requiremens' do
+ expect(subject.query).to eq([{ group: 'name', priority: 1, active_metrics: 0, metrics_missing_requirements: 1 }])
+ end
+ end
end
- it "something something" do
+ context 'with two groups where only one metric is found' do
+ before do
+ allow(metric_group_class).to receive(:all).and_return([simple_metric_group,
+ simple_metric_group('nameb', simple_metrics('metric_c'))])
+ allow(client).to receive(:label_values).and_return('metric_c')
+ end
+
+ context 'both metrics in the group pass requirements' do
+ before do
+ allow(client).to receive(:series).and_return(series_info_with_environment('metric_c'))
+ end
+
+ it 'responds with one metrics as active and no missing requiremens' do
+ expect(subject.query).to eq([{ group: 'nameb', priority: 1, active_metrics: 1, metrics_missing_requirements: 0 }])
+ end
+ end
+
+ context 'no metris in group pass requirements' do
+ before do
+ allow(client).to receive(:series).and_return(series_info_without_environment)
+ end
- expect(subject.query).to eq("asf")
+ it 'responds with one metrics as active and no missing requiremens' do
+ expect(subject.query).to eq([{ group: 'nameb', priority: 1, active_metrics: 0, metrics_missing_requirements: 1 }])
+ end
+ end
end
end
diff --git a/spec/support/prometheus/matched_metrics_query_helper.rb b/spec/support/prometheus/matched_metrics_query_helper.rb
new file mode 100644
index 00000000000..ecaf85e3338
--- /dev/null
+++ b/spec/support/prometheus/matched_metrics_query_helper.rb
@@ -0,0 +1,33 @@
+module Prometheus
+ module MatchedMetricsQueryHelper
+ def metric_names
+ %w{metric_a metric_b}
+ 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)]
+ end
+
+ def simple_metric_group(name = 'name', metrics = simple_metrics)
+ metric_group_class.new(name, 1, metrics)
+ end
+
+ def series_info_with_environment(*more_metrics)
+ %w{metric_a metric_b}.concat(more_metrics).map { |metric_name| { '__name__' => metric_name, 'environment' => '' } }
+ end
+
+ def series_info_without_environment
+ [{ '__name__' => 'metric_a' },
+ { '__name__' => 'metric_b' }]
+ end
+
+ def partialy_empty_series_info
+ [{ '__name__' => 'metric_a', 'environment' => '' }]
+ end
+
+ def empty_series_info
+ []
+ end
+ end
+end