2019-08-22 06:57:44 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-01-08 13:07:32 -05:00
|
|
|
require 'spec_helper'
|
2018-04-01 03:46:52 -04:00
|
|
|
|
|
|
|
describe Gitlab::SidekiqLogging::StructuredLogger do
|
|
|
|
describe '#call' do
|
2019-12-18 10:08:03 -05:00
|
|
|
let(:timestamp) { Time.iso8601('2018-01-01T12:00:00.000Z') }
|
2019-08-08 23:51:40 -04:00
|
|
|
let(:created_at) { timestamp - 1.second }
|
|
|
|
let(:scheduling_latency_s) { 1.0 }
|
2019-07-31 08:07:47 -04:00
|
|
|
|
2018-04-01 03:46:52 -04:00
|
|
|
let(:job) do
|
|
|
|
{
|
|
|
|
"class" => "TestWorker",
|
2020-03-02 13:07:42 -05:00
|
|
|
"args" => [1234, 'hello', { 'key' => 'value' }],
|
2018-04-01 03:46:52 -04:00
|
|
|
"retry" => false,
|
|
|
|
"queue" => "cronjob:test_queue",
|
|
|
|
"queue_namespace" => "cronjob",
|
|
|
|
"jid" => "da883554ee4fe414012f5f42",
|
2019-07-31 08:07:47 -04:00
|
|
|
"created_at" => created_at.to_f,
|
|
|
|
"enqueued_at" => created_at.to_f,
|
2020-02-16 22:09:00 -05:00
|
|
|
"correlation_id" => 'cid',
|
|
|
|
"error_message" => "wrong number of arguments (2 for 3)",
|
|
|
|
"error_class" => "ArgumentError",
|
|
|
|
"error_backtrace" => []
|
2018-04-01 03:46:52 -04:00
|
|
|
}
|
|
|
|
end
|
2019-07-31 08:07:47 -04:00
|
|
|
|
2019-01-24 08:43:02 -05:00
|
|
|
let(:logger) { double }
|
2019-10-11 11:06:41 -04:00
|
|
|
let(:clock_thread_cputime_start) { 0.222222299 }
|
|
|
|
let(:clock_thread_cputime_end) { 1.333333799 }
|
2018-04-01 03:46:52 -04:00
|
|
|
let(:start_payload) do
|
2020-02-16 22:09:00 -05:00
|
|
|
job.except('error_backtrace', 'error_class', 'error_message').merge(
|
2018-04-01 03:46:52 -04:00
|
|
|
'message' => 'TestWorker JID-da883554ee4fe414012f5f42: start',
|
|
|
|
'job_status' => 'start',
|
|
|
|
'pid' => Process.pid,
|
2020-01-10 19:08:28 -05:00
|
|
|
'created_at' => created_at.to_f,
|
|
|
|
'enqueued_at' => created_at.to_f,
|
2019-07-31 08:07:47 -04:00
|
|
|
'scheduling_latency_s' => scheduling_latency_s
|
2018-04-01 03:46:52 -04:00
|
|
|
)
|
|
|
|
end
|
|
|
|
let(:end_payload) do
|
|
|
|
start_payload.merge(
|
|
|
|
'message' => 'TestWorker JID-da883554ee4fe414012f5f42: done: 0.0 sec',
|
|
|
|
'job_status' => 'done',
|
2020-04-21 11:21:10 -04:00
|
|
|
'duration_s' => 0.0,
|
2020-01-10 19:08:28 -05:00
|
|
|
'completed_at' => timestamp.to_f,
|
2020-04-21 11:21:10 -04:00
|
|
|
'cpu_s' => 1.11,
|
|
|
|
'db_duration_s' => 0.0
|
2018-04-01 03:46:52 -04:00
|
|
|
)
|
|
|
|
end
|
|
|
|
let(:exception_payload) do
|
|
|
|
end_payload.merge(
|
|
|
|
'message' => 'TestWorker JID-da883554ee4fe414012f5f42: fail: 0.0 sec',
|
|
|
|
'job_status' => 'fail',
|
2019-09-24 05:06:04 -04:00
|
|
|
'error_class' => 'ArgumentError',
|
2018-04-01 03:46:52 -04:00
|
|
|
'error_message' => 'some exception'
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(Sidekiq).to receive(:logger).and_return(logger)
|
|
|
|
|
|
|
|
allow(subject).to receive(:current_time).and_return(timestamp.to_f)
|
2019-08-22 10:31:57 -04:00
|
|
|
|
2019-10-11 11:06:41 -04:00
|
|
|
allow(Process).to receive(:clock_gettime).with(Process::CLOCK_THREAD_CPUTIME_ID).and_return(clock_thread_cputime_start, clock_thread_cputime_end)
|
2018-04-01 03:46:52 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
subject { described_class.new }
|
|
|
|
|
|
|
|
context 'with SIDEKIQ_LOG_ARGUMENTS enabled' do
|
|
|
|
before do
|
|
|
|
stub_env('SIDEKIQ_LOG_ARGUMENTS', '1')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'logs start and end of job' do
|
|
|
|
Timecop.freeze(timestamp) do
|
|
|
|
expect(logger).to receive(:info).with(start_payload).ordered
|
|
|
|
expect(logger).to receive(:info).with(end_payload).ordered
|
|
|
|
expect(subject).to receive(:log_job_start).and_call_original
|
|
|
|
expect(subject).to receive(:log_job_done).and_call_original
|
|
|
|
|
|
|
|
subject.call(job, 'test_queue') { }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'logs an exception in job' do
|
|
|
|
Timecop.freeze(timestamp) do
|
|
|
|
expect(logger).to receive(:info).with(start_payload)
|
|
|
|
expect(logger).to receive(:warn).with(hash_including(exception_payload))
|
|
|
|
expect(subject).to receive(:log_job_start).and_call_original
|
|
|
|
expect(subject).to receive(:log_job_done).and_call_original
|
|
|
|
|
|
|
|
expect do
|
|
|
|
subject.call(job, 'test_queue') do
|
|
|
|
raise ArgumentError, 'some exception'
|
|
|
|
end
|
|
|
|
end.to raise_error(ArgumentError)
|
|
|
|
end
|
|
|
|
end
|
2019-01-18 11:21:38 -05:00
|
|
|
|
2020-03-02 13:07:42 -05:00
|
|
|
it 'does not modify the job' do
|
|
|
|
Timecop.freeze(timestamp) do
|
|
|
|
job_copy = job.deep_dup
|
|
|
|
|
|
|
|
allow(logger).to receive(:info)
|
|
|
|
allow(subject).to receive(:log_job_start).and_call_original
|
|
|
|
allow(subject).to receive(:log_job_done).and_call_original
|
|
|
|
|
|
|
|
subject.call(job, 'test_queue') do
|
|
|
|
expect(job).to eq(job_copy)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-04-01 03:46:52 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'with SIDEKIQ_LOG_ARGUMENTS disabled' do
|
2019-01-18 11:21:38 -05:00
|
|
|
it 'logs start and end of job without args' do
|
2018-04-01 03:46:52 -04:00
|
|
|
Timecop.freeze(timestamp) do
|
2019-01-18 11:21:38 -05:00
|
|
|
expect(logger).to receive(:info).with(start_payload.except('args')).ordered
|
|
|
|
expect(logger).to receive(:info).with(end_payload.except('args')).ordered
|
2018-04-01 03:46:52 -04:00
|
|
|
expect(subject).to receive(:log_job_start).and_call_original
|
|
|
|
expect(subject).to receive(:log_job_done).and_call_original
|
|
|
|
|
|
|
|
subject.call(job, 'test_queue') { }
|
|
|
|
end
|
|
|
|
end
|
2019-07-31 08:07:47 -04:00
|
|
|
|
|
|
|
it 'logs without created_at and enqueued_at fields' do
|
|
|
|
Timecop.freeze(timestamp) do
|
|
|
|
excluded_fields = %w(created_at enqueued_at args scheduling_latency_s)
|
|
|
|
|
|
|
|
expect(logger).to receive(:info).with(start_payload.except(*excluded_fields)).ordered
|
|
|
|
expect(logger).to receive(:info).with(end_payload.except(*excluded_fields)).ordered
|
|
|
|
expect(subject).to receive(:log_job_start).and_call_original
|
|
|
|
expect(subject).to receive(:log_job_done).and_call_original
|
|
|
|
|
|
|
|
subject.call(job.except("created_at", "enqueued_at"), 'test_queue') { }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with latency' do
|
2019-12-18 10:08:03 -05:00
|
|
|
let(:created_at) { Time.iso8601('2018-01-01T10:00:00.000Z') }
|
2019-07-31 08:07:47 -04:00
|
|
|
let(:scheduling_latency_s) { 7200.0 }
|
|
|
|
|
|
|
|
it 'logs with scheduling latency' do
|
|
|
|
Timecop.freeze(timestamp) do
|
|
|
|
expect(logger).to receive(:info).with(start_payload.except('args')).ordered
|
|
|
|
expect(logger).to receive(:info).with(end_payload.except('args')).ordered
|
|
|
|
expect(subject).to receive(:log_job_start).and_call_original
|
|
|
|
expect(subject).to receive(:log_job_done).and_call_original
|
|
|
|
|
|
|
|
subject.call(job, 'test_queue') { }
|
|
|
|
end
|
|
|
|
end
|
2018-04-01 03:46:52 -04:00
|
|
|
end
|
2019-08-09 00:33:20 -04:00
|
|
|
|
2020-03-24 14:07:55 -04:00
|
|
|
context 'with Gitaly, Rugged, and Redis calls' do
|
2019-08-09 00:33:20 -04:00
|
|
|
let(:timing_data) do
|
|
|
|
{
|
|
|
|
gitaly_calls: 10,
|
2020-04-21 11:21:10 -04:00
|
|
|
gitaly_duration_s: 10000,
|
2019-08-09 00:33:20 -04:00
|
|
|
rugged_calls: 1,
|
2020-04-21 11:21:10 -04:00
|
|
|
rugged_duration_s: 5000,
|
2020-03-24 14:07:55 -04:00
|
|
|
redis_calls: 3,
|
2020-04-21 11:21:10 -04:00
|
|
|
redis_duration_s: 1234
|
2019-08-09 00:33:20 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2020-03-24 14:07:55 -04:00
|
|
|
let(:expected_end_payload) do
|
|
|
|
end_payload.except('args').merge(timing_data)
|
2019-08-09 00:33:20 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'logs with Gitaly and Rugged timing data' do
|
|
|
|
Timecop.freeze(timestamp) do
|
|
|
|
expect(logger).to receive(:info).with(start_payload.except('args')).ordered
|
2020-03-24 14:07:55 -04:00
|
|
|
expect(logger).to receive(:info).with(expected_end_payload).ordered
|
2019-08-09 00:33:20 -04:00
|
|
|
|
2020-03-24 14:07:55 -04:00
|
|
|
subject.call(job, 'test_queue') do
|
|
|
|
job.merge!(timing_data)
|
|
|
|
end
|
2019-08-09 00:33:20 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-01-08 13:07:32 -05:00
|
|
|
|
|
|
|
context 'when the job performs database queries' do
|
|
|
|
before do
|
|
|
|
allow(Time).to receive(:now).and_return(timestamp)
|
|
|
|
allow(Process).to receive(:clock_gettime).and_call_original
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:expected_start_payload) { start_payload.except('args') }
|
|
|
|
|
|
|
|
let(:expected_end_payload) do
|
2020-04-21 11:21:10 -04:00
|
|
|
end_payload.except('args').merge('cpu_s' => a_value >= 0)
|
2020-01-08 13:07:32 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
let(:expected_end_payload_with_db) do
|
|
|
|
expected_end_payload.merge(
|
|
|
|
'db_duration_s' => a_value >= 0.1
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'logs the database time' do
|
|
|
|
expect(logger).to receive(:info).with(expected_start_payload).ordered
|
|
|
|
expect(logger).to receive(:info).with(expected_end_payload_with_db).ordered
|
|
|
|
|
|
|
|
subject.call(job, 'test_queue') { ActiveRecord::Base.connection.execute('SELECT pg_sleep(0.1);') }
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'prevents database time from leaking to the next job' do
|
|
|
|
expect(logger).to receive(:info).with(expected_start_payload).ordered
|
|
|
|
expect(logger).to receive(:info).with(expected_end_payload_with_db).ordered
|
|
|
|
expect(logger).to receive(:info).with(expected_start_payload).ordered
|
|
|
|
expect(logger).to receive(:info).with(expected_end_payload).ordered
|
|
|
|
|
|
|
|
subject.call(job, 'test_queue') { ActiveRecord::Base.connection.execute('SELECT pg_sleep(0.1);') }
|
|
|
|
subject.call(job, 'test_queue') { }
|
|
|
|
end
|
|
|
|
end
|
2019-10-11 11:06:41 -04:00
|
|
|
end
|
2019-08-22 10:31:57 -04:00
|
|
|
|
2019-10-11 11:06:41 -04:00
|
|
|
describe '#add_time_keys!' do
|
|
|
|
let(:time) { { duration: 0.1231234, cputime: 1.2342345 } }
|
|
|
|
let(:payload) { { 'class' => 'my-class', 'message' => 'my-message', 'job_status' => 'my-job-status' } }
|
2020-01-10 19:08:28 -05:00
|
|
|
let(:current_utc_time) { Time.now.utc }
|
2020-04-21 11:21:10 -04:00
|
|
|
let(:payload_with_time_keys) { { 'class' => 'my-class', 'message' => 'my-message', 'job_status' => 'my-job-status', 'duration_s' => 0.12, 'cpu_s' => 1.23, 'completed_at' => current_utc_time.to_f } }
|
2019-08-22 10:31:57 -04:00
|
|
|
|
2019-10-11 11:06:41 -04:00
|
|
|
subject { described_class.new }
|
2019-08-22 10:31:57 -04:00
|
|
|
|
2019-10-11 11:06:41 -04:00
|
|
|
it 'update payload correctly' do
|
2020-01-10 19:08:28 -05:00
|
|
|
Timecop.freeze(current_utc_time) do
|
|
|
|
subject.send(:add_time_keys!, time, payload)
|
2019-08-22 10:31:57 -04:00
|
|
|
|
2020-01-10 19:08:28 -05:00
|
|
|
expect(payload).to eq(payload_with_time_keys)
|
|
|
|
end
|
2019-08-22 10:31:57 -04:00
|
|
|
end
|
2018-04-01 03:46:52 -04:00
|
|
|
end
|
|
|
|
end
|