1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
require 'spec_helper'
describe Gitlab::Prometheus::Queries::MatchedMetricsQuery do
include Prometheus::MetricBuilders
let(:metric_group_class) { Gitlab::Prometheus::MetricGroup }
let(:metric_class) { Gitlab::Prometheus::Metric }
def series_info_with_environment(*more_metrics)
%w{metric_a metric_b}.concat(more_metrics).map { |metric_name| { '__name__' => metric_name, 'environment' => '' } }
end
let(:metric_names) { %w{metric_a metric_b} }
let(:series_info_without_environment) do
[{ '__name__' => 'metric_a' },
{ '__name__' => 'metric_b' }]
end
let(:partialy_empty_series_info) { [{ '__name__' => 'metric_a', 'environment' => '' }] }
let(:empty_series_info) { [] }
let(:client) { double('prometheus_client') }
subject { described_class.new(client) }
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 '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 both metrics as actve' do
expect(subject.query).to eq([{ group: 'name', priority: 1, active_metrics: 2, metrics_missing_requirements: 0 }])
end
end
context 'none of the metrics pass requirements' do
before do
allow(client).to receive(:series).and_return(series_info_without_environment)
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 '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
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
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
context 'with two groups where metrics are found in each group' do
let(:second_metric_group) { simple_metric_group(name: 'nameb', metrics: simple_metrics(added_metric_name: 'metric_c')) }
before do
allow(metric_group_class).to receive(:all).and_return([simple_metric_group, second_metric_group])
allow(client).to receive(:label_values).and_return('metric_c')
end
context 'all metrics in both groups 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: 'name', priority: 1, active_metrics: 1, metrics_missing_requirements: 0 },
{ group: 'nameb', priority: 1, active_metrics: 2, metrics_missing_requirements: 0 }
]
)
end
end
context 'no metrics in groups 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 },
{ group: 'nameb', priority: 1, active_metrics: 0, metrics_missing_requirements: 2 }
]
)
end
end
end
end