BigW Consortium Gitlab

Commit 6a70509a by Pawel Chojnacki

Towards Reviewable prometheus

parent e74896df
...@@ -23,11 +23,10 @@ module Gitlab::Prometheus::Queries ...@@ -23,11 +23,10 @@ module Gitlab::Prometheus::Queries
metrics_groups.each do |group| metrics_groups.each do |group|
groups[group] ||= { active_metrics: 0, metrics_missing_requirements: 0 } groups[group] ||= { active_metrics: 0, metrics_missing_requirements: 0 }
metrics = group.metrics.flat_map(&:required_metrics) active_metrics = group.metrics.count { |metric| metric.required_metrics.all?(&lookup.method(:has_key?)) }
active_metrics = metrics.count(&lookup.method(:has_key?))
groups[group][:active_metrics] += active_metrics 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 end
groups groups
...@@ -48,7 +47,7 @@ module Gitlab::Prometheus::Queries ...@@ -48,7 +47,7 @@ module Gitlab::Prometheus::Queries
end end
def has_matching_label(series_info) def has_matching_label(series_info)
series_info.has_key?('environment') series_info.key?('environment')
end end
def available_metrics def available_metrics
......
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
require 'spec_helper' require 'spec_helper'
describe Gitlab::Prometheus::Queries::MatchedMetricsQuery, lib: true do describe Gitlab::Prometheus::Queries::MatchedMetricsQuery, lib: true do
let(:environment) { create(:environment, slug: 'environment-slug') } include Prometheus::MatchedMetricsQueryHelper
let(:deployment) { create(:deployment, environment: environment) }
let(:metric_group_class) { Gitlab::Prometheus::MetricGroup }
let(:metric_class) { Gitlab::Prometheus::Metric }
let(:client) { double('prometheus_client') } let(:client) { double('prometheus_client') }
subject { described_class.new(client) } subject { described_class.new(client) }
around do |example| context 'with one group where two metrics are found' do
time_without_subsecond_values = Time.local(2008, 9, 1, 12, 0, 0) before do
Timecop.freeze(time_without_subsecond_values) { example.run } allow(metric_group_class).to receive(:all).and_return([simple_metric_group])
end allow(client).to receive(:label_values).and_return(metric_names)
end
let(:metric_group_class) { Gitlab::Prometheus::MetricGroup } context 'both metrics in the group pass requirements' do
let(:metric_class) { Gitlab::Prometheus::Metric } before do
allow(client).to receive(:series).and_return(series_info_with_environment)
end
let(:simple_metrics) do it 'responds with both metrics as actve' do
[ expect(subject.query).to eq([{ group: 'name', priority: 1, active_metrics: 2, metrics_missing_requirements: 0 }])
metric_class.new('title', ['metrica', 'metricb'], '1', 'y_label', [{ :query_range => 'avg' }]) end
] end
end
let(:simple_metric_group) do context 'none of the metrics pass requirements' do
metric_group_class.new('name', 1, simple_metrics) before do
end allow(client).to receive(:series).and_return(series_info_without_environment)
end
let(:xx) do it 'responds with both metrics missing requirements' do
[{ expect(subject.query).to eq([{ group: 'name', priority: 1, active_metrics: 0, metrics_missing_requirements: 2 }])
'__name__': 'metrica', end
'environment': 'mattermost' end
},
{ context 'no series information found about the metrics' do
'__name__': 'metricb', before do
'environment': 'mattermost' 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 end
before do context 'with one group where only one metric is found' do
allow(metric_group_class).to receive(:all).and_return([simple_metric_group]) 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']) context 'no metrics in group pass requirements' do
allow(client).to receive(:series).and_return(xx) 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 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
end end
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
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