Commit 89d99723 authored by Stan Hu's avatar Stan Hu

Merge branch '4994-geo-log-jid-for-sync-related-jobs' into 'master'

Resolve "[Geo] Log JID for sync related jobs"

Closes #4994

See merge request gitlab-org/gitlab-ee!5007
parents 73d1fab8 fe1625bb
---
title: "[Geo] Log JID for sync related jobs"
merge_request:
author:
type: other
module Gitlab
module Geo
module LogHelpers
SIDEKIQ_JID_LENGTH = 24
def log_info(message, details = {})
data = base_log_data(message)
data.merge!(details) if details
......@@ -19,13 +21,26 @@ module Gitlab
def base_log_data(message)
{
class: self.class.name,
message: message
}
message: message,
job_id: get_sidekiq_job_id
}.compact
end
def geo_logger
Gitlab::Geo::Logger
end
def get_sidekiq_job_id
context_data = Thread.current[:sidekiq_context]&.first
return unless context_data
index = context_data.index('JID-')
return unless index
context_data[index + 4..-1]
end
end
end
end
......@@ -9,8 +9,9 @@ module Gitlab
project_id: project.id,
project_path: project.full_path,
storage_version: project.storage_version,
message: message
}
message: message,
job_id: get_sidekiq_job_id
}.compact
end
end
end
......
require 'spec_helper'
describe Gitlab::Geo::LogHelpers do
class FakeLogHelpersConsumer
include Gitlab::Geo::LogHelpers
def execute
log_error('Test message')
end
end
def stub_sidekiq_job_context(context)
original_value = Thread.current[:sidekiq_context]
Thread.current[:sidekiq_context] = context
yield
Thread.current[:sidekiq_context] = original_value
end
context 'Sidekiq context' do
it 'does not log empty job_id when running outside of job' do
expect(Gitlab::Geo::Logger).to receive(:error).with({ class: 'FakeLogHelpersConsumer',
message: 'Test message' })
FakeLogHelpersConsumer.new.execute
end
it 'logs sidekiq_context' do
expect(Gitlab::Geo::Logger).to receive(:error).with({ class: 'FakeLogHelpersConsumer',
message: 'Test message',
job_id: '5b9b108c7558fe3c32cc61a5' })
stub_sidekiq_job_context(['TestWorker JID-5b9b108c7558fe3c32cc61a5']) do
FakeLogHelpersConsumer.new.execute
end
end
end
end
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