summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorRyan Cobb <rcobb@gitlab.com>2019-07-01 14:43:21 -0700
committerRyan Cobb <rcobb@gitlab.com>2019-07-09 17:02:08 -0600
commitab2a8e2ba839c92530838e2766ab6d8aa0228bda (patch)
treeac52ac44cdf1d8eba68538153bb5f54fb69e60bc /lib
parent9aad4174e052ba330fdaf4abc0276d8497c7de03 (diff)
downloadgitlab-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.
Diffstat (limited to 'lib')
-rw-r--r--lib/gitlab/importers/common_metrics.rb10
-rw-r--r--lib/gitlab/importers/common_metrics/importer.rb80
-rw-r--r--lib/gitlab/importers/common_metrics/prometheus_metric.rb12
-rw-r--r--lib/gitlab/importers/common_metrics/prometheus_metric_enums.rb40
4 files changed, 142 insertions, 0 deletions
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