2017-06-06 14:07:33 +00:00
|
|
|
module Gitlab
|
|
|
|
module Prometheus
|
|
|
|
module Queries
|
|
|
|
module QueryAdditionalMetrics
|
|
|
|
def query_metrics(query_context)
|
|
|
|
query_processor = method(:process_query).curry[query_context]
|
|
|
|
|
|
|
|
groups = matched_metrics.map do |group|
|
|
|
|
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
|
|
|
|
|
|
|
|
groups.select(&method(:group_with_any_metrics))
|
|
|
|
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)
|
|
|
|
query_with_result = 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-06-06 14:07:33 +00:00
|
|
|
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
|
2017-06-09 10:43:12 +00:00
|
|
|
query_with_result[:result] = result&.map(&:deep_symbolize_keys)
|
2017-06-06 14:07:33 +00:00
|
|
|
query_with_result
|
|
|
|
end
|
|
|
|
|
|
|
|
def available_metrics
|
|
|
|
@available_metrics ||= client_label_values || []
|
|
|
|
end
|
|
|
|
|
|
|
|
def matched_metrics
|
|
|
|
result = Gitlab::Prometheus::MetricGroup.all.map do |group|
|
|
|
|
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
|
|
|
|
|
|
|
def common_query_context(environment)
|
|
|
|
variables = {
|
|
|
|
ci_environment_slug: environment.slug,
|
|
|
|
kube_namespace: environment.project.kubernetes_service.actual_namespace,
|
|
|
|
}.flat_map { |k, v| [[k, v], [k.upcase, v]] }.to_h
|
|
|
|
|
|
|
|
macros = {
|
|
|
|
environment_filter: %{container_name!="POD",environment="#{environment.slug}"}
|
|
|
|
}
|
|
|
|
variables.merge(macros)
|
|
|
|
end
|
2017-06-06 14:07:33 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|