Commit 406c702a authored by Toon Claes's avatar Toon Claes

Refactor the Geo LogCursor Logger to make class more descriptive

To make the source class more descriptive, i.e. including all the
parent module names, the Logger used by the Geo LogCursor is
refactored to be a class. The caller should instantiate it and pass it
it's class so the logger can extract it's name.

Closes gitlab-org/gitlab-ee#5632.
parent 20e12599
---
title: Refactor the Geo LogCursor Logger to make class more descriptive
merge_request: 5483
author:
type: fixed
...@@ -2,6 +2,8 @@ module Gitlab ...@@ -2,6 +2,8 @@ module Gitlab
module Geo module Geo
module LogCursor module LogCursor
class Daemon class Daemon
include Utils::StrongMemoize
VERSION = '0.2.0'.freeze VERSION = '0.2.0'.freeze
BATCH_SIZE = 250 BATCH_SIZE = 250
SECONDARY_CHECK_INTERVAL = 1.minute SECONDARY_CHECK_INTERVAL = 1.minute
...@@ -11,7 +13,6 @@ module Gitlab ...@@ -11,7 +13,6 @@ module Gitlab
def initialize(options = {}) def initialize(options = {})
@options = options @options = options
@exit = false @exit = false
logger.geo_logger.build.level = options[:debug] ? :debug : Rails.logger.level
end end
def run! def run!
...@@ -301,7 +302,11 @@ module Gitlab ...@@ -301,7 +302,11 @@ module Gitlab
end end
def logger def logger
Gitlab::Geo::LogCursor::Logger strong_memoize(:logger) do
level = options[:debug] ? :debug : Rails.logger.level
Gitlab::Geo::LogCursor::Logger.new(self.class, level)
end
end end
end end
end end
......
...@@ -50,7 +50,7 @@ module Gitlab ...@@ -50,7 +50,7 @@ module Gitlab
end end
def self.logger def self.logger
Gitlab::Geo::LogCursor::Logger @logger ||= Gitlab::Geo::LogCursor::Logger.new(self)
end end
private_class_method :exclusive_lease, :lease_taken_message, :logger private_class_method :exclusive_lease, :lease_taken_message, :logger
......
module Gitlab module Gitlab
module Geo module Geo
module LogCursor module LogCursor
module Logger class Logger
attr_accessor :klass
PID = Process.pid.freeze PID = Process.pid.freeze
def self.event_info(created_at, message, params = {}) def initialize(klass, level = Rails.logger.level)
args = { pid: PID, @klass = klass
class: caller_name, geo_logger.build.level = level
message: message, end
cursor_delay_s: cursor_delay(created_at) }.merge(params)
def event_info(created_at, message, params = {})
args = base_log_data(message).merge(
cursor_delay_s: cursor_delay(created_at)
).merge(params)
geo_logger.info(args) geo_logger.info(args)
end end
def self.info(message, params = {}) def info(message, params = {})
geo_logger.info({ pid: PID, class: caller_name, message: message }.merge(params)) geo_logger.info(base_log_data(message).merge(params))
end end
def self.error(message, params = {}) def error(message, params = {})
geo_logger.error({ pid: PID, class: caller_name, message: message }.merge(params)) geo_logger.error(base_log_data(message).merge(params))
end end
def self.debug(message, params = {}) def debug(message, params = {})
geo_logger.debug({ pid: PID, class: caller_name, message: message }.merge(params)) geo_logger.debug(base_log_data(message).merge(params))
end end
def self.geo_logger private
def geo_logger
Gitlab::Geo::Logger Gitlab::Geo::Logger
end end
def self.caller_name def caller_name
caller_locations[1].to_s.rpartition('/').last[/[a-z_]*/]&.classify klass.name
end end
def self.cursor_delay(created_at) def cursor_delay(created_at)
(Time.now - created_at).to_f.round(3) (Time.now - created_at).to_f.round(3)
end end
private_class_method :caller_name, :cursor_delay def base_log_data(message)
{
pid: PID,
class: caller_name,
message: message
}
end
end end
end end
end end
......
...@@ -7,15 +7,26 @@ describe Gitlab::Geo::LogCursor::Lease, :clean_gitlab_redis_shared_state do ...@@ -7,15 +7,26 @@ describe Gitlab::Geo::LogCursor::Lease, :clean_gitlab_redis_shared_state do
end end
end end
describe '.renew_lease!' do describe '.renew!' do
it 'returns an exclusive lease instance' do it 'returns an exclusive lease instance' do
expect_any_instance_of(Gitlab::ExclusiveLease).to receive(:renew) expect_any_instance_of(Gitlab::ExclusiveLease).to receive(:renew)
described_class.renew! described_class.renew!
end end
it 'logs with the correct caller class' do
stub_const("Gitlab::Geo::LogCursor::Logger::PID", 111)
allow_any_instance_of(Gitlab::ExclusiveLease).to receive(:renew).and_return(true)
expect(::Gitlab::Logger).to receive(:debug).with(pid: 111,
class: 'Gitlab::Geo::LogCursor::Lease',
message: 'Lease renewed.')
described_class.renew!
end
end end
describe '.try_obtain_lease' do describe '.try_obtain_lease_with_ttl' do
it 'returns zero when there is no lease' do it 'returns zero when there is no lease' do
result = described_class.try_obtain_with_ttl {} result = described_class.try_obtain_with_ttl {}
......
require 'spec_helper' require 'spec_helper'
describe Gitlab::Geo::LogCursor::Logger do describe Gitlab::Geo::LogCursor::Logger do
class LoggerSpec; end
subject(:logger) { described_class.new(LoggerSpec) }
before do before do
stub_const("#{described_class.name}::PID", 111) stub_const("#{described_class.name}::PID", 111)
end end
...@@ -10,7 +14,7 @@ describe Gitlab::Geo::LogCursor::Logger do ...@@ -10,7 +14,7 @@ describe Gitlab::Geo::LogCursor::Logger do
class: "LoggerSpec", class: "LoggerSpec",
message: 'Test') message: 'Test')
described_class.info('Test') logger.info('Test')
end end
it 'logs an error event' do it 'logs an error event' do
...@@ -18,7 +22,7 @@ describe Gitlab::Geo::LogCursor::Logger do ...@@ -18,7 +22,7 @@ describe Gitlab::Geo::LogCursor::Logger do
class: "LoggerSpec", class: "LoggerSpec",
message: 'Test') message: 'Test')
described_class.error('Test') logger.error('Test')
end end
describe '.event_info' do describe '.event_info' do
...@@ -28,7 +32,7 @@ describe Gitlab::Geo::LogCursor::Logger do ...@@ -28,7 +32,7 @@ describe Gitlab::Geo::LogCursor::Logger do
message: 'Test', message: 'Test',
cursor_delay_s: 0.0) cursor_delay_s: 0.0)
described_class.event_info(Time.now, 'Test') logger.event_info(Time.now, 'Test')
end end
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