Commit ee11fb16 authored by Alina Mihaila's avatar Alina Mihaila Committed by Pavel Shutsin

Add class_name option to metric definition generator

parent 4c1a5552
......@@ -181,6 +181,7 @@ product_group: group::product intelligence
value_type: string
status: active
milestone: 9.1
instrumentation_class: UuidMetric
introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/1521
time_frame: none
data_source: database
......@@ -199,22 +200,23 @@ The GitLab codebase provides a dedicated [generator](https://gitlab.com/gitlab-o
For uniqueness, the generated files include a timestamp prefix in ISO 8601 format.
The generator takes a list of key paths and 2 options as arguments. It creates metric YAML definitions in the corresponding location:
The generator takes a list of key paths and 3 options as arguments. It creates metric YAML definitions in the corresponding location:
- `--ee`, `--no-ee` Indicates if metric is for EE.
- `--dir=DIR` Indicates the metric directory. It must be one of: `counts_7d`, `7d`, `counts_28d`, `28d`, `counts_all`, `all`, `settings`, `license`.
- `--class_name=CLASS_NAME` Indicates the instrumentation class. For example `UsersCreatingIssuesMetric`, `UuidMetric`
**Single metric example**
```shell
bundle exec rails generate gitlab:usage_metric_definition counts.issues --dir=7d
bundle exec rails generate gitlab:usage_metric_definition counts.issues --dir=7d --class_name=CountIssues
create config/metrics/counts_7d/issues.yml
```
**Multiple metrics example**
```shell
bundle exec rails generate gitlab:usage_metric_definition counts.issues counts.users --dir=7d
bundle exec rails generate gitlab:usage_metric_definition counts.issues counts.users --dir=7d --class_name=CountUsersCreatingIssues
create config/metrics/counts_7d/issues.yml
create config/metrics/counts_7d/users.yml
```
......@@ -223,7 +225,7 @@ NOTE:
To create a metric definition used in EE, add the `--ee` flag.
```shell
bundle exec rails generate gitlab:usage_metric_definition counts.issues --ee --dir=7d
bundle exec rails generate gitlab:usage_metric_definition counts.issues --ee --dir=7d --class_name=CountUsersCreatingIssues
create ee/config/metrics/counts_7d/issues.yml
```
......@@ -236,9 +238,9 @@ A YAML metric definition is required for each metric. A dedicated generator is p
The generator takes `category` and `event` arguments, as the root key is `redis_hll_counters`, and creates two metric definitions for weekly and monthly time frames:
```shell
bundle exec rails generate gitlab:usage_metric_definition:redis_hll issues i_closed
create config/metrics/counts_7d/i_closed_weekly.yml
create config/metrics/counts_28d/i_closed_monthly.yml
bundle exec rails generate gitlab:usage_metric_definition:redis_hll issues count_users_closing_issues
create config/metrics/counts_7d/count_users_closing_issues_weekly.yml
create config/metrics/counts_28d/count_users_closing_issues_monthly.yml
```
To create a metric definition used in EE, add the `--ee` flag.
......
......@@ -12,6 +12,7 @@ introduced_by_url:
time_frame: <%= time_frame %>
data_source:
data_category: optional
instrumentation_class: <%= class_name %>
performance_indicator_type:
distribution:
<%= distribution %>
......
......@@ -12,11 +12,11 @@ module Gitlab
class_option :ee, type: :boolean, optional: true, default: false, desc: 'Indicates if metric is for ee'
def create_metrics
weekly_params = ["#{key_path}_weekly", '--dir', '7d']
weekly_params = ["#{key_path}_weekly", '--dir', '7d', '--class_name', 'RedisHLLMetric']
weekly_params << '--ee' if ee?
Gitlab::UsageMetricDefinitionGenerator.start(weekly_params)
monthly_params = ["#{key_path}_monthly", '--dir', '28d']
monthly_params = ["#{key_path}_monthly", '--dir', '28d', '--class_name', 'RedisHLLMetric']
monthly_params << '--ee' if ee?
Gitlab::UsageMetricDefinitionGenerator.start(monthly_params)
end
......
......@@ -35,6 +35,7 @@ module Gitlab
class_option :ee, type: :boolean, optional: true, default: false, desc: 'Indicates if metric is for ee'
class_option :dir,
type: :string, desc: "Indicates the metric location. It must be one of: #{VALID_INPUT_DIRS.join(', ')}"
class_option :class_name, type: :string, optional: true, desc: 'Instrumentation class name, e.g.: CountIssues'
argument :key_paths, type: :array, desc: 'Unique JSON key paths for the metrics'
......@@ -66,6 +67,10 @@ module Gitlab
Gitlab::VERSION.match('(\d+\.\d+)').captures.first
end
def class_name
options[:class_name]
end
private
def metric_name_suggestion(key_path)
......
......@@ -13,6 +13,7 @@ introduced_by_url:
time_frame: 7d
data_source:
data_category: operational
instrumentation_class: Count
performance_indicator_type:
distribution:
- ce
......
......@@ -13,6 +13,7 @@ introduced_by_url:
time_frame: 7d
data_source:
data_category: optional
instrumentation_class: Count
performance_indicator_type:
distribution:
- ee
......
......@@ -14,6 +14,7 @@ introduced_by_url:
time_frame: 7d
data_source:
data_category: optional
instrumentation_class: Count
performance_indicator_type:
distribution:
- ce
......
......@@ -28,8 +28,14 @@ RSpec.describe Gitlab::UsageMetricDefinition::RedisHllGenerator, :silence_stdout
weekly_metric_definition_path = Dir.glob(File.join(temp_dir, 'metrics/counts_7d/*i_test_event_weekly.yml')).first
monthly_metric_definition_path = Dir.glob(File.join(temp_dir, 'metrics/counts_28d/*i_test_event_monthly.yml')).first
expect(YAML.safe_load(File.read(weekly_metric_definition_path))).to include("key_path" => "redis_hll_counters.test_category.i_test_event_weekly")
expect(YAML.safe_load(File.read(monthly_metric_definition_path))).to include("key_path" => "redis_hll_counters.test_category.i_test_event_monthly")
weekly_metric_definition = YAML.safe_load(File.read(weekly_metric_definition_path))
monthly_metric_definition = YAML.safe_load(File.read(monthly_metric_definition_path))
expect(weekly_metric_definition).to include("key_path" => "redis_hll_counters.test_category.i_test_event_weekly")
expect(monthly_metric_definition).to include("key_path" => "redis_hll_counters.test_category.i_test_event_monthly")
expect(weekly_metric_definition["instrumentation_class"]).to eq('RedisHLLMetric')
expect(monthly_metric_definition["instrumentation_class"]).to eq('RedisHLLMetric')
end
context 'with ee option' do
......@@ -49,9 +55,11 @@ RSpec.describe Gitlab::UsageMetricDefinition::RedisHllGenerator, :silence_stdout
expect(weekly_metric_definition).to include("key_path" => "redis_hll_counters.test_category.i_test_event_weekly")
expect(weekly_metric_definition["distribution"]).to include('ee')
expect(weekly_metric_definition["instrumentation_class"]).to eq('RedisHLLMetric')
expect(monthly_metric_definition).to include("key_path" => "redis_hll_counters.test_category.i_test_event_monthly")
expect(monthly_metric_definition["distribution"]).to include('ee')
expect(monthly_metric_definition["instrumentation_class"]).to eq('RedisHLLMetric')
end
end
end
......@@ -7,6 +7,7 @@ RSpec.describe Gitlab::UsageMetricDefinitionGenerator, :silence_stdout do
let(:key_path) { 'counts_weekly.test_metric' }
let(:dir) { '7d' }
let(:class_name) { 'Count' }
let(:temp_dir) { Dir.mktmpdir }
before do
......@@ -33,7 +34,7 @@ RSpec.describe Gitlab::UsageMetricDefinitionGenerator, :silence_stdout do
let(:metric_definition_path) { Dir.glob(File.join(temp_dir, 'metrics/counts_7d/*_test_metric.yml')).first }
it 'creates a metric definition file using the template' do
described_class.new([key_path], { 'dir' => dir }).invoke_all
described_class.new([key_path], { 'dir' => dir, 'class_name' => class_name }).invoke_all
expect(YAML.safe_load(File.read(metric_definition_path))).to eq(sample_metric)
end
end
......@@ -48,14 +49,14 @@ RSpec.describe Gitlab::UsageMetricDefinitionGenerator, :silence_stdout do
end
it 'creates a metric definition file using the template' do
described_class.new([key_path], { 'dir' => dir, 'ee': true }).invoke_all
described_class.new([key_path], { 'dir' => dir, 'class_name' => class_name, 'ee': true }).invoke_all
expect(YAML.safe_load(File.read(metric_definition_path))).to eq(sample_metric)
end
end
end
describe 'Validation' do
let(:options) { [key_path, '--dir', dir] }
let(:options) { [key_path, '--dir', dir, '--class_name', class_name] }
subject { described_class.start(options) }
......@@ -93,7 +94,7 @@ RSpec.describe Gitlab::UsageMetricDefinitionGenerator, :silence_stdout do
describe 'Name suggestions' do
it 'adds name key to metric definition' do
expect(::Gitlab::Usage::Metrics::NamesSuggestions::Generator).to receive(:generate).and_return('some name')
described_class.new([key_path], { 'dir' => dir }).invoke_all
described_class.new([key_path], { 'dir' => dir, 'class_name' => class_name }).invoke_all
metric_definition_path = Dir.glob(File.join(temp_dir, 'metrics/counts_7d/*_test_metric.yml')).first
expect(YAML.safe_load(File.read(metric_definition_path))).to include("name" => "some name")
......@@ -104,7 +105,7 @@ RSpec.describe Gitlab::UsageMetricDefinitionGenerator, :silence_stdout do
let(:key_paths) { ['counts_weekly.test_metric', 'counts_weekly.test1_metric'] }
it 'creates multiple files' do
described_class.new(key_paths, { 'dir' => dir }).invoke_all
described_class.new(key_paths, { 'dir' => dir, 'class_name' => class_name }).invoke_all
files = Dir.glob(File.join(temp_dir, 'metrics/counts_7d/*_metric.yml'))
expect(files.count).to eq(2)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment