Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keep original VCAP-Request-ID for re-enqueued ReoccurringJobs #3208

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 20 additions & 14 deletions app/jobs/logging_context_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,23 @@ def perform
raise CloudController::Errors::ApiError.new_from_details('BlobstoreError', e.message)
end

def success(job)
with_request_id_set do
super(job)
end
end

def error(job, e)
error_presenter = if e.instance_of?(CloudController::Errors::CompoundError)
ErrorPresenter.new(e, false, V3ErrorHasher.new(e))
else
ErrorPresenter.new(e)
end
log_error(error_presenter, job)
save_error(error_presenter, job)
super(job, e)
with_request_id_set do
error_presenter = if e.instance_of?(CloudController::Errors::CompoundError)
ErrorPresenter.new(e, false, V3ErrorHasher.new(e))
else
ErrorPresenter.new(e)
end
log_error(error_presenter, job)
save_error(error_presenter, job)
super(job, e)
end
end

private
Expand All @@ -40,12 +48,10 @@ def save_error(error_presenter, job)
end

def log_error(error_presenter, job)
with_request_id_set do
if error_presenter.client_error?
logger.info(error_presenter.log_message, job_guid: job.guid)
else
logger.error(error_presenter.log_message, job_guid: job.guid)
end
if error_presenter.client_error?
logger.info(error_presenter.log_message, job_guid: job.guid)
else
logger.error(error_presenter.log_message, job_guid: job.guid)
end
end

Expand Down
43 changes: 37 additions & 6 deletions spec/unit/jobs/logging_context_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module Jobs
end
let(:request_id) { 'abc123' }
let(:background_logger) { instance_double(Steno::Logger).as_null_object }

let(:job) { double('Job', guid: 'gregid').as_null_object }
let(:handler) { double('Handler', error: nil, perform: 'fake-perform', max_attempts: 1, reschedule_at: Time.now) }

before do
Expand Down Expand Up @@ -76,8 +76,39 @@ module Jobs
end
end

context '#success(job)' do
it "sets the thread-local VCAP Request ID during execution of the wrapped job's success method" do
expect(handler).to receive(:success) do
expect(::VCAP::Request.current_id).to eq request_id
end

logging_context_job.success(job)
end

it "restores the original VCAP Request ID after execution of the wrapped job's success method" do
random_request_id = SecureRandom.uuid
::VCAP::Request.current_id = random_request_id

logging_context_job.success(job)

expect(::VCAP::Request.current_id).to eq random_request_id
end

it "restores the original VCAP Request ID after exception within execution of the wrapped job's success method" do
allow(handler).to receive(:success) do
raise 'runtime test exception'
end

random_request_id = SecureRandom.uuid
::VCAP::Request.current_id = random_request_id

expect { logging_context_job.success(job) }.to raise_error 'runtime test exception'

expect(::VCAP::Request.current_id).to eq random_request_id
end
end

context '#error(job, exception)' do
let(:job) { double('Job', guid: 'gregid').as_null_object }
let(:error_presenter) { instance_double(ErrorPresenter, to_hash: 'sanitized exception hash').as_null_object }

before do
Expand All @@ -101,15 +132,15 @@ module Jobs
logging_context_job.error(job, 'exception')
end

it 'sets the thread-local VCAP Request ID while logging method' do
expect(background_logger).to receive(:info) do
it "sets the thread-local VCAP Request ID during execution of the wrapped job's error method" do
expect(handler).to receive(:error) do
expect(::VCAP::Request.current_id).to eq request_id
end

logging_context_job.error(job, 'exception')
end

it "restores the original VCAP Request ID after execution of the wrapped job's perform method" do
it "restores the original VCAP Request ID after execution of the wrapped job's error method" do
random_request_id = SecureRandom.uuid
::VCAP::Request.current_id = random_request_id

Expand All @@ -118,7 +149,7 @@ module Jobs
expect(::VCAP::Request.current_id).to eq random_request_id
end

it "restores the original VCAP Request ID after exception within execution of the wrapped job's perform method" do
it "restores the original VCAP Request ID after exception within execution of the wrapped job's error method" do
allow(handler).to receive(:error) do
raise 'runtime test exception'
end
Expand Down