2019-07-29 07:53:12 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-01-16 13:08:46 -05:00
|
|
|
require 'spec_helper'
|
2019-07-29 07:53:12 -04:00
|
|
|
|
2021-03-01 16:11:09 -05:00
|
|
|
# rubocop: disable RSpec/MultipleMemoizedHelpers
|
2020-06-24 14:09:03 -04:00
|
|
|
RSpec.describe Gitlab::SidekiqMiddleware::ServerMetrics do
|
2021-04-14 11:09:04 -04:00
|
|
|
shared_examples "a metrics middleware" do
|
|
|
|
context "with mocked prometheus" do
|
|
|
|
include_context 'server metrics with mocked prometheus'
|
2019-10-22 08:06:20 -04:00
|
|
|
|
2021-06-28 02:07:45 -04:00
|
|
|
describe '.initialize_process_metrics' do
|
2021-04-14 11:09:04 -04:00
|
|
|
it 'sets concurrency metrics' do
|
|
|
|
expect(concurrency_metric).to receive(:set).with({}, Sidekiq.options[:concurrency].to_i)
|
2019-11-22 13:06:00 -05:00
|
|
|
|
2021-06-28 02:07:45 -04:00
|
|
|
described_class.initialize_process_metrics
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'initializes sidekiq_jobs_completion_seconds for the workers in the current Sidekiq process' do
|
|
|
|
allow(Gitlab::SidekiqConfig)
|
|
|
|
.to receive(:current_worker_queue_mappings)
|
2021-07-13 11:08:38 -04:00
|
|
|
.and_return('MergeWorker' => 'merge', 'Ci::BuildFinishedWorker' => 'default')
|
2021-06-28 02:07:45 -04:00
|
|
|
|
|
|
|
expect(completion_seconds_metric)
|
2022-05-02 11:10:10 -04:00
|
|
|
.to receive(:get).with({ queue: 'merge',
|
2021-06-28 02:07:45 -04:00
|
|
|
worker: 'MergeWorker',
|
|
|
|
urgency: 'high',
|
|
|
|
external_dependencies: 'no',
|
|
|
|
feature_category: 'source_code_management',
|
|
|
|
boundary: '',
|
2022-05-02 11:10:10 -04:00
|
|
|
job_status: 'done' })
|
2021-06-28 02:07:45 -04:00
|
|
|
|
|
|
|
expect(completion_seconds_metric)
|
2022-05-02 11:10:10 -04:00
|
|
|
.to receive(:get).with({ queue: 'merge',
|
2021-06-28 02:07:45 -04:00
|
|
|
worker: 'MergeWorker',
|
|
|
|
urgency: 'high',
|
|
|
|
external_dependencies: 'no',
|
|
|
|
feature_category: 'source_code_management',
|
|
|
|
boundary: '',
|
2022-05-02 11:10:10 -04:00
|
|
|
job_status: 'fail' })
|
2021-06-28 02:07:45 -04:00
|
|
|
|
|
|
|
expect(completion_seconds_metric)
|
2022-05-02 11:10:10 -04:00
|
|
|
.to receive(:get).with({ queue: 'default',
|
2021-07-13 11:08:38 -04:00
|
|
|
worker: 'Ci::BuildFinishedWorker',
|
2021-06-28 02:07:45 -04:00
|
|
|
urgency: 'high',
|
|
|
|
external_dependencies: 'no',
|
|
|
|
feature_category: 'continuous_integration',
|
|
|
|
boundary: 'cpu',
|
2022-05-02 11:10:10 -04:00
|
|
|
job_status: 'done' })
|
2021-06-28 02:07:45 -04:00
|
|
|
|
|
|
|
expect(completion_seconds_metric)
|
2022-05-02 11:10:10 -04:00
|
|
|
.to receive(:get).with({ queue: 'default',
|
2021-07-13 11:08:38 -04:00
|
|
|
worker: 'Ci::BuildFinishedWorker',
|
2021-06-28 02:07:45 -04:00
|
|
|
urgency: 'high',
|
|
|
|
external_dependencies: 'no',
|
|
|
|
feature_category: 'continuous_integration',
|
|
|
|
boundary: 'cpu',
|
2022-05-02 11:10:10 -04:00
|
|
|
job_status: 'fail' })
|
2021-06-28 02:07:45 -04:00
|
|
|
|
|
|
|
described_class.initialize_process_metrics
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the sidekiq_job_completion_metric_initialize feature flag is disabled' do
|
|
|
|
before do
|
|
|
|
stub_feature_flags(sidekiq_job_completion_metric_initialize: false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets the concurrency metric' do
|
|
|
|
expect(concurrency_metric).to receive(:set).with({}, Sidekiq.options[:concurrency].to_i)
|
|
|
|
|
|
|
|
described_class.initialize_process_metrics
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not initialize sidekiq_jobs_completion_seconds' do
|
|
|
|
allow(Gitlab::SidekiqConfig)
|
|
|
|
.to receive(:current_worker_queue_mappings)
|
2021-07-13 11:08:38 -04:00
|
|
|
.and_return('MergeWorker' => 'merge', 'Ci::BuildFinishedWorker' => 'default')
|
2021-06-28 02:07:45 -04:00
|
|
|
|
|
|
|
expect(completion_seconds_metric).not_to receive(:get)
|
|
|
|
|
|
|
|
described_class.initialize_process_metrics
|
|
|
|
end
|
2019-11-22 13:06:00 -05:00
|
|
|
end
|
2021-04-14 11:09:04 -04:00
|
|
|
end
|
2019-11-22 13:06:00 -05:00
|
|
|
|
2021-04-14 11:09:04 -04:00
|
|
|
describe '#call' do
|
|
|
|
include_context 'server metrics call'
|
2019-11-22 13:06:00 -05:00
|
|
|
|
2021-04-14 11:09:04 -04:00
|
|
|
it 'yields block' do
|
|
|
|
expect { |b| subject.call(worker, job, :test, &b) }.to yield_control.once
|
2019-11-22 13:06:00 -05:00
|
|
|
end
|
|
|
|
|
2021-04-14 11:09:04 -04:00
|
|
|
it 'calls BackgroundTransaction' do
|
|
|
|
expect_next_instance_of(Gitlab::Metrics::BackgroundTransaction) do |instance|
|
|
|
|
expect(instance).to receive(:run)
|
2021-04-01 11:08:54 -04:00
|
|
|
end
|
|
|
|
|
2021-04-14 11:09:04 -04:00
|
|
|
subject.call(worker, job, :test) {}
|
|
|
|
end
|
2019-11-22 13:06:00 -05:00
|
|
|
|
2021-04-14 11:09:04 -04:00
|
|
|
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)
|
2022-07-22 17:11:07 -04:00
|
|
|
expect(sidekiq_mem_total_bytes).to receive(:set).with(labels_with_job_status, mem_total_bytes)
|
2021-04-14 11:09:04 -04:00
|
|
|
|
|
|
|
subject.call(worker, job, :test) { nil }
|
|
|
|
end
|
2021-03-10 10:09:11 -05:00
|
|
|
|
2021-06-28 02:07:45 -04:00
|
|
|
it 'sets sidekiq_jobs_completion_seconds values that are compatible with those from .initialize_process_metrics' do
|
|
|
|
label_validator = Prometheus::Client::LabelSetValidator.new([:le])
|
|
|
|
|
|
|
|
allow(Gitlab::SidekiqConfig)
|
|
|
|
.to receive(:current_worker_queue_mappings)
|
2021-07-13 11:08:38 -04:00
|
|
|
.and_return('MergeWorker' => 'merge', 'Ci::BuildFinishedWorker' => 'default')
|
2021-06-28 02:07:45 -04:00
|
|
|
|
|
|
|
allow(completion_seconds_metric).to receive(:get) do |labels|
|
|
|
|
expect { label_validator.validate(labels) }.not_to raise_error
|
|
|
|
end
|
|
|
|
|
|
|
|
allow(completion_seconds_metric).to receive(:observe) do |labels, _duration|
|
|
|
|
expect { label_validator.validate(labels) }.not_to raise_error
|
|
|
|
end
|
|
|
|
|
|
|
|
described_class.initialize_process_metrics
|
|
|
|
|
|
|
|
subject.call(worker, job, :test) { nil }
|
|
|
|
end
|
|
|
|
|
2021-04-14 11:09:04 -04:00
|
|
|
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-03-10 10:09:11 -05:00
|
|
|
|
2021-04-14 11:09:04 -04:00
|
|
|
subject.call(worker, job, :test) { nil }
|
|
|
|
end
|
2020-05-20 08:07:52 -04:00
|
|
|
|
2021-04-14 11:09:04 -04:00
|
|
|
context 'when job_duration is not available' do
|
|
|
|
let(:queue_duration_for_job) { nil }
|
2019-11-22 13:06:00 -05:00
|
|
|
|
2021-04-14 11:09:04 -04:00
|
|
|
it 'does not set the queue_duration_seconds histogram' do
|
|
|
|
expect(queue_duration_seconds).not_to receive(:observe)
|
2020-07-21 23:09:40 -04:00
|
|
|
|
|
|
|
subject.call(worker, job, :test) { nil }
|
|
|
|
end
|
2021-04-14 11:09:04 -04:00
|
|
|
end
|
2020-07-21 23:09:40 -04:00
|
|
|
|
2021-04-14 11:09:04 -04:00
|
|
|
context 'when error is raised' do
|
|
|
|
let(:job_status) { :fail }
|
2019-11-22 13:06:00 -05:00
|
|
|
|
2021-04-14 11:09:04 -04:00
|
|
|
it 'sets sidekiq_jobs_failed_total and reraises' do
|
|
|
|
expect(failed_total_metric).to receive(:increment).with(labels, 1)
|
2019-11-22 13:06:00 -05:00
|
|
|
|
2021-04-14 11:09:04 -04:00
|
|
|
expect { subject.call(worker, job, :test) { raise StandardError, "Failed" } }.to raise_error(StandardError, "Failed")
|
2019-11-22 13:06:00 -05:00
|
|
|
end
|
|
|
|
end
|
2019-10-22 08:06:20 -04:00
|
|
|
|
2021-04-14 11:09:04 -04:00
|
|
|
context 'when job is retried' do
|
|
|
|
let(:job) { { 'retry_count' => 1 } }
|
2019-10-22 08:06:20 -04:00
|
|
|
|
2021-04-14 11:09:04 -04:00
|
|
|
it 'sets sidekiq_jobs_retried_total metric' do
|
|
|
|
expect(retried_total_metric).to receive(:increment)
|
2019-10-28 11:05:58 -04:00
|
|
|
|
2021-04-14 11:09:04 -04:00
|
|
|
subject.call(worker, job, :test) { nil }
|
2019-11-22 13:06:00 -05:00
|
|
|
end
|
|
|
|
end
|
2022-07-18 05:08:30 -04:00
|
|
|
|
|
|
|
context 'when job is interrupted' do
|
|
|
|
let(:job) { { 'interrupted_count' => 1 } }
|
|
|
|
|
|
|
|
it 'sets sidekiq_jobs_interrupted_total metric' do
|
|
|
|
expect(interrupted_total_metric).to receive(:increment)
|
|
|
|
|
|
|
|
subject.call(worker, job, :test) { nil }
|
|
|
|
end
|
|
|
|
end
|
2019-11-22 13:06:00 -05:00
|
|
|
end
|
|
|
|
end
|
2019-10-28 11:05:58 -04:00
|
|
|
|
2021-04-14 11:09:04 -04:00
|
|
|
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-05-20 17:09:09 -04:00
|
|
|
end
|
|
|
|
|
2021-04-14 11:09:04 -04:00
|
|
|
context 'when error is raised' do
|
|
|
|
let(:job_status) { :fail }
|
2019-07-29 07:53:12 -04:00
|
|
|
|
2021-04-14 11:09:04 -04:00
|
|
|
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-02-05 04:09:10 -05:00
|
|
|
end
|
|
|
|
end
|
2021-03-16 08:11:31 -04:00
|
|
|
end
|
2021-04-14 11:09:04 -04:00
|
|
|
end
|
2021-03-16 08:11:31 -04:00
|
|
|
|
2021-04-14 11:09:04 -04:00
|
|
|
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-07-29 07:53:12 -04:00
|
|
|
end
|
2021-06-08 11:10:00 -04:00
|
|
|
|
|
|
|
context 'DB load balancing' do
|
|
|
|
subject { described_class.new }
|
|
|
|
|
|
|
|
let(:queue) { :test }
|
|
|
|
let(:worker_class) { worker.class }
|
2021-06-21 05:10:07 -04:00
|
|
|
let(:worker) { TestWorker.new }
|
|
|
|
let(:client_middleware) { Gitlab::Database::LoadBalancing::SidekiqClientMiddleware.new }
|
|
|
|
let(:load_balancer) { double.as_null_object }
|
|
|
|
let(:load_balancing_metric) { double('load balancing metric') }
|
|
|
|
let(:job) { { "retry" => 3, "job_id" => "a180b47c-3fd6-41b8-81e9-34da61c3400e" } }
|
|
|
|
|
|
|
|
def process_job
|
|
|
|
client_middleware.call(worker_class, job, queue, double) do
|
|
|
|
worker_class.process_job(job)
|
|
|
|
end
|
2021-06-08 11:10:00 -04:00
|
|
|
end
|
|
|
|
|
2021-09-20 20:09:28 -04:00
|
|
|
include_context 'server metrics with mocked prometheus'
|
|
|
|
include_context 'server metrics call'
|
|
|
|
|
2021-06-08 11:10:00 -04:00
|
|
|
before do
|
|
|
|
stub_const('TestWorker', Class.new)
|
|
|
|
TestWorker.class_eval do
|
|
|
|
include Sidekiq::Worker
|
|
|
|
include WorkerAttributes
|
2021-06-21 05:10:07 -04:00
|
|
|
|
|
|
|
def perform(*args)
|
|
|
|
end
|
2021-06-08 11:10:00 -04:00
|
|
|
end
|
2021-06-21 05:10:07 -04:00
|
|
|
|
|
|
|
allow(::Gitlab::Database::LoadBalancing).to receive_message_chain(:proxy, :load_balancer).and_return(load_balancer)
|
|
|
|
allow(load_balancing_metric).to receive(:increment)
|
|
|
|
allow(Gitlab::Metrics).to receive(:counter).with(:sidekiq_load_balancing_count, anything).and_return(load_balancing_metric)
|
2021-06-08 11:10:00 -04:00
|
|
|
end
|
|
|
|
|
2021-06-21 05:10:07 -04:00
|
|
|
around do |example|
|
|
|
|
with_sidekiq_server_middleware do |chain|
|
|
|
|
chain.add Gitlab::Database::LoadBalancing::SidekiqServerMiddleware
|
|
|
|
chain.add described_class
|
|
|
|
Sidekiq::Testing.inline! { example.run }
|
|
|
|
end
|
|
|
|
end
|
2021-06-08 11:10:00 -04:00
|
|
|
|
2021-06-21 05:10:07 -04:00
|
|
|
shared_context 'worker declaring data consistency' do
|
|
|
|
let(:worker_class) { LBTestWorker }
|
2021-11-22 07:10:20 -05:00
|
|
|
let(:wal_locations) { { Gitlab::Database::MAIN_DATABASE_NAME.to_sym => 'AB/12345' } }
|
|
|
|
let(:job) { { "retry" => 3, "job_id" => "a180b47c-3fd6-41b8-81e9-34da61c3400e", "wal_locations" => wal_locations } }
|
2021-06-08 11:10:00 -04:00
|
|
|
|
|
|
|
before do
|
2021-06-21 05:10:07 -04:00
|
|
|
stub_const('LBTestWorker', Class.new(TestWorker))
|
|
|
|
LBTestWorker.class_eval do
|
|
|
|
include ApplicationWorker
|
2021-06-08 11:10:00 -04:00
|
|
|
|
2021-06-21 05:10:07 -04:00
|
|
|
data_consistency :delayed
|
2021-06-08 11:10:00 -04:00
|
|
|
end
|
|
|
|
end
|
2021-06-21 05:10:07 -04:00
|
|
|
end
|
2021-06-08 11:10:00 -04:00
|
|
|
|
2021-09-21 14:11:18 -04:00
|
|
|
describe '#call' do
|
|
|
|
context 'when worker declares data consistency' do
|
|
|
|
include_context 'worker declaring data consistency'
|
2021-06-08 11:10:00 -04:00
|
|
|
|
2021-09-21 14:11:18 -04:00
|
|
|
it 'increments load balancing counter with defined data consistency' do
|
|
|
|
process_job
|
2021-06-08 11:10:00 -04:00
|
|
|
|
2021-09-21 14:11:18 -04:00
|
|
|
expect(load_balancing_metric).to have_received(:increment).with(
|
|
|
|
a_hash_including(
|
|
|
|
data_consistency: :delayed,
|
|
|
|
load_balancing_strategy: 'replica'
|
|
|
|
), 1)
|
2021-06-08 11:10:00 -04:00
|
|
|
end
|
|
|
|
end
|
2021-06-21 05:10:07 -04:00
|
|
|
|
2021-09-21 14:11:18 -04:00
|
|
|
context 'when worker does not declare data consistency' do
|
|
|
|
it 'increments load balancing counter with default data consistency' do
|
2021-06-21 05:10:07 -04:00
|
|
|
process_job
|
|
|
|
|
2021-09-21 14:11:18 -04:00
|
|
|
expect(load_balancing_metric).to have_received(:increment).with(
|
|
|
|
a_hash_including(
|
|
|
|
data_consistency: :always,
|
|
|
|
load_balancing_strategy: 'primary'
|
|
|
|
), 1)
|
2021-06-21 05:10:07 -04:00
|
|
|
end
|
|
|
|
end
|
2021-06-08 11:10:00 -04:00
|
|
|
end
|
|
|
|
end
|
2021-09-20 20:09:28 -04:00
|
|
|
|
|
|
|
context 'feature attribution' do
|
|
|
|
let(:test_worker) do
|
|
|
|
category = worker_category
|
|
|
|
|
|
|
|
Class.new do
|
|
|
|
include Sidekiq::Worker
|
|
|
|
include WorkerAttributes
|
|
|
|
|
|
|
|
if category
|
|
|
|
feature_category category
|
|
|
|
else
|
2022-04-05 08:10:23 -04:00
|
|
|
feature_category :not_owned
|
2021-09-20 20:09:28 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def perform
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:context_category) { 'continuous_integration' }
|
|
|
|
let(:job) { { 'meta.feature_category' => 'continuous_integration' } }
|
|
|
|
|
|
|
|
before do
|
|
|
|
stub_const('TestWorker', test_worker)
|
|
|
|
end
|
|
|
|
|
|
|
|
around do |example|
|
|
|
|
with_sidekiq_server_middleware do |chain|
|
|
|
|
Gitlab::SidekiqMiddleware.server_configurator(
|
|
|
|
metrics: true,
|
|
|
|
arguments_logger: false,
|
|
|
|
memory_killer: false
|
|
|
|
).call(chain)
|
|
|
|
|
|
|
|
Sidekiq::Testing.inline! { example.run }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
include_context 'server metrics with mocked prometheus'
|
|
|
|
include_context 'server metrics call'
|
|
|
|
|
|
|
|
context 'when a worker has a feature category' do
|
|
|
|
let(:worker_category) { 'authentication_and_authorization' }
|
|
|
|
|
|
|
|
it 'uses that category for metrics' do
|
|
|
|
expect(completion_seconds_metric).to receive(:observe).with(a_hash_including(feature_category: worker_category), anything)
|
|
|
|
|
|
|
|
TestWorker.process_job(job)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when a worker does not have a feature category' do
|
|
|
|
let(:worker_category) { nil }
|
|
|
|
|
|
|
|
it 'uses the category from the context for metrics' do
|
|
|
|
expect(completion_seconds_metric).to receive(:observe).with(a_hash_including(feature_category: context_category), anything)
|
|
|
|
|
|
|
|
TestWorker.process_job(job)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-07-29 07:53:12 -04:00
|
|
|
end
|
2021-03-01 16:11:09 -05:00
|
|
|
# rubocop: enable RSpec/MultipleMemoizedHelpers
|