diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2021-06-16 21:25:58 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2021-06-16 21:25:58 +0300 |
commit | a5f4bba440d7f9ea47046a0a561d49adf0a1e6d4 (patch) | |
tree | fb69158581673816a8cd895f9d352dcb3c678b1e /lib/generators | |
parent | d16b2e8639e99961de6ddc93909f3bb5c1445ba1 (diff) |
Add latest changes from gitlab-org/gitlab@14-0-stable-eev14.0.0-rc42
Diffstat (limited to 'lib/generators')
4 files changed, 101 insertions, 0 deletions
diff --git a/lib/generators/gitlab/usage_metric/USAGE b/lib/generators/gitlab/usage_metric/USAGE new file mode 100644 index 00000000000..3a2166c3bb1 --- /dev/null +++ b/lib/generators/gitlab/usage_metric/USAGE @@ -0,0 +1,9 @@ +Description: + Creates a stub instrumentation for a Service Ping metric + +Example: + rails generate gitlab:usage_metric CountIssues --type database + + This will create: + lib/gitlab/usage/metrics/instrumentations/count_issues_metric.rb + spec/lib/gitlab/usage/metrics/instrumentations/count_issues_metric_spec.rb diff --git a/lib/generators/gitlab/usage_metric/templates/instrumentation_class.rb.template b/lib/generators/gitlab/usage_metric/templates/instrumentation_class.rb.template new file mode 100644 index 00000000000..603b6f3bc8a --- /dev/null +++ b/lib/generators/gitlab/usage_metric/templates/instrumentation_class.rb.template @@ -0,0 +1,14 @@ +# frozen_string_literal: true + +module Gitlab + module Usage + module Metrics + module Instrumentations + class <%= class_name %>Metric < <%= metric_superclass %>Metric + def value + end + end + end + end + end +end diff --git a/lib/generators/gitlab/usage_metric/templates/instrumentation_class_spec.rb.template b/lib/generators/gitlab/usage_metric/templates/instrumentation_class_spec.rb.template new file mode 100644 index 00000000000..e984daee0a4 --- /dev/null +++ b/lib/generators/gitlab/usage_metric/templates/instrumentation_class_spec.rb.template @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Gitlab::Usage::Metrics::Instrumentations::<%= class_name %>Metric do + it_behaves_like 'a correct instrumented metric value', {}, 1 +end diff --git a/lib/generators/gitlab/usage_metric/usage_metric_generator.rb b/lib/generators/gitlab/usage_metric/usage_metric_generator.rb new file mode 100644 index 00000000000..f7125fdc911 --- /dev/null +++ b/lib/generators/gitlab/usage_metric/usage_metric_generator.rb @@ -0,0 +1,71 @@ +# frozen_string_literal: true + +require 'rails/generators' + +module Gitlab + class UsageMetricGenerator < Rails::Generators::Base + CE_DIR = 'lib/gitlab/usage/metrics/instrumentations' + EE_DIR = 'ee/lib/ee/gitlab/usage/metrics/instrumentations' + SPEC_CE_DIR = 'spec/lib/gitlab/usage/metrics/instrumentations' + SPEC_EE_DIR = 'ee/spec/lib/ee/gitlab/usage/metrics/instrumentations' + + ALLOWED_SUPERCLASSES = { + generic: 'Generic', + database: 'Database', + redis_hll: 'RedisHLL' + }.freeze + + source_root File.expand_path('templates', __dir__) + + class_option :ee, type: :boolean, optional: true, default: false, desc: 'Indicates if instrumentation is for EE' + class_option :type, type: :string, desc: "Metric type, must be one of: #{ALLOWED_SUPERCLASSES.keys.join(', ')}" + + argument :class_name, type: :string, desc: 'Instrumentation class name, e.g.: CountIssues' + + def create_class_files + validate! + + template "instrumentation_class.rb.template", file_path + template "instrumentation_class_spec.rb.template", spec_file_path + end + + private + + def validate! + raise ArgumentError, "Type is required, valid options are #{ALLOWED_SUPERCLASSES.keys.join(', ')}" unless type.present? + raise ArgumentError, "Unknown type '#{type}', valid options are #{ALLOWED_SUPERCLASSES.keys.join(', ')}" if metric_superclass.nil? + end + + def ee? + options[:ee] + end + + def type + options[:type] + end + + def file_path + dir = ee? ? EE_DIR : CE_DIR + + File.join(dir, file_name) + end + + def spec_file_path + dir = ee? ? SPEC_EE_DIR : SPEC_CE_DIR + + File.join(dir, spec_file_name) + end + + def file_name + "#{class_name.underscore}_metric.rb" + end + + def spec_file_name + "#{class_name.underscore}_metric_spec.rb" + end + + def metric_superclass + ALLOWED_SUPERCLASSES[type.to_sym] + end + end +end |