blob: c48fcadee57a4a6e4fe244d58c095cb152b799aa (
plain)
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
|
module Gitlab::Prometheus::Queries
class AdditionalMetricsQuery < BaseQuery
def query(environment_id)
environment = Environment.find_by(id: environment_id)
query_context = {
environment_slug: environment.slug,
environment_filter: %{container_name!="POD",environment="#{environment.slug}"},
timeframe_start: 8.hours.ago.to_f,
timeframe_end: Time.now.to_f
}
query_metrics(query_context)
end
protected
def query_metrics(query_context)
query_processor = method(:process_query).curry[query_context]
matched_metrics.map do |group|
metrics = group.metrics.map do |metric|
{
title: metric.title,
weight: metric.weight,
queries: metric.queries.map(&query_processor)
}
end
{
group: group.name,
priority: group.priority,
metrics: metrics
}
end
end
private
def process_query(context, query)
query_with_result = query.dup
query_with_result[:result] =
if query.has_key?(:query_range)
client_query_range(query[:query_range] % context, start: context[:timeframe_start], stop: context[:timeframe_end])
else
client_query(query[:query] % context, time: context[:timeframe_end])
end
query_with_result
end
def process_result(query_result)
contains_metrics = query_result.all? do |item|
item&.[](:values)&.any? || item&.[](:value)&.any?
end
contains_metrics
end
def matched_metrics
label_values = client_label_values || []
Gitlab::Prometheus::MetricGroup.all
result = Gitlab::Prometheus::MetricGroup.all.map do |group|
group.metrics.select! do |metric|
matcher = Regexp.compile(metric.detect)
label_values.any? &matcher.method(:match)
end
group
end
result.select { |group| group.metrics.any? }
end
end
end
|