diff options
author | Ryan Cobb <rcobb@gitlab.com> | 2019-07-01 14:43:21 -0700 |
---|---|---|
committer | Ryan Cobb <rcobb@gitlab.com> | 2019-07-09 17:02:08 -0600 |
commit | ab2a8e2ba839c92530838e2766ab6d8aa0228bda (patch) | |
tree | ac52ac44cdf1d8eba68538153bb5f54fb69e60bc | |
parent | 9aad4174e052ba330fdaf4abc0276d8497c7de03 (diff) | |
download | gitlab-ce-ab2a8e2ba839c92530838e2766ab6d8aa0228bda.tar.gz |
Move importers to lib folder
This moves our metric importers into the lib folder. This will allow
them to be autoloaded instead of having to explicitly require them.
-rw-r--r-- | db/importers/common_metrics.rb | 17 | ||||
-rw-r--r-- | db/importers/common_metrics/importer.rb | 76 | ||||
-rw-r--r-- | db/importers/common_metrics/prometheus_metric.rb | 8 | ||||
-rw-r--r-- | db/importers/common_metrics/prometheus_metric_enums.rb | 36 | ||||
-rw-r--r-- | db/importers/common_metrics_importer.rb | 10 | ||||
-rw-r--r-- | lib/gitlab/importers/common_metrics.rb | 10 | ||||
-rw-r--r-- | lib/gitlab/importers/common_metrics/importer.rb | 80 | ||||
-rw-r--r-- | lib/gitlab/importers/common_metrics/prometheus_metric.rb | 12 | ||||
-rw-r--r-- | lib/gitlab/importers/common_metrics/prometheus_metric_enums.rb | 40 | ||||
-rw-r--r-- | spec/db/importers/common_metrics_importer_spec.rb | 6 |
10 files changed, 153 insertions, 142 deletions
diff --git a/db/importers/common_metrics.rb b/db/importers/common_metrics.rb deleted file mode 100644 index 411d366ef83..00000000000 --- a/db/importers/common_metrics.rb +++ /dev/null @@ -1,17 +0,0 @@ -require_relative './common_metrics/importer' -require_relative './common_metrics/prometheus_metric' -require_relative './common_metrics/prometheus_metric_enums' - -require Rails.root.join('ee', 'db', 'importers', 'common_metrics') if Gitlab.ee? - -module Importers - module CommonMetrics - end - - # Patch to preserve old CommonMetricsImporter api - module CommonMetricsImporter - def self.new(*args) - Importers::CommonMetrics::Importer.new(*args) - end - end -end diff --git a/db/importers/common_metrics/importer.rb b/db/importers/common_metrics/importer.rb deleted file mode 100644 index 24149fbbea6..00000000000 --- a/db/importers/common_metrics/importer.rb +++ /dev/null @@ -1,76 +0,0 @@ -# frozen_string_literal: true - -module Importers - module CommonMetrics - class Importer - MissingQueryId = Class.new(StandardError) - - attr_reader :content - - def initialize(filename = 'common_metrics.yml') - @content = YAML.load_file(Rails.root.join('config', 'prometheus', filename)) - end - - def execute - PrometheusMetric.reset_column_information - - process_content do |id, attributes| - find_or_build_metric!(id) - .update!(**attributes) - end - end - - private - - def process_content(&blk) - content['panel_groups'].map do |group| - process_group(group, &blk) - end - end - - def process_group(group, &blk) - attributes = { - group: find_group_title_key(group['group']) - } - - group['panels'].map do |panel| - process_panel(panel, attributes, &blk) - end - end - - def process_panel(panel, attributes, &blk) - attributes = attributes.merge( - title: panel['title'], - y_label: panel['y_label']) - - panel['metrics'].map do |metric_details| - process_metric_details(metric_details, attributes, &blk) - end - end - - def process_metric_details(metric_details, attributes, &blk) - attributes = attributes.merge( - legend: metric_details['label'], - query: metric_details['query_range'], - unit: metric_details['unit']) - - yield(metric_details['id'], attributes) - end - - def find_or_build_metric!(id) - raise MissingQueryId unless id - - PrometheusMetric.common.find_by(identifier: id) || - PrometheusMetric.new(common: true, identifier: id) - end - - def find_group_title_key(title) - PrometheusMetricEnums.groups[find_group_title(title)] - end - - def find_group_title(title) - PrometheusMetricEnums.group_titles.invert[title] - end - end - end -end diff --git a/db/importers/common_metrics/prometheus_metric.rb b/db/importers/common_metrics/prometheus_metric.rb deleted file mode 100644 index 9149549c750..00000000000 --- a/db/importers/common_metrics/prometheus_metric.rb +++ /dev/null @@ -1,8 +0,0 @@ -module Importers - module CommonMetrics - class PrometheusMetric < ActiveRecord::Base - enum group: PrometheusMetricEnums.groups - scope :common, -> { where(common: true) } - end - end -end diff --git a/db/importers/common_metrics/prometheus_metric_enums.rb b/db/importers/common_metrics/prometheus_metric_enums.rb deleted file mode 100644 index 50a1081a629..00000000000 --- a/db/importers/common_metrics/prometheus_metric_enums.rb +++ /dev/null @@ -1,36 +0,0 @@ -module Importers - module CommonMetrics - module PrometheusMetricEnums - def self.groups - { - # built-in groups - nginx_ingress_vts: -1, - ha_proxy: -2, - aws_elb: -3, - nginx: -4, - kubernetes: -5, - nginx_ingress: -6, - - # custom groups - business: 0, - response: 1, - system: 2 - } - end - - def self.group_titles - { - business: _('Business metrics (Custom)'), - response: _('Response metrics (Custom)'), - system: _('System metrics (Custom)'), - nginx_ingress_vts: _('Response metrics (NGINX Ingress VTS)'), - nginx_ingress: _('Response metrics (NGINX Ingress)'), - ha_proxy: _('Response metrics (HA Proxy)'), - aws_elb: _('Response metrics (AWS ELB)'), - nginx: _('Response metrics (NGINX)'), - kubernetes: _('System metrics (Kubernetes)') - } - end - end - end -end diff --git a/db/importers/common_metrics_importer.rb b/db/importers/common_metrics_importer.rb index cf5f5e181de..a2e3996a123 100644 --- a/db/importers/common_metrics_importer.rb +++ b/db/importers/common_metrics_importer.rb @@ -1,3 +1,9 @@ -# This functionality has been moved to the common_metrics module. +# This functionality has been moved to the lib/gitlab/importers/common_metrics module. # This is here only to preserve existing ::Importers::CommonMetricsImporter api -require_relative './common_metrics' +module Importers + module CommonMetricsImporter + def self.new(*args) + Gitlab::Importers::CommonMetrics::Importer.new(*args) + end + end +end diff --git a/lib/gitlab/importers/common_metrics.rb b/lib/gitlab/importers/common_metrics.rb new file mode 100644 index 00000000000..210ae6cb14a --- /dev/null +++ b/lib/gitlab/importers/common_metrics.rb @@ -0,0 +1,10 @@ +# frozen_string_literal: true + +module Gitlab + module Importers + module CommonMetrics + end + end +end + +Gitlab::Importers::CommonMetrics.prepend(EE::Gitlab::Importers::CommonMetrics) diff --git a/lib/gitlab/importers/common_metrics/importer.rb b/lib/gitlab/importers/common_metrics/importer.rb new file mode 100644 index 00000000000..c916af58820 --- /dev/null +++ b/lib/gitlab/importers/common_metrics/importer.rb @@ -0,0 +1,80 @@ +# frozen_string_literal: true + +module Gitlab + module Importers + module CommonMetrics + class Importer + MissingQueryId = Class.new(StandardError) + + attr_reader :content + + def initialize(filename = 'common_metrics.yml') + @content = YAML.load_file(Rails.root.join('config', 'prometheus', filename)) + end + + def execute + PrometheusMetric.reset_column_information + + process_content do |id, attributes| + find_or_build_metric!(id) + .update!(**attributes) + end + end + + private + + def process_content(&blk) + content['panel_groups'].map do |group| + process_group(group, &blk) + end + end + + def process_group(group, &blk) + attributes = { + group: find_group_title_key(group['group']) + } + + group['panels'].map do |panel| + process_panel(panel, attributes, &blk) + end + end + + def process_panel(panel, attributes, &blk) + attributes = attributes.merge( + title: panel['title'], + y_label: panel['y_label']) + + panel['metrics'].map do |metric_details| + process_metric_details(metric_details, attributes, &blk) + end + end + + def process_metric_details(metric_details, attributes, &blk) + attributes = attributes.merge( + legend: metric_details['label'], + query: metric_details['query_range'], + unit: metric_details['unit']) + + yield(metric_details['id'], attributes) + end + + # rubocop: disable CodeReuse/ActiveRecord + def find_or_build_metric!(id) + raise MissingQueryId unless id + + PrometheusMetric.common.find_by(identifier: id) || + PrometheusMetric.new(common: true, identifier: id) + end + # rubocop: enable CodeReuse/ActiveRecord + + def find_group_title_key(title) + PrometheusMetricEnums.groups[find_group_title(title)] + end + + def find_group_title(title) + PrometheusMetricEnums.group_titles.invert[title] + end + end + end + end +end diff --git a/lib/gitlab/importers/common_metrics/prometheus_metric.rb b/lib/gitlab/importers/common_metrics/prometheus_metric.rb new file mode 100644 index 00000000000..a9397f4c055 --- /dev/null +++ b/lib/gitlab/importers/common_metrics/prometheus_metric.rb @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +module Gitlab + module Importers + module CommonMetrics + class PrometheusMetric < ApplicationRecord + enum group: PrometheusMetricEnums.groups + scope :common, -> { where(common: true) } + end + end + end +end diff --git a/lib/gitlab/importers/common_metrics/prometheus_metric_enums.rb b/lib/gitlab/importers/common_metrics/prometheus_metric_enums.rb new file mode 100644 index 00000000000..dbf4c9e9b55 --- /dev/null +++ b/lib/gitlab/importers/common_metrics/prometheus_metric_enums.rb @@ -0,0 +1,40 @@ +# frozen_string_literal: true + +module Gitlab + module Importers + module CommonMetrics + module PrometheusMetricEnums + def self.groups + { + # built-in groups + nginx_ingress_vts: -1, + ha_proxy: -2, + aws_elb: -3, + nginx: -4, + kubernetes: -5, + nginx_ingress: -6, + + # custom groups + business: 0, + response: 1, + system: 2 + } + end + + def self.group_titles + { + business: _('Business metrics (Custom)'), + response: _('Response metrics (Custom)'), + system: _('System metrics (Custom)'), + nginx_ingress_vts: _('Response metrics (NGINX Ingress VTS)'), + nginx_ingress: _('Response metrics (NGINX Ingress)'), + ha_proxy: _('Response metrics (HA Proxy)'), + aws_elb: _('Response metrics (AWS ELB)'), + nginx: _('Response metrics (NGINX)'), + kubernetes: _('System metrics (Kubernetes)') + } + end + end + end + end +end diff --git a/spec/db/importers/common_metrics_importer_spec.rb b/spec/db/importers/common_metrics_importer_spec.rb index c47effa6803..02e7d51c268 100644 --- a/spec/db/importers/common_metrics_importer_spec.rb +++ b/spec/db/importers/common_metrics_importer_spec.rb @@ -3,7 +3,7 @@ require 'rails_helper' require Rails.root.join("db", "importers", "common_metrics_importer.rb") -describe Importers::CommonMetrics::PrometheusMetric do +describe Gitlab::Importers::CommonMetrics::PrometheusMetric do let(:existing_group_titles) do ::PrometheusMetricEnums.group_details.each_with_object({}) do |(key, value), memo| memo[key] = value[:group_title] @@ -15,7 +15,7 @@ describe Importers::CommonMetrics::PrometheusMetric do end it 'GROUP_TITLES equals ::PrometheusMetric' do - expect(Importers::CommonMetrics::PrometheusMetricEnums.group_titles).to eq(existing_group_titles) + expect(Gitlab::Importers::CommonMetrics::PrometheusMetricEnums.group_titles).to eq(existing_group_titles) end end @@ -104,7 +104,7 @@ describe Importers::CommonMetricsImporter do let(:query_identifier) { } it 'raises exception' do - expect { subject.execute }.to raise_error(Importers::CommonMetrics::Importer::MissingQueryId) + expect { subject.execute }.to raise_error(Gitlab::Importers::CommonMetrics::Importer::MissingQueryId) end end |