Commit 6a70509a authored by Pawel Chojnacki's avatar Pawel Chojnacki

Towards Reviewable prometheus

parent e74896df
......@@ -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
......
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'
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 }
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' }])
]
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)
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
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
allow(client).to receive(:label_values).and_return(['metrica', 'metricb'])
allow(client).to receive(:series).and_return(xx)
context 'both metrics in the group pass requirements' do
before do
allow(client).to receive(:series).and_return(series_info_with_environment)
end
it "something something" do
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
context 'no metrics 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: 'name', priority: 1, active_metrics: 0, metrics_missing_requirements: 1 }])
end
end
end
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
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
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