2018-11-20 02:01:13 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-06-06 14:07:33 +00:00
|
|
|
module Gitlab
|
|
|
|
module Prometheus
|
|
|
|
module Queries
|
|
|
|
module QueryAdditionalMetrics
|
2018-06-25 09:57:29 +00:00
|
|
|
def query_metrics(project, environment, query_context)
|
2018-02-23 20:33:33 +00:00
|
|
|
matched_metrics(project).map(&query_group(query_context))
|
|
|
|
.select(&method(:group_with_any_metrics))
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
def query_group(query_context)
|
2017-06-06 14:07:33 +00:00
|
|
|
query_processor = method(:process_query).curry[query_context]
|
|
|
|
|
2018-02-23 20:33:33 +00:00
|
|
|
lambda do |group|
|
2017-06-06 14:07:33 +00:00
|
|
|
metrics = group.metrics.map do |metric|
|
2018-07-25 08:31:50 +00:00
|
|
|
metric_hsh = {
|
2017-06-06 14:07:33 +00:00
|
|
|
title: metric.title,
|
|
|
|
weight: metric.weight,
|
|
|
|
y_label: metric.y_label,
|
|
|
|
queries: metric.queries.map(&query_processor).select(&method(:query_with_result))
|
|
|
|
}
|
2018-07-25 08:31:50 +00:00
|
|
|
|
|
|
|
metric_hsh[:id] = metric.id if metric.id
|
|
|
|
|
|
|
|
metric_hsh
|
2017-06-06 14:07:33 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
{
|
|
|
|
group: group.name,
|
|
|
|
priority: group.priority,
|
|
|
|
metrics: metrics.select(&method(:metric_with_any_queries))
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def metric_with_any_queries(metric)
|
|
|
|
metric[:queries]&.count&.> 0
|
|
|
|
end
|
|
|
|
|
|
|
|
def group_with_any_metrics(group)
|
|
|
|
group[:metrics]&.count&.> 0
|
|
|
|
end
|
|
|
|
|
|
|
|
def query_with_result(query)
|
|
|
|
query[:result]&.any? do |item|
|
2017-06-09 10:43:12 +00:00
|
|
|
item&.[](:values)&.any? || item&.[](:value)&.any?
|
2017-06-06 14:07:33 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def process_query(context, query)
|
2017-07-31 11:29:00 +00:00
|
|
|
query = query.dup
|
2017-06-09 10:43:12 +00:00
|
|
|
result =
|
2017-06-07 10:41:06 +00:00
|
|
|
if query.key?(:query_range)
|
2017-07-31 11:29:00 +00:00
|
|
|
query[:query_range] %= context
|
2020-03-24 00:09:24 +00:00
|
|
|
client_query_range(query[:query_range], start_time: context[:timeframe_start], end_time: context[:timeframe_end])
|
2017-06-06 14:07:33 +00:00
|
|
|
else
|
2017-07-31 11:29:00 +00:00
|
|
|
query[:query] %= context
|
|
|
|
client_query(query[:query], time: context[:timeframe_end])
|
2017-06-06 14:07:33 +00:00
|
|
|
end
|
2017-08-01 18:28:10 +00:00
|
|
|
|
2017-07-31 11:29:00 +00:00
|
|
|
query[:result] = result&.map(&:deep_symbolize_keys)
|
|
|
|
query
|
2017-06-06 14:07:33 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def available_metrics
|
|
|
|
@available_metrics ||= client_label_values || []
|
|
|
|
end
|
|
|
|
|
2018-02-23 17:58:40 +00:00
|
|
|
def matched_metrics(project)
|
|
|
|
result = Gitlab::Prometheus::MetricGroup.for_project(project).map do |group|
|
2017-06-06 14:07:33 +00:00
|
|
|
group.metrics.select! do |metric|
|
|
|
|
metric.required_metrics.all?(&available_metrics.method(:include?))
|
|
|
|
end
|
|
|
|
group
|
|
|
|
end
|
|
|
|
|
|
|
|
result.select { |group| group.metrics.any? }
|
|
|
|
end
|
2017-07-28 21:02:21 +00:00
|
|
|
|
2017-08-01 20:11:59 +00:00
|
|
|
def common_query_context(environment, timeframe_start:, timeframe_end:)
|
2018-11-16 14:02:43 +00:00
|
|
|
base_query_context(timeframe_start, timeframe_end)
|
|
|
|
.merge(QueryVariables.call(environment))
|
2018-02-23 20:33:33 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def base_query_context(timeframe_start, timeframe_end)
|
|
|
|
{
|
|
|
|
timeframe_start: timeframe_start,
|
|
|
|
timeframe_end: timeframe_end
|
2017-07-28 21:02:21 +00:00
|
|
|
}
|
|
|
|
end
|
2017-06-06 14:07:33 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-04-28 09:09:34 +00:00
|
|
|
|
|
|
|
Gitlab::Prometheus::Queries::QueryAdditionalMetrics.prepend_if_ee('EE::Gitlab::Prometheus::Queries::QueryAdditionalMetrics')
|