2017-09-10 17:25:29 +05:30
|
|
|
module Gitlab
|
|
|
|
module Prometheus
|
|
|
|
module Queries
|
|
|
|
module QueryAdditionalMetrics
|
2018-03-27 19:54:05 +05:30
|
|
|
def query_metrics(project, query_context)
|
|
|
|
matched_metrics(project).map(&query_group(query_context))
|
|
|
|
.select(&method(:group_with_any_metrics))
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
def query_group(query_context)
|
2017-09-10 17:25:29 +05:30
|
|
|
query_processor = method(:process_query).curry[query_context]
|
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
lambda do |group|
|
2017-09-10 17:25:29 +05:30
|
|
|
metrics = group.metrics.map do |metric|
|
|
|
|
{
|
|
|
|
title: metric.title,
|
|
|
|
weight: metric.weight,
|
|
|
|
y_label: metric.y_label,
|
|
|
|
queries: metric.queries.map(&query_processor).select(&method(:query_with_result))
|
|
|
|
}
|
|
|
|
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|
|
|
|
|
item&.[](:values)&.any? || item&.[](:value)&.any?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def process_query(context, query)
|
|
|
|
query = query.dup
|
|
|
|
result =
|
|
|
|
if query.key?(:query_range)
|
|
|
|
query[:query_range] %= context
|
|
|
|
client_query_range(query[:query_range], start: context[:timeframe_start], stop: context[:timeframe_end])
|
|
|
|
else
|
|
|
|
query[:query] %= context
|
|
|
|
client_query(query[:query], time: context[:timeframe_end])
|
|
|
|
end
|
|
|
|
|
|
|
|
query[:result] = result&.map(&:deep_symbolize_keys)
|
|
|
|
query
|
|
|
|
end
|
|
|
|
|
|
|
|
def available_metrics
|
|
|
|
@available_metrics ||= client_label_values || []
|
|
|
|
end
|
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
def matched_metrics(project)
|
|
|
|
result = Gitlab::Prometheus::MetricGroup.for_project(project).map do |group|
|
2017-09-10 17:25:29 +05:30
|
|
|
group.metrics.select! do |metric|
|
|
|
|
metric.required_metrics.all?(&available_metrics.method(:include?))
|
|
|
|
end
|
|
|
|
group
|
|
|
|
end
|
|
|
|
|
|
|
|
result.select { |group| group.metrics.any? }
|
|
|
|
end
|
|
|
|
|
|
|
|
def common_query_context(environment, timeframe_start:, timeframe_end:)
|
2018-03-27 19:54:05 +05:30
|
|
|
base_query_context(timeframe_start, timeframe_end).merge({
|
2017-09-10 17:25:29 +05:30
|
|
|
ci_environment_slug: environment.slug,
|
2018-05-09 12:01:36 +05:30
|
|
|
kube_namespace: environment.deployment_platform&.actual_namespace || '',
|
2017-09-10 17:25:29 +05:30
|
|
|
environment_filter: %{container_name!="POD",environment="#{environment.slug}"}
|
2018-03-27 19:54:05 +05:30
|
|
|
})
|
|
|
|
end
|
|
|
|
|
|
|
|
def base_query_context(timeframe_start, timeframe_end)
|
|
|
|
{
|
|
|
|
timeframe_start: timeframe_start,
|
|
|
|
timeframe_end: timeframe_end
|
2017-09-10 17:25:29 +05:30
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|