2019-12-13 16:07:41 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 14:09:03 -04:00
|
|
|
RSpec.describe 'Marginalia spec' do
|
2021-03-19 14:09:19 -04:00
|
|
|
class MarginaliaTestController < ApplicationController
|
|
|
|
skip_before_action :authenticate_user!, :check_two_factor_requirement
|
|
|
|
|
2019-12-13 16:07:41 -05:00
|
|
|
def first_user
|
|
|
|
User.first
|
|
|
|
render body: nil
|
|
|
|
end
|
2021-03-19 14:09:19 -04:00
|
|
|
|
2022-07-25 17:08:32 -04:00
|
|
|
def first_ci_pipeline
|
|
|
|
Ci::Pipeline.first
|
|
|
|
render body: nil
|
|
|
|
end
|
|
|
|
|
2021-03-19 14:09:19 -04:00
|
|
|
private
|
|
|
|
|
|
|
|
[:auth_user, :current_user, :set_experimentation_subject_id_cookie, :signed_in?].each do |method|
|
2022-08-01 23:09:33 -04:00
|
|
|
define_method(method) {}
|
2021-03-19 14:09:19 -04:00
|
|
|
end
|
2019-12-13 16:07:41 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
class MarginaliaTestJob
|
|
|
|
include Sidekiq::Worker
|
|
|
|
|
|
|
|
def perform
|
2021-03-19 14:09:19 -04:00
|
|
|
Gitlab::ApplicationContext.with_context(caller_id: self.class.name) do
|
|
|
|
User.first
|
|
|
|
end
|
2019-12-13 16:07:41 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-01-27 16:08:47 -05:00
|
|
|
class MarginaliaTestMailer < ApplicationMailer
|
2019-12-13 16:07:41 -05:00
|
|
|
def first_user
|
|
|
|
User.first
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'For rails web requests' do
|
|
|
|
let(:correlation_id) { SecureRandom.uuid }
|
2022-07-25 17:08:32 -04:00
|
|
|
let(:recorded) { ActiveRecord::QueryRecorder.new { make_request(correlation_id, :first_user) } }
|
2019-12-13 16:07:41 -05:00
|
|
|
|
|
|
|
let(:component_map) do
|
|
|
|
{
|
2021-03-19 14:09:19 -04:00
|
|
|
"application" => "test",
|
|
|
|
"endpoint_id" => "MarginaliaTestController#first_user",
|
2021-09-15 08:11:13 -04:00
|
|
|
"correlation_id" => correlation_id,
|
|
|
|
"db_config_name" => "main"
|
2019-12-13 16:07:41 -05:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2021-02-22 04:10:46 -05:00
|
|
|
it 'generates a query that includes the component and value' do
|
|
|
|
component_map.each do |component, value|
|
|
|
|
expect(recorded.log.last).to include("#{component}:#{value}")
|
2019-12-13 16:07:41 -05:00
|
|
|
end
|
|
|
|
end
|
2021-09-15 08:11:13 -04:00
|
|
|
|
|
|
|
context 'when using CI database' do
|
2022-07-25 17:08:32 -04:00
|
|
|
let(:recorded) { ActiveRecord::QueryRecorder.new { make_request(correlation_id, :first_ci_pipeline) } }
|
2021-09-15 08:11:13 -04:00
|
|
|
let(:component_map) do
|
|
|
|
{
|
|
|
|
"application" => "test",
|
2022-07-25 17:08:32 -04:00
|
|
|
"endpoint_id" => "MarginaliaTestController#first_ci_pipeline",
|
2021-09-15 08:11:13 -04:00
|
|
|
"correlation_id" => correlation_id,
|
2022-05-24 14:09:18 -04:00
|
|
|
"db_config_name" => 'ci'
|
2021-09-15 08:11:13 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2021-11-02 23:13:15 -04:00
|
|
|
before do
|
2021-09-15 08:11:13 -04:00
|
|
|
skip_if_multiple_databases_not_setup
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'generates a query that includes the component and value' do
|
|
|
|
component_map.each do |component, value|
|
|
|
|
expect(recorded.log.last).to include("#{component}:#{value}")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-12-13 16:07:41 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
describe 'for Sidekiq worker jobs' do
|
2020-04-21 11:21:10 -04:00
|
|
|
around do |example|
|
|
|
|
with_sidekiq_server_middleware do |chain|
|
2021-03-19 14:09:19 -04:00
|
|
|
chain.add Labkit::Middleware::Sidekiq::Context::Server
|
2020-04-21 11:21:10 -04:00
|
|
|
chain.add Marginalia::SidekiqInstrumentation::Middleware
|
|
|
|
Marginalia.application_name = "sidekiq"
|
|
|
|
example.run
|
|
|
|
end
|
2019-12-13 16:07:41 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
after(:all) do
|
|
|
|
MarginaliaTestJob.clear
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
MarginaliaTestJob.perform_async
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:sidekiq_job) { MarginaliaTestJob.jobs.first }
|
|
|
|
let(:recorded) { ActiveRecord::QueryRecorder.new { MarginaliaTestJob.drain } }
|
|
|
|
|
|
|
|
let(:component_map) do
|
|
|
|
{
|
2021-03-19 14:09:19 -04:00
|
|
|
"application" => "sidekiq",
|
|
|
|
"endpoint_id" => "MarginaliaTestJob",
|
|
|
|
"correlation_id" => sidekiq_job['correlation_id'],
|
2021-09-15 08:11:13 -04:00
|
|
|
"jid" => sidekiq_job['jid'],
|
|
|
|
"db_config_name" => "main"
|
2019-12-13 16:07:41 -05:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2021-02-22 04:10:46 -05:00
|
|
|
it 'generates a query that includes the component and value' do
|
|
|
|
component_map.each do |component, value|
|
|
|
|
expect(recorded.log.last).to include("#{component}:#{value}")
|
2019-12-13 16:07:41 -05:00
|
|
|
end
|
2021-02-22 04:10:46 -05:00
|
|
|
end
|
2019-12-13 16:07:41 -05:00
|
|
|
|
2021-06-29 23:07:30 -04:00
|
|
|
describe 'for ActionMailer delivery jobs', :sidekiq_mailers do
|
2021-02-22 04:10:46 -05:00
|
|
|
let(:delivery_job) { MarginaliaTestMailer.first_user.deliver_later }
|
2019-12-13 16:07:41 -05:00
|
|
|
|
2021-02-22 04:10:46 -05:00
|
|
|
let(:recorded) do
|
|
|
|
ActiveRecord::QueryRecorder.new do
|
2021-03-19 14:09:19 -04:00
|
|
|
Sidekiq::Worker.drain_all
|
2019-12-13 16:07:41 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-02-22 04:10:46 -05:00
|
|
|
let(:component_map) do
|
|
|
|
{
|
2021-09-15 08:11:13 -04:00
|
|
|
"application" => "sidekiq",
|
|
|
|
"endpoint_id" => "ActionMailer::MailDeliveryJob",
|
|
|
|
"jid" => delivery_job.job_id,
|
|
|
|
"db_config_name" => "main"
|
2021-02-22 04:10:46 -05:00
|
|
|
}
|
2019-12-13 16:07:41 -05:00
|
|
|
end
|
|
|
|
|
2021-02-22 04:10:46 -05:00
|
|
|
it 'generates a query that includes the component and value' do
|
|
|
|
component_map.each do |component, value|
|
|
|
|
expect(recorded.log.last).to include("#{component}:#{value}")
|
|
|
|
end
|
2019-12-13 16:07:41 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-10-14 08:08:58 -04:00
|
|
|
|
2022-07-25 17:08:32 -04:00
|
|
|
def make_request(correlation_id, action_name)
|
2020-10-14 08:08:58 -04:00
|
|
|
request_env = Rack::MockRequest.env_for('/')
|
|
|
|
|
|
|
|
::Labkit::Correlation::CorrelationId.use_id(correlation_id) do
|
2022-07-25 17:08:32 -04:00
|
|
|
MarginaliaTestController.action(action_name).call(request_env)
|
2020-10-14 08:08:58 -04:00
|
|
|
end
|
|
|
|
end
|
2019-12-13 16:07:41 -05:00
|
|
|
end
|