diff options
author | Pawel Chojnacki <pawel@chojnacki.ws> | 2017-05-10 12:25:30 +0300 |
---|---|---|
committer | Pawel Chojnacki <pawel@chojnacki.ws> | 2017-05-25 16:05:56 +0300 |
commit | 2061414054ce43aa6d53d1be3f602114e5a336d2 (patch) | |
tree | c32e83fcd76cd279171117f3ff282ef54f5308a2 /lib/gitlab/prometheus/queries | |
parent | 78de1c059ac588df4ba1ef352b28e5b1c6102804 (diff) |
Additional metrics initial work, with working metrics listing, but without actoual metrics mesurements
Diffstat (limited to 'lib/gitlab/prometheus/queries')
-rw-r--r-- | lib/gitlab/prometheus/queries/additional_metrics_query.rb | 70 | ||||
-rw-r--r-- | lib/gitlab/prometheus/queries/base_query.rb | 2 | ||||
-rw-r--r-- | lib/gitlab/prometheus/queries/matched_metrics_query.rb | 72 |
3 files changed, 143 insertions, 1 deletions
diff --git a/lib/gitlab/prometheus/queries/additional_metrics_query.rb b/lib/gitlab/prometheus/queries/additional_metrics_query.rb new file mode 100644 index 00000000000..001701383c3 --- /dev/null +++ b/lib/gitlab/prometheus/queries/additional_metrics_query.rb @@ -0,0 +1,70 @@ +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 + + 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 + end + metric + end + group + end + 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) + else + client_query(query[:query] % context, time: timeframe_end) + end + contains_metrics = result.all? do |item| + item&.[](:values)&.any? || item&.[](:value)&.any? + end + end + + def process_result(query_result) + contains_metrics = query_result.all? do |item| + item&.[](:values)&.any? || item&.[](:value)&.any? + end + + contains_metrics + end + + def matched_metrics + label_values = client_label_values || [] + + result = Gitlab::Prometheus::MetricsSources.additional_metrics.map do |group| + group[:metrics].map!(&:symbolize_keys) + 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?} + end + end +end diff --git a/lib/gitlab/prometheus/queries/base_query.rb b/lib/gitlab/prometheus/queries/base_query.rb index 2a2eb4ae57f..c60828165bd 100644 --- a/lib/gitlab/prometheus/queries/base_query.rb +++ b/lib/gitlab/prometheus/queries/base_query.rb @@ -3,7 +3,7 @@ module Gitlab module Queries class BaseQuery attr_accessor :client - delegate :query_range, :query, to: :client, prefix: true + delegate :query_range, :query, :label_values, :series, to: :client, prefix: true def raw_memory_usage_query(environment_slug) %{avg(container_memory_usage_bytes{container_name!="POD",environment="#{environment_slug}"}) / 2^20} diff --git a/lib/gitlab/prometheus/queries/matched_metrics_query.rb b/lib/gitlab/prometheus/queries/matched_metrics_query.rb new file mode 100644 index 00000000000..61926320e40 --- /dev/null +++ b/lib/gitlab/prometheus/queries/matched_metrics_query.rb @@ -0,0 +1,72 @@ +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| + { + group: group.name, + priority: group.priority, + active_metrics: data[:active_metrics], + metrics_missing_requirements: data[:metrics_missing_requirements] + } + end + end + + def groups_data + metrics_series = metrics_with_series(Gitlab::Prometheus::MetricGroup.all) + lookup = active_series_lookup(metrics_series) + + groups = {} + + metrics_series.each do |metrics, series| + groups[metrics.group] ||= { active_metrics: 0, metrics_missing_requirements: 0 } + group = groups[metrics.group] + + if series.all?(&lookup.method(:has_key?)) + group[:active_metrics] += 1 + else + group[:metrics_missing_requirements] += 1 + end + group + end + + groups + end + + def active_series_lookup(metrics) + timeframe_start = 8.hours.ago + timeframe_end = Time.now + + series = metrics.flat_map { |metrics, series| series }.uniq + + lookup = series.each_slice(MAX_QUERY_ITEMS).flat_map do |batched_series| + client_series(*batched_series, start: timeframe_start, stop: timeframe_end) + .select(&method(:has_matching_label)) + .map { |series_info| [series_info['__name__'], true] } + end + lookup.to_h + end + + def has_matching_label(series_info) + series_info.has_key?('environment') + end + + def metrics_with_series(metric_groups) + label_values = client_label_values || [] + + metrics = metric_groups.flat_map do |group| + group.metrics.map do |metric| + matcher = Regexp.compile(metric.detect) + [metric, label_values.select(&matcher.method(:match))] + end + end + + metrics.select { |metric, labels| labels&.any? } + end + end +end |