BigW Consortium Gitlab

Commit 61d7b7f1 by Pawel Chojnacki

Finalize refactoring additional metrics query

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