-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #109 from FundingCircle/loga-sidekiq-integration
Loga Sidekiq integration
- Loading branch information
Showing
17 changed files
with
438 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,3 +25,5 @@ spec/fixtures/**/*.log | |
gemfiles/*.lock | ||
vendor/bundle | ||
.byebug_history | ||
.DS_Store | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# This file was generated by Appraisal | ||
|
||
source "https://rubygems.org" | ||
|
||
gem "sidekiq", "~> 5.1.0" | ||
|
||
group :test do | ||
gem "simplecov" | ||
end | ||
|
||
gemspec path: "../" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
require 'loga/sidekiq/job_logger' | ||
|
||
module Loga | ||
module Sidekiq | ||
def self.configure_logging | ||
return unless defined?(::Sidekiq) | ||
return if Gem::Version.new(::Sidekiq::VERSION) < Gem::Version.new('5.0') | ||
|
||
::Sidekiq.configure_server do |config| | ||
config.options[:job_logger] = Loga::Sidekiq::JobLogger | ||
end | ||
|
||
::Sidekiq::Logging.logger = Loga.configuration.logger | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
module Loga | ||
module Sidekiq | ||
# The approach of using a custom job logger in sidekiq was introduced | ||
# in v5.0: https://github.com/mperham/sidekiq/pull/3235 | ||
# This job logger does not support logging for Sidekiq versions | ||
# that are before 5.0 | ||
class JobLogger | ||
include Loga::Utilities | ||
|
||
EVENT_TYPE = 'sidekiq'.freeze | ||
|
||
attr_reader :started_at, :data | ||
|
||
def initialize | ||
@started_at = Time.now | ||
@data = {} | ||
end | ||
|
||
def call(item, _queue) | ||
yield | ||
rescue Exception => ex # rubocop:disable Lint/RescueException | ||
data['exception'] = ex | ||
|
||
raise | ||
ensure | ||
assign_data(item) | ||
send_message | ||
end | ||
|
||
private | ||
|
||
def assign_data(item) | ||
data['created_at'] = item['created_at'] | ||
data['enqueued_at'] = item['enqueued_at'] | ||
data['jid'] = item['jid'] | ||
data['queue'] = item['queue'] | ||
data['retry'] = item['retry'] | ||
data['params'] = item['args'] | ||
data['class'] = item['class'] | ||
data['duration'] = duration_in_ms(started_at) | ||
end | ||
|
||
def short_message | ||
"#{data['class']} with jid: '#{data['jid']}' executed in #{data['duration']}ms" | ||
end | ||
|
||
def send_message | ||
event = Event.new(data: data, message: short_message, type: EVENT_TYPE) | ||
|
||
logger.public_send(compute_level, event) | ||
end | ||
|
||
def compute_level | ||
data.key?('exception') ? :warn : :info | ||
end | ||
|
||
def logger | ||
Loga.logger | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
module Loga | ||
VERSION = '2.2.0'.freeze | ||
VERSION = '2.3.0'.freeze | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
require 'spec_helper' | ||
require 'timecop' | ||
require 'fakeredis' | ||
|
||
dummy_redis_config = ConnectionPool.new(size: 5) { Redis.new } | ||
|
||
Sidekiq.configure_client do |config| | ||
config.redis = dummy_redis_config | ||
end | ||
|
||
Sidekiq.configure_server do |config| | ||
config.redis = dummy_redis_config | ||
end | ||
|
||
class MySidekiqWorker | ||
include Sidekiq::Worker | ||
|
||
def perform(_name); end | ||
end | ||
|
||
describe 'Sidekiq client logger' do | ||
let(:target) { StringIO.new } | ||
|
||
let(:json_line) do | ||
target.rewind | ||
JSON.parse(target.read) | ||
end | ||
|
||
before do | ||
Redis.current.flushall | ||
|
||
Loga.reset | ||
|
||
Loga.configure( | ||
service_name: 'hello_world_app', | ||
service_version: '1.0', | ||
device: target, | ||
format: :gelf, | ||
) | ||
end | ||
|
||
it 'has the proper job logger' do | ||
job_logger = Loga::Sidekiq::JobLogger | ||
|
||
expect(Sidekiq.options[:job_logger]).to eq job_logger | ||
end | ||
|
||
it 'has the proper logger Sidekiq::Logging.logger' do | ||
expect(Sidekiq::Logging.logger).to eq Loga.logger | ||
end | ||
|
||
it 'has the proper logger for Sidekiq.logger' do | ||
expect(Sidekiq.logger).to eq Loga.logger | ||
end | ||
|
||
it 'pushes a new element in the default queue' do | ||
MySidekiqWorker.perform_async('Bob') | ||
|
||
last_element = JSON.parse(Redis.current.lpop('queue:default')) | ||
|
||
aggregate_failures do | ||
expect(last_element['class']).to eq 'MySidekiqWorker' | ||
expect(last_element['args']).to eq ['Bob'] | ||
expect(last_element['retry']).to eq true | ||
expect(last_element['queue']).to eq 'default' | ||
end | ||
end | ||
|
||
if ENV['BUNDLE_GEMFILE'] =~ /sidekiq51/ | ||
# https://github.com/mperham/sidekiq/blob/97363210b47a4f8a1d8c1233aaa059d6643f5040/test/test_actors.rb#L57-L79 | ||
let(:mgr) do | ||
Class.new do | ||
attr_reader :latest_error, :mutex, :cond | ||
|
||
def initialize | ||
@mutex = ::Mutex.new | ||
@cond = ::ConditionVariable.new | ||
end | ||
|
||
def processor_died(_inst, err) | ||
@latest_error = err | ||
|
||
@mutex.synchronize { @cond.signal } | ||
end | ||
|
||
def processor_stopped(_inst) | ||
@mutex.synchronize { @cond.signal } | ||
end | ||
|
||
def options | ||
{ | ||
concurrency: 3, | ||
queues: ['default'], | ||
job_logger: Loga::Sidekiq::JobLogger, | ||
} | ||
end | ||
end | ||
end | ||
|
||
it 'logs the job processing event' do | ||
MySidekiqWorker.perform_async('Bob') | ||
|
||
require 'sidekiq/processor' | ||
Sidekiq::Processor.new(mgr.new).start | ||
sleep 0.5 | ||
|
||
expected_attributes = { | ||
'_queue'=> 'default', | ||
'_retry'=> true, | ||
'_params'=> ['Bob'], | ||
'_class'=> 'MySidekiqWorker', | ||
'_type'=> 'sidekiq', | ||
'_service.name'=> 'hello_world_app', | ||
'_service.version'=> '1.0', | ||
'_tags'=> '', | ||
'level'=> 6, | ||
'version'=> '1.1', | ||
} | ||
|
||
aggregate_failures do | ||
expect(json_line).to include(expected_attributes) | ||
|
||
%w[_created_at _enqueued_at _jid _duration timestamp host].each do |key| | ||
expect(json_line).to have_key(key) | ||
end | ||
|
||
expect(json_line['short_message']).to match(/MySidekiqWorker with jid:*/) | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.