1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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
|