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
|
2018-02-23 20:33:33 +00:00
|
|
|
class MatchedMetricQuery < BaseQuery
|
2017-06-06 14:07:33 +00:00
|
|
|
MAX_QUERY_ITEMS = 40.freeze
|
2017-05-10 09:25:30 +00:00
|
|
|
|
2017-06-06 14:07:33 +00:00
|
|
|
def query
|
|
|
|
groups_data.map do |group, data|
|
|
|
|
{
|
|
|
|
group: group.name,
|
|
|
|
priority: group.priority,
|
|
|
|
active_metrics: data[:active_metrics],
|
|
|
|
metrics_missing_requirements: data[:metrics_missing_requirements]
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
2017-05-10 09:25:30 +00:00
|
|
|
|
2017-06-06 14:07:33 +00:00
|
|
|
private
|
2017-05-25 12:01:10 +00:00
|
|
|
|
2017-06-06 14:07:33 +00:00
|
|
|
def groups_data
|
2018-02-23 17:58:40 +00:00
|
|
|
metrics_groups = groups_with_active_metrics(Gitlab::Prometheus::MetricGroup.common_metrics)
|
2017-06-06 14:07:33 +00:00
|
|
|
lookup = active_series_lookup(metrics_groups)
|
2017-05-10 09:25:30 +00:00
|
|
|
|
2017-06-06 14:07:33 +00:00
|
|
|
groups = {}
|
2017-05-10 09:25:30 +00:00
|
|
|
|
2017-06-06 14:07:33 +00:00
|
|
|
metrics_groups.each do |group|
|
|
|
|
groups[group] ||= { active_metrics: 0, metrics_missing_requirements: 0 }
|
|
|
|
active_metrics = group.metrics.count { |metric| metric.required_metrics.all?(&lookup.method(:has_key?)) }
|
2017-05-10 09:25:30 +00:00
|
|
|
|
2017-06-06 14:07:33 +00:00
|
|
|
groups[group][:active_metrics] += active_metrics
|
|
|
|
groups[group][:metrics_missing_requirements] += group.metrics.count - active_metrics
|
|
|
|
end
|
2017-05-10 09:25:30 +00:00
|
|
|
|
2017-06-06 14:07:33 +00:00
|
|
|
groups
|
|
|
|
end
|
2017-05-10 09:25:30 +00:00
|
|
|
|
2017-06-06 14:07:33 +00:00
|
|
|
def active_series_lookup(metric_groups)
|
|
|
|
timeframe_start = 8.hours.ago
|
|
|
|
timeframe_end = Time.now
|
2017-05-10 09:25:30 +00:00
|
|
|
|
2017-06-06 14:07:33 +00:00
|
|
|
series = metric_groups.flat_map(&:metrics).flat_map(&:required_metrics).uniq
|
2017-05-10 09:25:30 +00:00
|
|
|
|
2017-06-06 14:07:33 +00:00
|
|
|
lookup = series.each_slice(MAX_QUERY_ITEMS).flat_map do |batched_series|
|
2020-03-24 00:09:24 +00:00
|
|
|
client_series(*batched_series, start_time: timeframe_start, end_time: timeframe_end)
|
2017-08-24 17:16:31 +00:00
|
|
|
.select(&method(:has_matching_label?))
|
2017-06-06 14:07:33 +00:00
|
|
|
.map { |series_info| [series_info['__name__'], true] }
|
|
|
|
end
|
|
|
|
lookup.to_h
|
|
|
|
end
|
2017-05-10 09:25:30 +00:00
|
|
|
|
2017-08-24 17:16:31 +00:00
|
|
|
def has_matching_label?(series_info)
|
2017-06-06 14:07:33 +00:00
|
|
|
series_info.key?('environment')
|
|
|
|
end
|
2017-05-10 09:25:30 +00:00
|
|
|
|
2017-06-06 14:07:33 +00:00
|
|
|
def available_metrics
|
|
|
|
@available_metrics ||= client_label_values || []
|
|
|
|
end
|
2017-05-10 09:25:30 +00:00
|
|
|
|
2017-06-06 14:07:33 +00:00
|
|
|
def filter_active_metrics(metric_group)
|
|
|
|
metric_group.metrics.select! do |metric|
|
|
|
|
metric.required_metrics.all?(&available_metrics.method(:include?))
|
|
|
|
end
|
|
|
|
metric_group
|
|
|
|
end
|
2017-05-10 09:25:30 +00:00
|
|
|
|
2017-06-06 14:07:33 +00:00
|
|
|
def groups_with_active_metrics(metric_groups)
|
|
|
|
metric_groups.map(&method(:filter_active_metrics)).select { |group| group.metrics.any? }
|
|
|
|
end
|
2017-05-31 17:36:07 +00:00
|
|
|
|
2017-06-06 14:07:33 +00:00
|
|
|
def metrics_with_required_series(metric_groups)
|
|
|
|
metric_groups.flat_map do |group|
|
|
|
|
group.metrics.select do |metric|
|
|
|
|
metric.required_metrics.all?(&available_metrics.method(:include?))
|
|
|
|
end
|
|
|
|
end
|
2017-05-31 17:36:07 +00:00
|
|
|
end
|
|
|
|
end
|
2017-05-10 09:25:30 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|