2021-03-08 18:12:59 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
RSpec.describe Gitlab::Usage::MetricDefinition do
|
|
|
|
let(:attributes) do
|
|
|
|
{
|
|
|
|
description: 'GitLab instance unique identifier',
|
|
|
|
value_type: 'string',
|
|
|
|
product_category: 'collection',
|
2021-03-11 19:13:27 +05:30
|
|
|
product_stage: 'growth',
|
2021-12-11 22:18:48 +05:30
|
|
|
product_section: 'devops',
|
2021-11-11 11:23:49 +05:30
|
|
|
status: 'active',
|
|
|
|
milestone: '14.1',
|
2021-03-08 18:12:59 +05:30
|
|
|
default_generation: 'generation_1',
|
2021-03-11 19:13:27 +05:30
|
|
|
key_path: 'uuid',
|
|
|
|
product_group: 'group::product analytics',
|
2021-03-08 18:12:59 +05:30
|
|
|
time_frame: 'none',
|
|
|
|
data_source: 'database',
|
|
|
|
distribution: %w(ee ce),
|
2021-04-29 21:17:54 +05:30
|
|
|
tier: %w(free starter premium ultimate bronze silver gold),
|
2021-09-30 23:02:18 +05:30
|
|
|
name: 'uuid',
|
2022-07-16 23:28:13 +05:30
|
|
|
data_category: 'standard',
|
|
|
|
removed_by_url: 'http://gdk.test'
|
2021-03-08 18:12:59 +05:30
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:path) { File.join('metrics', 'uuid.yml') }
|
|
|
|
let(:definition) { described_class.new(path, attributes) }
|
|
|
|
let(:yaml_content) { attributes.deep_stringify_keys.to_yaml }
|
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
around do |example|
|
|
|
|
described_class.instance_variable_set(:@definitions, nil)
|
|
|
|
example.run
|
|
|
|
described_class.instance_variable_set(:@definitions, nil)
|
|
|
|
end
|
|
|
|
|
2021-04-29 21:17:54 +05:30
|
|
|
def write_metric(metric, path, content)
|
|
|
|
path = File.join(metric, path)
|
|
|
|
dir = File.dirname(path)
|
|
|
|
FileUtils.mkdir_p(dir)
|
|
|
|
File.write(path, content)
|
|
|
|
end
|
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
after do
|
|
|
|
# Reset memoized `definitions` result
|
|
|
|
described_class.instance_variable_set(:@definitions, nil)
|
|
|
|
end
|
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
it 'has all definitons valid' do
|
2021-09-04 01:27:46 +05:30
|
|
|
expect { described_class.definitions }.not_to raise_error
|
2021-03-08 18:12:59 +05:30
|
|
|
end
|
|
|
|
|
2022-05-07 20:08:51 +05:30
|
|
|
describe 'not_removed' do
|
|
|
|
let(:all_definitions) do
|
|
|
|
metrics_definitions = [
|
|
|
|
{ key_path: 'metric1', instrumentation_class: 'RedisHLLMetric', status: 'active' },
|
|
|
|
{ key_path: 'metric2', instrumentation_class: 'RedisHLLMetric', status: 'broken' },
|
|
|
|
{ key_path: 'metric3', instrumentation_class: 'RedisHLLMetric', status: 'active' },
|
|
|
|
{ key_path: 'metric4', instrumentation_class: 'RedisHLLMetric', status: 'removed' }
|
|
|
|
]
|
|
|
|
metrics_definitions.map { |definition| described_class.new(definition[:key_path], definition.symbolize_keys) }
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(described_class).to receive(:all).and_return(all_definitions)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'includes metrics that are not removed' do
|
|
|
|
expect(described_class.not_removed.count).to eq(3)
|
|
|
|
|
|
|
|
expect(described_class.not_removed.keys).to match_array(%w(metric1 metric2 metric3))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-11-18 22:05:49 +05:30
|
|
|
describe '#with_instrumentation_class' do
|
|
|
|
let(:metric_status) { 'active' }
|
|
|
|
let(:all_definitions) do
|
|
|
|
metrics_definitions = [
|
|
|
|
{ key_path: 'metric1', instrumentation_class: 'RedisHLLMetric', status: 'data_available' },
|
|
|
|
{ key_path: 'metric2', instrumentation_class: 'RedisHLLMetric', status: 'implemented' },
|
|
|
|
{ key_path: 'metric3', instrumentation_class: 'RedisHLLMetric', status: 'deprecated' },
|
|
|
|
{ key_path: 'metric4', instrumentation_class: 'RedisHLLMetric', status: metric_status },
|
|
|
|
{ key_path: 'metric5', status: 'active' },
|
|
|
|
{ key_path: 'metric_missing_status' }
|
|
|
|
]
|
|
|
|
metrics_definitions.map { |definition| described_class.new(definition[:key_path], definition.symbolize_keys) }
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(described_class).to receive(:all).and_return(all_definitions)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'includes definitions with instrumentation_class' do
|
|
|
|
expect(described_class.with_instrumentation_class.count).to eq(4)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with removed metric' do
|
|
|
|
let(:metric_status) { 'removed' }
|
|
|
|
|
|
|
|
it 'excludes removed definitions' do
|
|
|
|
expect(described_class.with_instrumentation_class.count).to eq(3)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
describe '#key' do
|
|
|
|
subject { definition.key }
|
|
|
|
|
|
|
|
it 'returns a symbol from name' do
|
|
|
|
is_expected.to eq('uuid')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#validate' do
|
|
|
|
using RSpec::Parameterized::TableSyntax
|
|
|
|
|
|
|
|
where(:attribute, :value) do
|
|
|
|
:description | nil
|
|
|
|
:value_type | nil
|
|
|
|
:value_type | 'test'
|
|
|
|
:status | nil
|
2021-11-11 11:23:49 +05:30
|
|
|
:milestone | nil
|
2021-09-30 23:02:18 +05:30
|
|
|
:data_category | nil
|
2021-03-11 19:13:27 +05:30
|
|
|
:key_path | nil
|
|
|
|
:product_group | nil
|
2021-03-08 18:12:59 +05:30
|
|
|
:time_frame | nil
|
|
|
|
:time_frame | '29d'
|
|
|
|
:data_source | 'other'
|
|
|
|
:data_source | nil
|
|
|
|
:distribution | nil
|
|
|
|
:distribution | 'test'
|
|
|
|
:tier | %w(test ee)
|
2021-04-29 21:17:54 +05:30
|
|
|
:name | 'count_<adjective_describing>_boards'
|
2021-09-04 01:27:46 +05:30
|
|
|
:repair_issue_url | nil
|
2022-07-16 23:28:13 +05:30
|
|
|
:removed_by_url | 1
|
2021-06-08 01:23:25 +05:30
|
|
|
|
|
|
|
:instrumentation_class | 'Metric_Class'
|
|
|
|
:instrumentation_class | 'metricClass'
|
2021-03-08 18:12:59 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
with_them do
|
|
|
|
before do
|
|
|
|
attributes[attribute] = value
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'raise exception' do
|
2021-10-27 15:23:28 +05:30
|
|
|
expect(Gitlab::ErrorTracking).to receive(:track_and_raise_for_dev_exception).at_least(:once).with(instance_of(Gitlab::Usage::MetricDefinition::InvalidError))
|
2021-03-08 18:12:59 +05:30
|
|
|
|
|
|
|
described_class.new(path, attributes).validate!
|
|
|
|
end
|
2021-03-11 19:13:27 +05:30
|
|
|
|
|
|
|
context 'with skip_validation' do
|
|
|
|
it 'raise exception if skip_validation: false' do
|
2021-10-27 15:23:28 +05:30
|
|
|
expect(Gitlab::ErrorTracking).to receive(:track_and_raise_for_dev_exception).at_least(:once).with(instance_of(Gitlab::Usage::MetricDefinition::InvalidError))
|
2021-03-11 19:13:27 +05:30
|
|
|
|
|
|
|
described_class.new(path, attributes.merge( { skip_validation: false } )).validate!
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not raise exception if has skip_validation: true' do
|
|
|
|
expect(Gitlab::ErrorTracking).not_to receive(:track_and_raise_for_dev_exception)
|
|
|
|
|
|
|
|
described_class.new(path, attributes.merge( { skip_validation: true } )).validate!
|
|
|
|
end
|
|
|
|
end
|
2021-03-08 18:12:59 +05:30
|
|
|
end
|
2021-09-04 01:27:46 +05:30
|
|
|
|
|
|
|
context 'conditional validations' do
|
|
|
|
context 'when metric has broken status' do
|
|
|
|
it 'has to have repair issue url provided' do
|
|
|
|
attributes[:status] = 'broken'
|
|
|
|
attributes.delete(:repair_issue_url)
|
|
|
|
|
2021-10-27 15:23:28 +05:30
|
|
|
expect(Gitlab::ErrorTracking).to receive(:track_and_raise_for_dev_exception).at_least(:once).with(instance_of(Gitlab::Usage::MetricDefinition::InvalidError))
|
2021-09-04 01:27:46 +05:30
|
|
|
|
|
|
|
described_class.new(path, attributes).validate!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2021-03-08 18:12:59 +05:30
|
|
|
end
|
|
|
|
|
2022-07-16 23:28:13 +05:30
|
|
|
describe '#valid_service_ping_status?' do
|
|
|
|
context 'when metric has active status' do
|
|
|
|
it 'has to return true' do
|
|
|
|
attributes[:status] = 'active'
|
|
|
|
|
|
|
|
expect(described_class.new(path, attributes).valid_service_ping_status?).to be_truthy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when metric has removed status' do
|
|
|
|
it 'has to return false' do
|
|
|
|
attributes[:status] = 'removed'
|
|
|
|
|
|
|
|
expect(described_class.new(path, attributes).valid_service_ping_status?).to be_falsey
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-04-29 21:17:54 +05:30
|
|
|
describe 'statuses' do
|
|
|
|
using RSpec::Parameterized::TableSyntax
|
|
|
|
|
|
|
|
where(:status, :skip_validation?) do
|
|
|
|
'deprecated' | true
|
|
|
|
'removed' | true
|
2021-11-11 11:23:49 +05:30
|
|
|
'active' | false
|
2021-04-29 21:17:54 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
with_them do
|
|
|
|
subject(:validation) do
|
|
|
|
described_class.new(path, attributes.merge( { status: status } )).send(:skip_validation?)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns true/false for skip_validation' do
|
|
|
|
expect(validation).to eq(skip_validation?)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
describe '.load_all!' do
|
|
|
|
let(:metric1) { Dir.mktmpdir('metric1') }
|
|
|
|
let(:metric2) { Dir.mktmpdir('metric2') }
|
|
|
|
let(:definitions) { {} }
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(described_class).to receive(:paths).and_return(
|
|
|
|
[
|
|
|
|
File.join(metric1, '**', '*.yml'),
|
|
|
|
File.join(metric2, '**', '*.yml')
|
|
|
|
]
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
subject { described_class.send(:load_all!) }
|
|
|
|
|
|
|
|
it 'has empty list when there are no definition files' do
|
|
|
|
is_expected.to be_empty
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'has one metric when there is one file' do
|
|
|
|
write_metric(metric1, path, yaml_content)
|
|
|
|
|
|
|
|
is_expected.to be_one
|
|
|
|
end
|
|
|
|
|
2021-09-04 01:27:46 +05:30
|
|
|
it 'when the same metric is defined multiple times raises exception' do
|
2021-03-08 18:12:59 +05:30
|
|
|
write_metric(metric1, path, yaml_content)
|
|
|
|
write_metric(metric2, path, yaml_content)
|
|
|
|
|
2021-10-27 15:23:28 +05:30
|
|
|
expect(Gitlab::ErrorTracking).to receive(:track_and_raise_for_dev_exception).with(instance_of(Gitlab::Usage::MetricDefinition::InvalidError))
|
2021-03-08 18:12:59 +05:30
|
|
|
|
|
|
|
subject
|
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
FileUtils.rm_rf(metric1)
|
|
|
|
FileUtils.rm_rf(metric2)
|
|
|
|
end
|
2021-04-29 21:17:54 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
describe 'dump_metrics_yaml' do
|
|
|
|
let(:other_attributes) do
|
|
|
|
{
|
|
|
|
description: 'Test metric definition',
|
|
|
|
value_type: 'string',
|
|
|
|
product_category: 'collection',
|
|
|
|
product_stage: 'growth',
|
2021-12-11 22:18:48 +05:30
|
|
|
product_section: 'devops',
|
2021-11-11 11:23:49 +05:30
|
|
|
status: 'active',
|
|
|
|
milestone: '14.1',
|
2021-04-29 21:17:54 +05:30
|
|
|
default_generation: 'generation_1',
|
|
|
|
key_path: 'counter.category.event',
|
|
|
|
product_group: 'group::product analytics',
|
|
|
|
time_frame: 'none',
|
|
|
|
data_source: 'database',
|
|
|
|
distribution: %w(ee ce),
|
2021-09-30 23:02:18 +05:30
|
|
|
tier: %w(free starter premium ultimate bronze silver gold),
|
2021-10-27 15:23:28 +05:30
|
|
|
data_category: 'optional'
|
2021-04-29 21:17:54 +05:30
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:other_yaml_content) { other_attributes.deep_stringify_keys.to_yaml }
|
|
|
|
let(:other_path) { File.join('metrics', 'test_metric.yml') }
|
|
|
|
let(:metric1) { Dir.mktmpdir('metric1') }
|
|
|
|
let(:metric2) { Dir.mktmpdir('metric2') }
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(described_class).to receive(:paths).and_return(
|
|
|
|
[
|
|
|
|
File.join(metric1, '**', '*.yml'),
|
|
|
|
File.join(metric2, '**', '*.yml')
|
|
|
|
]
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
FileUtils.rm_rf(metric1)
|
|
|
|
FileUtils.rm_rf(metric2)
|
|
|
|
end
|
|
|
|
|
|
|
|
subject { described_class.dump_metrics_yaml }
|
|
|
|
|
|
|
|
it 'returns a YAML with both metrics in a sequence' do
|
|
|
|
write_metric(metric1, path, yaml_content)
|
|
|
|
write_metric(metric2, other_path, other_yaml_content)
|
2021-03-08 18:12:59 +05:30
|
|
|
|
2021-04-29 21:17:54 +05:30
|
|
|
is_expected.to eq([attributes, other_attributes].map(&:deep_stringify_keys).to_yaml)
|
2021-03-08 18:12:59 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|