2020-01-16 13:08:46 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 14:09:03 -04:00
|
|
|
RSpec.describe Gitlab::SidekiqMiddleware::ClientMetrics do
|
2020-01-16 13:08:46 -05:00
|
|
|
context "with worker attribution" do
|
|
|
|
subject { described_class.new }
|
|
|
|
|
|
|
|
let(:queue) { :test }
|
|
|
|
let(:worker_class) { worker.class }
|
|
|
|
let(:job) { {} }
|
2020-04-02 14:08:11 -04:00
|
|
|
let(:default_labels) do
|
|
|
|
{ queue: queue.to_s,
|
|
|
|
worker: worker_class.to_s,
|
|
|
|
boundary: "",
|
|
|
|
external_dependencies: "no",
|
|
|
|
feature_category: "",
|
|
|
|
urgency: "low" }
|
|
|
|
end
|
2020-01-16 13:08:46 -05:00
|
|
|
|
|
|
|
shared_examples "a metrics client middleware" do
|
|
|
|
context "with mocked prometheus" do
|
|
|
|
let(:enqueued_jobs_metric) { double('enqueued jobs metric', increment: true) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(Gitlab::Metrics).to receive(:counter).with(described_class::ENQUEUED, anything).and_return(enqueued_jobs_metric)
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#call' do
|
|
|
|
it 'yields block' do
|
2020-01-23 10:08:46 -05:00
|
|
|
expect { |b| subject.call(worker_class, job, :test, double, &b) }.to yield_control.once
|
2020-01-16 13:08:46 -05:00
|
|
|
end
|
|
|
|
|
2020-01-23 10:08:46 -05:00
|
|
|
it 'increments enqueued jobs metric with correct labels when worker is a string of the class' do
|
2020-01-16 13:08:46 -05:00
|
|
|
expect(enqueued_jobs_metric).to receive(:increment).with(labels, 1)
|
|
|
|
|
2020-01-23 10:08:46 -05:00
|
|
|
subject.call(worker_class.to_s, job, :test, double) { nil }
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'increments enqueued jobs metric with correct labels' do
|
|
|
|
expect(enqueued_jobs_metric).to receive(:increment).with(labels, 1)
|
|
|
|
|
|
|
|
subject.call(worker_class, job, :test, double) { nil }
|
2020-01-16 13:08:46 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when workers are not attributed" do
|
2020-05-20 02:08:06 -04:00
|
|
|
before do
|
|
|
|
stub_const('TestNonAttributedWorker', Class.new)
|
|
|
|
TestNonAttributedWorker.class_eval do
|
|
|
|
include Sidekiq::Worker
|
|
|
|
end
|
2020-01-16 13:08:46 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like "a metrics client middleware" do
|
|
|
|
let(:worker) { TestNonAttributedWorker.new }
|
2020-03-02 13:07:42 -05:00
|
|
|
let(:labels) { default_labels.merge(urgency: "") }
|
2020-01-16 13:08:46 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when workers are attributed" do
|
2020-03-02 13:07:42 -05:00
|
|
|
def create_attributed_worker_class(urgency, external_dependencies, resource_boundary, category)
|
2020-01-23 10:08:46 -05:00
|
|
|
klass = Class.new do
|
2020-01-16 13:08:46 -05:00
|
|
|
include Sidekiq::Worker
|
|
|
|
include WorkerAttributes
|
|
|
|
|
2020-03-02 13:07:42 -05:00
|
|
|
urgency urgency if urgency
|
2020-01-16 13:08:46 -05:00
|
|
|
worker_has_external_dependencies! if external_dependencies
|
|
|
|
worker_resource_boundary resource_boundary unless resource_boundary == :unknown
|
|
|
|
feature_category category unless category.nil?
|
|
|
|
end
|
2020-01-23 10:08:46 -05:00
|
|
|
stub_const("TestAttributedWorker", klass)
|
2020-01-16 13:08:46 -05:00
|
|
|
end
|
|
|
|
|
2020-03-02 13:07:42 -05:00
|
|
|
let(:urgency) { nil }
|
2020-01-16 13:08:46 -05:00
|
|
|
let(:external_dependencies) { false }
|
|
|
|
let(:resource_boundary) { :unknown }
|
|
|
|
let(:feature_category) { nil }
|
2020-03-02 13:07:42 -05:00
|
|
|
let(:worker_class) { create_attributed_worker_class(urgency, external_dependencies, resource_boundary, feature_category) }
|
2020-01-16 13:08:46 -05:00
|
|
|
let(:worker) { worker_class.new }
|
|
|
|
|
2020-03-02 13:07:42 -05:00
|
|
|
context "high urgency" do
|
2020-01-16 13:08:46 -05:00
|
|
|
it_behaves_like "a metrics client middleware" do
|
2020-03-02 13:07:42 -05:00
|
|
|
let(:urgency) { :high }
|
|
|
|
let(:labels) { default_labels.merge(urgency: "high") }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "no urgency" do
|
|
|
|
it_behaves_like "a metrics client middleware" do
|
2020-03-12 14:09:28 -04:00
|
|
|
let(:urgency) { :throttled }
|
|
|
|
let(:labels) { default_labels.merge(urgency: "throttled") }
|
2020-01-16 13:08:46 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "external dependencies" do
|
|
|
|
it_behaves_like "a metrics client middleware" do
|
|
|
|
let(:external_dependencies) { true }
|
|
|
|
let(:labels) { default_labels.merge(external_dependencies: "yes") }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "cpu boundary" do
|
|
|
|
it_behaves_like "a metrics client middleware" do
|
|
|
|
let(:resource_boundary) { :cpu }
|
|
|
|
let(:labels) { default_labels.merge(boundary: "cpu") }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "memory boundary" do
|
|
|
|
it_behaves_like "a metrics client middleware" do
|
|
|
|
let(:resource_boundary) { :memory }
|
|
|
|
let(:labels) { default_labels.merge(boundary: "memory") }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "feature category" do
|
|
|
|
it_behaves_like "a metrics client middleware" do
|
|
|
|
let(:feature_category) { :authentication }
|
|
|
|
let(:labels) { default_labels.merge(feature_category: "authentication") }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "combined" do
|
|
|
|
it_behaves_like "a metrics client middleware" do
|
2020-03-02 13:07:42 -05:00
|
|
|
let(:urgency) { :high }
|
2020-01-16 13:08:46 -05:00
|
|
|
let(:external_dependencies) { true }
|
|
|
|
let(:resource_boundary) { :cpu }
|
|
|
|
let(:feature_category) { :authentication }
|
2020-03-02 13:07:42 -05:00
|
|
|
let(:labels) { default_labels.merge(urgency: "high", external_dependencies: "yes", boundary: "cpu", feature_category: "authentication") }
|
2020-01-16 13:08:46 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|