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
135
136
137
138
139
140
141
142
143
|
RSpec.shared_examples 'additional metrics query' do
include Prometheus::MetricBuilders
before do
allow(client).to receive(:label_values).and_return(metric_names)
allow(metric_group_class).to receive(:all).and_return([simple_metric_group(metrics: [simple_metric])])
end
let(:metric_group_class) { Gitlab::Prometheus::MetricGroup }
let(:metric_class) { Gitlab::Prometheus::Metric }
context 'with one group where two metrics is found' do
before do
allow(metric_group_class).to receive(:all).and_return([simple_metric_group])
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: nil, 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 eq(expected)
end
end
end
context 'with two groups with one metric each' do
let(:metrics) { [simple_metric(queries: [simple_query])] }
before do
allow(metric_group_class).to receive(:all).and_return(
[
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
expected = [
{
group: 'group_a',
priority: 1,
metrics: [
{
title: 'title',
weight: nil,
y_label: 'Values',
queries: [
{
query_range: 'query_range_a',
result: [
{
metric: {},
values: [[1488758662.506, '0.00002996364761904785'], [1488758722.506, '0.00003090239047619091']] }
]
}
]
}
]
},
{
group: 'group_b',
priority: 1,
metrics: [
{
title: 'title_b',
weight: nil,
y_label: 'Values',
queries: [
{
query_range: 'query_range_b', result: [
{
metric: {},
values: [[1488758662.506, '0.00002996364761904785'], [1488758722.506, '0.00003090239047619091']]
}
]
}
]
}
]
}
]
expect(query_result).to eq(expected)
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
expected = [
{
group: 'group_a',
priority: 1,
metrics: [
{
title: 'title',
weight: nil,
y_label: 'Values',
queries: [
{
query_range: 'query_range_a',
result: query_range_result
}
]
}
]
}
]
expect(query_result).to eq(expected)
end
end
end
end
|