2019-10-12 21:52:04 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
require 'spec_helper'
|
2019-10-12 21:52:04 +05:30
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
# rubocop: disable RSpec/MultipleMemoizedHelpers
|
2020-07-28 23:09:34 +05:30
|
|
|
RSpec.describe Gitlab::SidekiqMiddleware::ServerMetrics do
|
2021-04-29 21:17:54 +05:30
|
|
|
shared_examples "a metrics middleware" do
|
|
|
|
context "with mocked prometheus" do
|
|
|
|
include_context 'server metrics with mocked prometheus'
|
2019-12-26 22:10:19 +05:30
|
|
|
|
2021-04-29 21:17:54 +05:30
|
|
|
describe '#initialize' do
|
|
|
|
it 'sets concurrency metrics' do
|
|
|
|
expect(concurrency_metric).to receive(:set).with({}, Sidekiq.options[:concurrency].to_i)
|
2020-01-01 13:55:28 +05:30
|
|
|
|
2021-04-29 21:17:54 +05:30
|
|
|
subject
|
2020-01-01 13:55:28 +05:30
|
|
|
end
|
2021-04-29 21:17:54 +05:30
|
|
|
end
|
2020-01-01 13:55:28 +05:30
|
|
|
|
2021-04-29 21:17:54 +05:30
|
|
|
describe '#call' do
|
|
|
|
include_context 'server metrics call'
|
2020-01-01 13:55:28 +05:30
|
|
|
|
2021-04-29 21:17:54 +05:30
|
|
|
it 'yields block' do
|
|
|
|
expect { |b| subject.call(worker, job, :test, &b) }.to yield_control.once
|
2020-01-01 13:55:28 +05:30
|
|
|
end
|
|
|
|
|
2021-04-29 21:17:54 +05:30
|
|
|
it 'calls BackgroundTransaction' do
|
|
|
|
expect_next_instance_of(Gitlab::Metrics::BackgroundTransaction) do |instance|
|
|
|
|
expect(instance).to receive(:run)
|
2020-01-01 13:55:28 +05:30
|
|
|
end
|
|
|
|
|
2021-04-29 21:17:54 +05:30
|
|
|
subject.call(worker, job, :test) {}
|
|
|
|
end
|
2020-01-01 13:55:28 +05:30
|
|
|
|
2021-04-29 21:17:54 +05:30
|
|
|
it 'sets queue specific metrics' do
|
|
|
|
expect(running_jobs_metric).to receive(:increment).with(labels, -1)
|
|
|
|
expect(running_jobs_metric).to receive(:increment).with(labels, 1)
|
|
|
|
expect(queue_duration_seconds).to receive(:observe).with(labels, queue_duration_for_job) if queue_duration_for_job
|
|
|
|
expect(user_execution_seconds_metric).to receive(:observe).with(labels_with_job_status, thread_cputime_duration)
|
|
|
|
expect(db_seconds_metric).to receive(:observe).with(labels_with_job_status, db_duration)
|
|
|
|
expect(gitaly_seconds_metric).to receive(:observe).with(labels_with_job_status, gitaly_duration)
|
|
|
|
expect(completion_seconds_metric).to receive(:observe).with(labels_with_job_status, monotonic_time_duration)
|
|
|
|
expect(redis_seconds_metric).to receive(:observe).with(labels_with_job_status, redis_duration)
|
|
|
|
expect(elasticsearch_seconds_metric).to receive(:observe).with(labels_with_job_status, elasticsearch_duration)
|
|
|
|
expect(redis_requests_total).to receive(:increment).with(labels_with_job_status, redis_calls)
|
|
|
|
expect(elasticsearch_requests_total).to receive(:increment).with(labels_with_job_status, elasticsearch_calls)
|
|
|
|
|
|
|
|
subject.call(worker, job, :test) { nil }
|
|
|
|
end
|
2021-04-17 20:07:23 +05:30
|
|
|
|
2021-04-29 21:17:54 +05:30
|
|
|
it 'sets the thread name if it was nil' do
|
|
|
|
allow(Thread.current).to receive(:name).and_return(nil)
|
|
|
|
expect(Thread.current).to receive(:name=).with(Gitlab::Metrics::Samplers::ThreadsSampler::SIDEKIQ_WORKER_THREAD_NAME)
|
2021-04-17 20:07:23 +05:30
|
|
|
|
2021-04-29 21:17:54 +05:30
|
|
|
subject.call(worker, job, :test) { nil }
|
|
|
|
end
|
2020-06-23 00:09:42 +05:30
|
|
|
|
2021-04-29 21:17:54 +05:30
|
|
|
context 'when job_duration is not available' do
|
|
|
|
let(:queue_duration_for_job) { nil }
|
2020-01-01 13:55:28 +05:30
|
|
|
|
2021-04-29 21:17:54 +05:30
|
|
|
it 'does not set the queue_duration_seconds histogram' do
|
|
|
|
expect(queue_duration_seconds).not_to receive(:observe)
|
2020-10-24 23:57:45 +05:30
|
|
|
|
|
|
|
subject.call(worker, job, :test) { nil }
|
|
|
|
end
|
2021-04-29 21:17:54 +05:30
|
|
|
end
|
2020-10-24 23:57:45 +05:30
|
|
|
|
2021-04-29 21:17:54 +05:30
|
|
|
context 'when error is raised' do
|
|
|
|
let(:job_status) { :fail }
|
2020-01-01 13:55:28 +05:30
|
|
|
|
2021-04-29 21:17:54 +05:30
|
|
|
it 'sets sidekiq_jobs_failed_total and reraises' do
|
|
|
|
expect(failed_total_metric).to receive(:increment).with(labels, 1)
|
2020-01-01 13:55:28 +05:30
|
|
|
|
2021-04-29 21:17:54 +05:30
|
|
|
expect { subject.call(worker, job, :test) { raise StandardError, "Failed" } }.to raise_error(StandardError, "Failed")
|
2020-01-01 13:55:28 +05:30
|
|
|
end
|
|
|
|
end
|
2019-12-26 22:10:19 +05:30
|
|
|
|
2021-04-29 21:17:54 +05:30
|
|
|
context 'when job is retried' do
|
|
|
|
let(:job) { { 'retry_count' => 1 } }
|
2019-12-26 22:10:19 +05:30
|
|
|
|
2021-04-29 21:17:54 +05:30
|
|
|
it 'sets sidekiq_jobs_retried_total metric' do
|
|
|
|
expect(retried_total_metric).to receive(:increment)
|
2019-12-26 22:10:19 +05:30
|
|
|
|
2021-04-29 21:17:54 +05:30
|
|
|
subject.call(worker, job, :test) { nil }
|
2020-01-01 13:55:28 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-12-26 22:10:19 +05:30
|
|
|
|
2021-04-29 21:17:54 +05:30
|
|
|
context "with prometheus integrated" do
|
|
|
|
describe '#call' do
|
|
|
|
it 'yields block' do
|
|
|
|
expect { |b| subject.call(worker, job, :test, &b) }.to yield_control.once
|
2020-06-23 00:09:42 +05:30
|
|
|
end
|
|
|
|
|
2021-04-29 21:17:54 +05:30
|
|
|
context 'when error is raised' do
|
|
|
|
let(:job_status) { :fail }
|
2019-10-12 21:52:04 +05:30
|
|
|
|
2021-04-29 21:17:54 +05:30
|
|
|
it 'sets sidekiq_jobs_failed_total and reraises' do
|
|
|
|
expect { subject.call(worker, job, :test) { raise StandardError, "Failed" } }.to raise_error(StandardError, "Failed")
|
|
|
|
end
|
2021-03-11 19:13:27 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2021-04-29 21:17:54 +05:30
|
|
|
end
|
2021-03-11 19:13:27 +05:30
|
|
|
|
2021-04-29 21:17:54 +05:30
|
|
|
it_behaves_like 'metrics middleware with worker attribution' do
|
|
|
|
let(:job_status) { :done }
|
|
|
|
let(:labels_with_job_status) { labels.merge(job_status: job_status.to_s) }
|
2019-10-12 21:52:04 +05:30
|
|
|
end
|
2021-09-04 01:27:46 +05:30
|
|
|
|
|
|
|
context 'DB load balancing' do
|
|
|
|
using RSpec::Parameterized::TableSyntax
|
|
|
|
|
|
|
|
subject { described_class.new }
|
|
|
|
|
|
|
|
let(:queue) { :test }
|
|
|
|
let(:worker_class) { worker.class }
|
|
|
|
let(:job) { {} }
|
|
|
|
let(:job_status) { :done }
|
|
|
|
let(:labels_with_job_status) { default_labels.merge(job_status: job_status.to_s) }
|
|
|
|
let(:default_labels) do
|
|
|
|
{ queue: queue.to_s,
|
|
|
|
worker: worker_class.to_s,
|
|
|
|
boundary: "",
|
|
|
|
external_dependencies: "no",
|
|
|
|
feature_category: "",
|
|
|
|
urgency: "low" }
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
stub_const('TestWorker', Class.new)
|
|
|
|
TestWorker.class_eval do
|
|
|
|
include Sidekiq::Worker
|
|
|
|
include WorkerAttributes
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:worker) { TestWorker.new }
|
|
|
|
|
|
|
|
include_context 'server metrics with mocked prometheus'
|
|
|
|
|
|
|
|
context 'when load_balancing is enabled' do
|
|
|
|
let(:load_balancing_metric) { double('load balancing metric') }
|
|
|
|
|
|
|
|
include_context 'clear DB Load Balancing configuration'
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(::Gitlab::Database::LoadBalancing).to receive(:enable?).and_return(true)
|
|
|
|
allow(Gitlab::Metrics).to receive(:counter).with(:sidekiq_load_balancing_count, anything).and_return(load_balancing_metric)
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#initialize' do
|
|
|
|
it 'sets load_balancing metrics' do
|
|
|
|
expect(Gitlab::Metrics).to receive(:counter).with(:sidekiq_load_balancing_count, anything).and_return(load_balancing_metric)
|
|
|
|
|
|
|
|
subject
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#call' do
|
|
|
|
include_context 'server metrics call'
|
|
|
|
|
|
|
|
context 'when :database_chosen is provided' do
|
|
|
|
where(:database_chosen) do
|
|
|
|
%w[primary retry replica]
|
|
|
|
end
|
|
|
|
|
|
|
|
with_them do
|
|
|
|
context "when #{params[:database_chosen]} is used" do
|
|
|
|
let(:labels_with_load_balancing) do
|
|
|
|
labels_with_job_status.merge(database_chosen: database_chosen, data_consistency: 'delayed')
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
job[:database_chosen] = database_chosen
|
|
|
|
job[:data_consistency] = 'delayed'
|
|
|
|
allow(load_balancing_metric).to receive(:increment)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'increment sidekiq_load_balancing_count' do
|
|
|
|
expect(load_balancing_metric).to receive(:increment).with(labels_with_load_balancing, 1)
|
|
|
|
|
|
|
|
described_class.new.call(worker, job, :test) { nil }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when :database_chosen is not provided' do
|
|
|
|
it 'does not increment sidekiq_load_balancing_count' do
|
|
|
|
expect(load_balancing_metric).not_to receive(:increment)
|
|
|
|
|
|
|
|
described_class.new.call(worker, job, :test) { nil }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when load_balancing is disabled' do
|
|
|
|
include_context 'clear DB Load Balancing configuration'
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(::Gitlab::Database::LoadBalancing).to receive(:enable?).and_return(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#initialize' do
|
|
|
|
it 'doesnt set load_balancing metrics' do
|
|
|
|
expect(Gitlab::Metrics).not_to receive(:counter).with(:sidekiq_load_balancing_count, anything)
|
|
|
|
|
|
|
|
subject
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-10-12 21:52:04 +05:30
|
|
|
end
|
2021-04-17 20:07:23 +05:30
|
|
|
# rubocop: enable RSpec/MultipleMemoizedHelpers
|