2020-06-02 05:08:01 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 14:09:03 -04:00
|
|
|
RSpec.describe Gitlab::Metrics::ElasticsearchRackMiddleware do
|
2020-06-02 05:08:01 -04:00
|
|
|
let(:app) { double(:app, call: 'app call result') }
|
|
|
|
let(:middleware) { described_class.new(app) }
|
|
|
|
let(:env) { {} }
|
|
|
|
let(:transaction) { Gitlab::Metrics::WebTransaction.new(env) }
|
|
|
|
|
|
|
|
describe '#call' do
|
|
|
|
let(:elasticsearch_query_time) { 0.1 }
|
|
|
|
let(:elasticsearch_requests_count) { 2 }
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(Gitlab::Instrumentation::ElasticsearchTransport).to receive(:query_time) { elasticsearch_query_time }
|
|
|
|
allow(Gitlab::Instrumentation::ElasticsearchTransport).to receive(:get_request_count) { elasticsearch_requests_count }
|
|
|
|
|
|
|
|
allow(Gitlab::Metrics).to receive(:current_transaction).and_return(transaction)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'calls the app' do
|
|
|
|
expect(middleware.call(env)).to eq('app call result')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'records elasticsearch metrics' do
|
2020-07-28 08:09:49 -04:00
|
|
|
expect(transaction).to receive(:increment).with(:http_elasticsearch_requests_total, elasticsearch_requests_count)
|
|
|
|
expect(transaction).to receive(:observe).with(:http_elasticsearch_requests_duration_seconds, elasticsearch_query_time)
|
2020-06-02 05:08:01 -04:00
|
|
|
|
|
|
|
middleware.call(env)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'records elasticsearch metrics if an error is raised' do
|
2020-07-28 08:09:49 -04:00
|
|
|
expect(transaction).to receive(:increment).with(:http_elasticsearch_requests_total, elasticsearch_requests_count)
|
|
|
|
expect(transaction).to receive(:observe).with(:http_elasticsearch_requests_duration_seconds, elasticsearch_query_time)
|
2020-06-02 05:08:01 -04:00
|
|
|
|
|
|
|
allow(app).to receive(:call).with(env).and_raise(StandardError)
|
|
|
|
|
|
|
|
expect { middleware.call(env) }.to raise_error(StandardError)
|
|
|
|
end
|
2020-08-05 23:10:05 -04:00
|
|
|
|
|
|
|
context 'when there are no elasticsearch requests' do
|
|
|
|
let(:elasticsearch_requests_count) { 0 }
|
|
|
|
|
2020-08-06 20:09:52 -04:00
|
|
|
it 'does not record any metrics' do
|
2020-08-05 23:10:05 -04:00
|
|
|
expect(transaction).not_to receive(:observe).with(:http_elasticsearch_requests_duration_seconds)
|
2020-08-06 20:09:52 -04:00
|
|
|
expect(transaction).not_to receive(:increment).with(:http_elasticsearch_requests_total, 0)
|
2020-08-05 23:10:05 -04:00
|
|
|
|
|
|
|
middleware.call(env)
|
|
|
|
end
|
|
|
|
end
|
2020-06-02 05:08:01 -04:00
|
|
|
end
|
|
|
|
end
|