Commit 61d7b7f1 authored by Pawel Chojnacki's avatar Pawel Chojnacki

Finalize refactoring additional metrics query

parent 20614140
......@@ -158,7 +158,9 @@ class Environment < ActiveRecord::Base
end
def additional_metrics
project.monitoring_service.reactive_query(Gitlab::Prometheus::Queries::AdditionalMetricsQuery, self.id) if has_additional_metrics?
if has_additional_metrics?
project.monitoring_service.reactive_query(Gitlab::Prometheus::Queries::AdditionalMetricsQuery.name, self.id, &:itself)
end
end
# An environment name is not necessarily suitable for use in URLs, DNS
......
......@@ -73,7 +73,7 @@ class PrometheusService < MonitoringService
end
def reactive_query(query_class, *args, &block)
calculate_reactive_cache(query_class, *args, &block)
with_reactive_cache(query_class, *args, &block)
end
# Cache metrics for specific environment
......
......@@ -13,6 +13,10 @@ module Gitlab::Prometheus
load_groups_from_yaml
end
def self.load_groups_from_yaml
additional_metrics_raw.map(&method(:group_from_entry))
end
def self.group_from_entry(entry)
missing_fields = [:group, :priority, :metrics].select { |key| !entry.has_key?(key) }
raise ParsingError.new("entry missing required fields #{missing_fields}") unless missing_fields.empty?
......@@ -22,10 +26,6 @@ module Gitlab::Prometheus
group
end
def self.load_groups_from_yaml
additional_metrics_raw.map(&method(:group_from_entry))
end
def self.additional_metrics_raw
@additional_metrics_raw ||= YAML.load_file(Rails.root.join('config/additional_metrics.yml'))&.map(&:deep_symbolize_keys).freeze
end
......
module Gitlab::Prometheus::Queries
class AdditionalMetricsQuery < BaseQuery
def self.metrics
@metrics ||= YAML.load_file(Rails.root.join('config/custom_metrics.yml')).freeze
end
def query(environment_id)
environment = Environment.find_by(id: environment_id)
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_processor = method(:process_query).curry[query_context(environment_id)]
matched_metrics.map do |group|
group[:metrics].map! do |metric|
metric[:queries].map! do |query|
query = query.symbolize_keys
query[:result] =
if query.has_key?(:query_range)
client_query_range(query[:query_range] % context, start: timeframe_start, stop: timeframe_end)
else
client_query(query[:query] % context, time: timeframe_end)
end
query
metrics = group.metrics.map do |metric|
{
title: metric.title,
weight: metric.weight,
queries: metric.queries.map(&query_processor)
}
end
metric
{
group: group.name,
priority: group.priority,
metrics: metrics
}
end
group
end
private
def query_context(environment_id)
environment = Environment.find_by(id: environment_id)
{
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
}
end
def process_query(group, query)
result = if query.has_key?(:query_range)
client_query_range(query[:query_range] % context, start: timeframe_start, stop: timeframe_end)
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: timeframe_end)
end
contains_metrics = result.all? do |item|
item&.[](:values)&.any? || item&.[](:value)&.any?
client_query(query[:query] % context, time: context[:timeframe_end])
end
query_with_result
end
def process_result(query_result)
......@@ -54,17 +53,17 @@ module Gitlab::Prometheus::Queries
def matched_metrics
label_values = client_label_values || []
Gitlab::Prometheus::MetricGroup.all
result = Gitlab::Prometheus::MetricsSources.additional_metrics.map do |group|
group[:metrics].map!(&:symbolize_keys)
group[:metrics].select! do |metric|
matcher = Regexp.compile(metric[:detect])
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].empty?}
result.select { |group| group.metrics.any? }
end
end
end
......@@ -2,10 +2,6 @@ module Gitlab::Prometheus::Queries
class MatchedMetricsQuery < BaseQuery
MAX_QUERY_ITEMS = 40.freeze
def self.metrics
@metrics ||= YAML.load_file(Rails.root.join('config/additional_metrics.yml')).map(&:deep_symbolize_keys)
end
def query
groups_data.map do |group, data|
{
......@@ -17,6 +13,8 @@ module Gitlab::Prometheus::Queries
end
end
private
def groups_data
metrics_series = metrics_with_series(Gitlab::Prometheus::MetricGroup.all)
lookup = active_series_lookup(metrics_series)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment