Commit e80446f0 authored by Mayra Cabrera's avatar Mayra Cabrera

Merge branch 'craigf/job-args-json-escape' into 'master'

Stringify sidekiq job args in logs

See merge request gitlab-org/gitlab!26075
parents 9f409f75 ec3f228c
...@@ -85,7 +85,11 @@ module Gitlab ...@@ -85,7 +85,11 @@ module Gitlab
job['pid'] = ::Process.pid job['pid'] = ::Process.pid
job.delete('args') unless ENV['SIDEKIQ_LOG_ARGUMENTS'] job.delete('args') unless ENV['SIDEKIQ_LOG_ARGUMENTS']
job['args'] = Gitlab::Utils::LogLimitedArray.log_limited_array(job['args']) if job['args']
if job['args']
job['args'].map!(&:to_s)
job['args'] = Gitlab::Utils::LogLimitedArray.log_limited_array(job['args'])
end
job job
end end
......
...@@ -30,6 +30,7 @@ describe Gitlab::SidekiqLogging::StructuredLogger do ...@@ -30,6 +30,7 @@ describe Gitlab::SidekiqLogging::StructuredLogger do
let(:clock_thread_cputime_end) { 1.333333799 } let(:clock_thread_cputime_end) { 1.333333799 }
let(:start_payload) do let(:start_payload) do
job.except('error_backtrace', 'error_class', 'error_message').merge( job.except('error_backtrace', 'error_class', 'error_message').merge(
'args' => job['args'].map(&:to_s),
'message' => 'TestWorker JID-da883554ee4fe414012f5f42: start', 'message' => 'TestWorker JID-da883554ee4fe414012f5f42: start',
'job_status' => 'start', 'job_status' => 'start',
'pid' => Process.pid, 'pid' => Process.pid,
...@@ -99,13 +100,29 @@ describe Gitlab::SidekiqLogging::StructuredLogger do ...@@ -99,13 +100,29 @@ describe Gitlab::SidekiqLogging::StructuredLogger do
end end
end end
context 'when the job args contain non-string objects' do
it 'converts them to strings' do
Timecop.freeze(timestamp) do
job['args'] = [2, true, 'foo', { "foo" => "bar" }]
expected_args = ['2', 'true', 'foo', '{"foo"=>"bar"}']
expect(logger).to receive(:info).with(start_payload.merge('args' => expected_args)).ordered
expect(logger).to receive(:info).with(end_payload.merge('args' => expected_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
end
context 'when the job args are bigger than the maximum allowed' do context 'when the job args are bigger than the maximum allowed' do
it 'keeps args from the front until they exceed the limit' do it 'keeps args from the front until they exceed the limit' do
Timecop.freeze(timestamp) do Timecop.freeze(timestamp) do
half_limit = Gitlab::Utils::LogLimitedArray::MAXIMUM_ARRAY_LENGTH / 2 half_limit = Gitlab::Utils::LogLimitedArray::MAXIMUM_ARRAY_LENGTH / 2
job['args'] = [1, 2, 'a' * half_limit, 'b' * half_limit, 3] job['args'] = [1, 2, 'a' * half_limit, 'b' * half_limit, 3]
expected_args = job['args'].take(3) + ['...'] expected_args = job['args'].map(&:to_s).take(3) + ['...']
expect(logger).to receive(:info).with(start_payload.merge('args' => expected_args)).ordered expect(logger).to receive(:info).with(start_payload.merge('args' => expected_args)).ordered
expect(logger).to receive(:info).with(end_payload.merge('args' => expected_args)).ordered expect(logger).to receive(:info).with(end_payload.merge('args' => expected_args)).ordered
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment