summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/metrics/web_transaction_spec.rb
diff options
context:
space:
mode:
Diffstat (limited to 'spec/lib/gitlab/metrics/web_transaction_spec.rb')
-rw-r--r--spec/lib/gitlab/metrics/web_transaction_spec.rb90
1 files changed, 20 insertions, 70 deletions
diff --git a/spec/lib/gitlab/metrics/web_transaction_spec.rb b/spec/lib/gitlab/metrics/web_transaction_spec.rb
index 9e22dccb2a2..06ce58a9e84 100644
--- a/spec/lib/gitlab/metrics/web_transaction_spec.rb
+++ b/spec/lib/gitlab/metrics/web_transaction_spec.rb
@@ -5,41 +5,14 @@ require 'spec_helper'
RSpec.describe Gitlab::Metrics::WebTransaction do
let(:env) { {} }
let(:transaction) { described_class.new(env) }
- let(:prometheus_metric) { instance_double(Prometheus::Client::Metric, base_labels: {}) }
- before do
- allow(described_class).to receive(:prometheus_metric).and_return(prometheus_metric)
- end
-
- RSpec.shared_context 'ActionController request' do
- let(:request) { double(:request, format: double(:format, ref: :html)) }
- let(:controller_class) { double(:controller_class, name: 'TestController') }
-
- before do
- controller = double(:controller, class: controller_class, action_name: 'show', request: request)
- env['action_controller.instance'] = controller
- end
- end
+ describe '#run' do
+ let(:prometheus_metric) { instance_double(Prometheus::Client::Metric, base_labels: {}) }
- RSpec.shared_context 'transaction observe metrics' do
before do
+ allow(described_class).to receive(:prometheus_metric).and_return(prometheus_metric)
allow(transaction).to receive(:observe)
end
- end
-
- RSpec.shared_examples 'metric with labels' do |metric_method|
- include_context 'ActionController request'
-
- it 'measures with correct labels and value' do
- value = 1
- expect(prometheus_metric).to receive(metric_method).with({ controller: 'TestController', action: 'show', feature_category: ::Gitlab::FeatureCategories::FEATURE_CATEGORY_DEFAULT }, value)
-
- transaction.send(metric_method, :bau, value)
- end
- end
-
- describe '#run' do
- include_context 'transaction observe metrics'
it 'yields the supplied block' do
expect { |b| transaction.run(&b) }.to yield_control
@@ -88,14 +61,6 @@ RSpec.describe Gitlab::Metrics::WebTransaction do
end
end
- describe '#method_call_for' do
- it 'returns a MethodCall' do
- method = transaction.method_call_for('Foo#bar', :Foo, '#bar')
-
- expect(method).to be_an_instance_of(Gitlab::Metrics::MethodCall)
- end
- end
-
describe '#labels' do
context 'when request goes to Grape endpoint' do
before do
@@ -115,7 +80,7 @@ RSpec.describe Gitlab::Metrics::WebTransaction do
end
it 'contains only the labels defined for transactions' do
- expect(transaction.labels.keys).to contain_exactly(*described_class.superclass::BASE_LABEL_KEYS)
+ expect(transaction.labels.keys).to contain_exactly(*described_class::BASE_LABEL_KEYS)
end
it 'does not provide labels if route infos are missing' do
@@ -129,14 +94,20 @@ RSpec.describe Gitlab::Metrics::WebTransaction do
end
context 'when request goes to ActionController' do
- include_context 'ActionController request'
+ let(:request) { double(:request, format: double(:format, ref: :html)) }
+ let(:controller_class) { double(:controller_class, name: 'TestController') }
+
+ before do
+ controller = double(:controller, class: controller_class, action_name: 'show', request: request)
+ env['action_controller.instance'] = controller
+ end
it 'tags a transaction with the name and action of a controller' do
expect(transaction.labels).to eq({ controller: 'TestController', action: 'show', feature_category: ::Gitlab::FeatureCategories::FEATURE_CATEGORY_DEFAULT })
end
it 'contains only the labels defined for transactions' do
- expect(transaction.labels.keys).to contain_exactly(*described_class.superclass::BASE_LABEL_KEYS)
+ expect(transaction.labels.keys).to contain_exactly(*described_class::BASE_LABEL_KEYS)
end
context 'when the request content type is not :html' do
@@ -170,37 +141,16 @@ RSpec.describe Gitlab::Metrics::WebTransaction do
end
end
- describe '#add_event' do
- let(:prometheus_metric) { instance_double(Prometheus::Client::Counter, :increment, base_labels: {}) }
-
- it 'adds a metric' do
- expect(prometheus_metric).to receive(:increment)
-
- transaction.add_event(:meow)
- end
+ it_behaves_like 'transaction metrics with labels' do
+ let(:request) { double(:request, format: double(:format, ref: :html)) }
+ let(:controller_class) { double(:controller_class, name: 'TestController') }
+ let(:controller) { double(:controller, class: controller_class, action_name: 'show', request: request) }
- it 'allows tracking of custom tags' do
- expect(prometheus_metric).to receive(:increment).with(animal: "dog")
+ let(:transaction_obj) { described_class.new({ 'action_controller.instance' => controller }) }
+ let(:labels) { { controller: 'TestController', action: 'show', feature_category: 'projects' } }
- transaction.add_event(:bau, animal: 'dog')
+ before do
+ ::Gitlab::ApplicationContext.push(feature_category: 'projects')
end
end
-
- describe '#increment' do
- let(:prometheus_metric) { instance_double(Prometheus::Client::Counter, :increment, base_labels: {}) }
-
- it_behaves_like 'metric with labels', :increment
- end
-
- describe '#set' do
- let(:prometheus_metric) { instance_double(Prometheus::Client::Gauge, :set, base_labels: {}) }
-
- it_behaves_like 'metric with labels', :set
- end
-
- describe '#observe' do
- let(:prometheus_metric) { instance_double(Prometheus::Client::Histogram, :observe, base_labels: {}) }
-
- it_behaves_like 'metric with labels', :observe
- end
end