diff options
Diffstat (limited to 'spec/lib/gitlab/usage/metrics/instrumentations')
4 files changed, 150 insertions, 15 deletions
diff --git a/spec/lib/gitlab/usage/metrics/instrumentations/collected_data_categories_metric_spec.rb b/spec/lib/gitlab/usage/metrics/instrumentations/collected_data_categories_metric_spec.rb index 8f52d550e5c..1b2170baf17 100644 --- a/spec/lib/gitlab/usage/metrics/instrumentations/collected_data_categories_metric_spec.rb +++ b/spec/lib/gitlab/usage/metrics/instrumentations/collected_data_categories_metric_spec.rb @@ -4,7 +4,7 @@ require 'spec_helper' RSpec.describe Gitlab::Usage::Metrics::Instrumentations::CollectedDataCategoriesMetric do it_behaves_like 'a correct instrumented metric value', {} do - let(:expected_value) { %w[Standard Subscription Operational Optional] } + let(:expected_value) { %w[standard subscription operational optional] } before do allow_next_instance_of(ServicePing::PermitDataCategoriesService) do |instance| diff --git a/spec/lib/gitlab/usage/metrics/instrumentations/database_metric_spec.rb b/spec/lib/gitlab/usage/metrics/instrumentations/database_metric_spec.rb index 5e36820df5e..0a32bdb95d3 100644 --- a/spec/lib/gitlab/usage/metrics/instrumentations/database_metric_spec.rb +++ b/spec/lib/gitlab/usage/metrics/instrumentations/database_metric_spec.rb @@ -4,11 +4,11 @@ require 'spec_helper' RSpec.describe Gitlab::Usage::Metrics::Instrumentations::DatabaseMetric do subject do - described_class.tap do |m| - m.relation { Issue } - m.operation :count - m.start { m.relation.minimum(:id) } - m.finish { m.relation.maximum(:id) } + described_class.tap do |metric_class| + metric_class.relation { Issue } + metric_class.operation :count + metric_class.start { metric_class.relation.minimum(:id) } + metric_class.finish { metric_class.relation.maximum(:id) } end.new(time_frame: 'all') end @@ -38,9 +38,9 @@ RSpec.describe Gitlab::Usage::Metrics::Instrumentations::DatabaseMetric do context 'with start and finish not called' do subject do - described_class.tap do |m| - m.relation { Issue } - m.operation :count + described_class.tap do |metric_class| + metric_class.relation { Issue } + metric_class.operation :count end.new(time_frame: 'all') end @@ -51,12 +51,12 @@ RSpec.describe Gitlab::Usage::Metrics::Instrumentations::DatabaseMetric do context 'with cache_start_and_finish_as called' do subject do - described_class.tap do |m| - m.relation { Issue } - m.operation :count - m.start { m.relation.minimum(:id) } - m.finish { m.relation.maximum(:id) } - m.cache_start_and_finish_as :special_issue_count + described_class.tap do |metric_class| + metric_class.relation { Issue } + metric_class.operation :count + metric_class.start { metric_class.relation.minimum(:id) } + metric_class.finish { metric_class.relation.maximum(:id) } + metric_class.cache_start_and_finish_as :special_issue_count end.new(time_frame: 'all') end @@ -71,5 +71,45 @@ RSpec.describe Gitlab::Usage::Metrics::Instrumentations::DatabaseMetric do expect(Rails.cache.read('metric_instrumentation/special_issue_count_maximum_id')).to eq(issues.max_by(&:id).id) end end + + context 'with estimate_batch_distinct_count' do + subject do + described_class.tap do |metric_class| + metric_class.relation { Issue } + metric_class.operation(:estimate_batch_distinct_count) + metric_class.start { metric_class.relation.minimum(:id) } + metric_class.finish { metric_class.relation.maximum(:id) } + end.new(time_frame: 'all') + end + + it 'calculates a correct result' do + expect(subject.value).to be_within(Gitlab::Database::PostgresHll::BatchDistinctCounter::ERROR_RATE).percent_of(3) + end + + context 'with block passed to operation' do + let(:buckets) { double('Buckets').as_null_object } + + subject do + described_class.tap do |metric_class| + metric_class.relation { Issue } + metric_class.operation(:estimate_batch_distinct_count) do |result| + result.foo + end + metric_class.start { metric_class.relation.minimum(:id) } + metric_class.finish { metric_class.relation.maximum(:id) } + end.new(time_frame: 'all') + end + + before do + allow(Gitlab::Database::PostgresHll::Buckets).to receive(:new).and_return(buckets) + end + + it 'calls the block passing HLL buckets as an argument' do + expect(buckets).to receive(:foo) + + subject.value + end + end + end end end diff --git a/spec/lib/gitlab/usage/metrics/instrumentations/generic_metric_spec.rb b/spec/lib/gitlab/usage/metrics/instrumentations/generic_metric_spec.rb new file mode 100644 index 00000000000..158be34d39c --- /dev/null +++ b/spec/lib/gitlab/usage/metrics/instrumentations/generic_metric_spec.rb @@ -0,0 +1,72 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Gitlab::Usage::Metrics::Instrumentations::GenericMetric do + shared_examples 'custom fallback' do |custom_fallback| + subject do + Class.new(described_class) do + fallback(custom_fallback) + value { Gitlab::Database.main.version } + end.new(time_frame: 'none') + end + + describe '#value' do + it 'gives the correct value' do + expect(subject.value).to eq(Gitlab::Database.main.version) + end + + context 'when raising an exception' do + it 'return the custom fallback' do + expect(Gitlab::Database.main).to receive(:version).and_raise('Error') + expect(subject.value).to eq(custom_fallback) + end + end + end + end + + context 'with default fallback' do + subject do + Class.new(described_class) do + value { Gitlab::Database.main.version } + end.new(time_frame: 'none') + end + + describe '#value' do + it 'gives the correct value' do + expect(subject.value).to eq(Gitlab::Database.main.version ) + end + + context 'when raising an exception' do + it 'return the default fallback' do + expect(Gitlab::Database.main).to receive(:version).and_raise('Error') + expect(subject.value).to eq(described_class::FALLBACK) + end + end + end + end + + context 'with custom fallback -2' do + it_behaves_like 'custom fallback', -2 + end + + context 'with custom fallback nil' do + it_behaves_like 'custom fallback', nil + end + + context 'with custom fallback false' do + it_behaves_like 'custom fallback', false + end + + context 'with custom fallback true' do + it_behaves_like 'custom fallback', true + end + + context 'with custom fallback []' do + it_behaves_like 'custom fallback', [] + end + + context 'with custom fallback { major: -1 }' do + it_behaves_like 'custom fallback', { major: -1 } + end +end diff --git a/spec/lib/gitlab/usage/metrics/instrumentations/redis_metric_spec.rb b/spec/lib/gitlab/usage/metrics/instrumentations/redis_metric_spec.rb new file mode 100644 index 00000000000..fb3bd1ba834 --- /dev/null +++ b/spec/lib/gitlab/usage/metrics/instrumentations/redis_metric_spec.rb @@ -0,0 +1,23 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Gitlab::Usage::Metrics::Instrumentations::RedisMetric, :clean_gitlab_redis_shared_state do + before do + 4.times do + Gitlab::UsageDataCounters::SourceCodeCounter.count(:pushes) + end + end + + let(:expected_value) { 4 } + + it_behaves_like 'a correct instrumented metric value', { options: { event: 'pushes', counter_class: 'SourceCodeCounter' } } + + it 'raises an exception if event option is not present' do + expect { described_class.new(counter_class: 'SourceCodeCounter') }.to raise_error(ArgumentError) + end + + it 'raises an exception if counter_class option is not present' do + expect { described_class.new(event: 'pushes') }.to raise_error(ArgumentError) + end +end |