debian-mirror-gitlab/spec/support/prometheus/additional_metrics_shared_examples.rb

160 lines
6 KiB
Ruby
Raw Normal View History

2019-10-12 21:52:04 +05:30
# frozen_string_literal: true
2017-09-10 17:25:29 +05:30
RSpec.shared_examples 'additional metrics query' do
include Prometheus::MetricBuilders
let(:metric_group_class) { Gitlab::Prometheus::MetricGroup }
let(:metric_class) { Gitlab::Prometheus::Metric }
let(:metric_names) { %w{metric_a metric_b} }
let(:query_range_result) do
[{ 'metric': {}, 'values': [[1488758662.506, '0.00002996364761904785'], [1488758722.506, '0.00003090239047619091']] }]
end
let(:client) { double('prometheus_client') }
let(:query_result) { described_class.new(client).query(*query_params) }
2020-03-13 15:44:24 +05:30
let(:project) { create(:project, :repository) }
2018-03-27 19:54:05 +05:30
let(:environment) { create(:environment, slug: 'environment-slug', project: project) }
2017-09-10 17:25:29 +05:30
before do
allow(client).to receive(:label_values).and_return(metric_names)
2018-03-27 19:54:05 +05:30
allow(metric_group_class).to receive(:common_metrics).and_return([simple_metric_group(metrics: [simple_metric])])
2017-09-10 17:25:29 +05:30
end
context 'metrics query context' do
subject! { described_class.new(client) }
shared_examples 'query context containing environment slug and filter' do
it 'contains ci_environment_slug' do
2018-11-18 11:00:15 +05:30
expect(subject).to receive(:query_metrics).with(project, environment, hash_including(ci_environment_slug: environment.slug))
2017-09-10 17:25:29 +05:30
subject.query(*query_params)
end
it 'contains environment filter' do
expect(subject).to receive(:query_metrics).with(
2018-03-27 19:54:05 +05:30
project,
2018-11-18 11:00:15 +05:30
environment,
2017-09-10 17:25:29 +05:30
hash_including(
environment_filter: "container_name!=\"POD\",environment=\"#{environment.slug}\""
)
)
subject.query(*query_params)
end
end
describe 'project has Kubernetes service' do
2019-09-04 21:01:54 +05:30
context 'when user configured kubernetes from CI/CD > Clusters' do
2020-03-13 15:44:24 +05:30
let!(:cluster) { create(:cluster, :project, :provided_by_gcp, projects: [project]) }
2018-03-17 18:26:18 +05:30
let(:environment) { create(:environment, slug: 'environment-slug', project: project) }
2019-10-12 21:52:04 +05:30
let(:kube_namespace) { environment.deployment_namespace }
2017-09-10 17:25:29 +05:30
2018-03-17 18:26:18 +05:30
it_behaves_like 'query context containing environment slug and filter'
2017-09-10 17:25:29 +05:30
2018-03-17 18:26:18 +05:30
it 'query context contains kube_namespace' do
2018-11-18 11:00:15 +05:30
expect(subject).to receive(:query_metrics).with(project, environment, hash_including(kube_namespace: kube_namespace))
2017-09-10 17:25:29 +05:30
2018-03-17 18:26:18 +05:30
subject.query(*query_params)
end
end
2017-09-10 17:25:29 +05:30
end
describe 'project without Kubernetes service' do
it_behaves_like 'query context containing environment slug and filter'
it 'query context contains empty kube_namespace' do
2018-11-18 11:00:15 +05:30
expect(subject).to receive(:query_metrics).with(project, environment, hash_including(kube_namespace: ''))
2017-09-10 17:25:29 +05:30
subject.query(*query_params)
end
end
end
context 'with one group where two metrics is found' do
before do
2018-03-27 19:54:05 +05:30
allow(metric_group_class).to receive(:common_metrics).and_return([simple_metric_group])
2017-09-10 17:25:29 +05:30
end
context 'some queries return results' do
before do
allow(client).to receive(:query_range).with('query_range_a', any_args).and_return(query_range_result)
allow(client).to receive(:query_range).with('query_range_b', any_args).and_return(query_range_result)
allow(client).to receive(:query_range).with('query_range_empty', any_args).and_return([])
end
it 'return group data only for queries with results' do
expected = [
{
group: 'name',
priority: 1,
metrics: [
{
title: 'title', weight: 1, 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 match_schema('prometheus/additional_metrics_query_result')
expect(query_result).to eq(expected)
end
end
end
context 'with two groups with one metric each' do
let(:metrics) { [simple_metric(queries: [simple_query])] }
before do
2018-03-27 19:54:05 +05:30
allow(metric_group_class).to receive(:common_metrics).and_return(
2017-09-10 17:25:29 +05:30
[
simple_metric_group(name: 'group_a', metrics: [simple_metric(queries: [simple_query])]),
simple_metric_group(name: 'group_b', metrics: [simple_metric(title: 'title_b', queries: [simple_query('b')])])
])
allow(client).to receive(:label_values).and_return(metric_names)
end
context 'both queries return results' do
before do
allow(client).to receive(:query_range).with('query_range_a', any_args).and_return(query_range_result)
allow(client).to receive(:query_range).with('query_range_b', any_args).and_return(query_range_result)
end
it 'return group data both queries' do
queries_with_result_a = { queries: [{ query_range: 'query_range_a', result: query_range_result }] }
queries_with_result_b = { queries: [{ query_range: 'query_range_b', result: query_range_result }] }
expect(query_result).to match_schema('prometheus/additional_metrics_query_result')
expect(query_result.count).to eq(2)
expect(query_result).to all(satisfy { |r| r[:metrics].count == 1 })
expect(query_result[0][:metrics].first).to include(queries_with_result_a)
expect(query_result[1][:metrics].first).to include(queries_with_result_b)
end
end
context 'one query returns result' do
before do
allow(client).to receive(:query_range).with('query_range_a', any_args).and_return(query_range_result)
allow(client).to receive(:query_range).with('query_range_b', any_args).and_return([])
end
it 'return group data only for query with results' do
queries_with_result = { queries: [{ query_range: 'query_range_a', result: query_range_result }] }
expect(query_result).to match_schema('prometheus/additional_metrics_query_result')
expect(query_result.count).to eq(1)
expect(query_result).to all(satisfy { |r| r[:metrics].count == 1 })
expect(query_result.first[:metrics].first).to include(queries_with_result)
end
end
end
end