Skip to content

Commit fba6cde

Browse files
authored
Merge pull request #109 from FundingCircle/loga-sidekiq-integration
Loga Sidekiq integration
2 parents 7b18e21 + ba2b19c commit fba6cde

17 files changed

Lines changed: 438 additions & 4 deletions

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,5 @@ spec/fixtures/**/*.log
2525
gemfiles/*.lock
2626
vendor/bundle
2727
.byebug_history
28+
.DS_Store
29+

.rubocop.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ AllCops:
44
- 'gemfiles/*'
55
- 'spec/fixtures/**/*'
66
- 'vendor/bundle/**/*'
7+
- 'gemfiles/vendor/bundle/**/*'
78

89
require: rubocop-rspec
910

Appraisals

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,9 @@ appraise 'rails52' do
2424
gem 'rails', '~> 5.2.0'
2525
end
2626

27+
appraise 'sidekiq51' do
28+
gem 'sidekiq', '~> 5.1.0'
29+
end
30+
2731
appraise 'unit' do
2832
end

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](http://keepachangelog.com/)
55
and this project adheres to [Semantic Versioning](http://semver.org/).
66

7+
## [2.3.0] - 2018-06-29
8+
### Added
9+
Support for Sidekiq `~> 5.0`.
10+
711
## [2.2.0] - 2018-05-10
812
### Added
913
Subscribe to `ActionMailer` events

Guardfile

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,20 @@ group :rails do
3939
end
4040
end
4141

42+
group :sidekiq do
43+
cmd = 'bundle exec appraisal sidekiq51 rspec'
44+
45+
guard :rspec, all_on_start: true, cmd: cmd do
46+
watch('lib/loga/sidekiq/job_logger.rb') do
47+
[
48+
'spec/integration/sidekiq_spec.rb',
49+
'spec/loga/sidekiq/job_logger_spec.rb',
50+
'spec/loga/sidekiq_spec.rb',
51+
]
52+
end
53+
end
54+
end
55+
4256
group :unit do
4357
guard :rspec, cmd: 'bundle exec appraisal unit rspec' do
4458
watch(%r{^spec/unit/.+_spec\.rb$})

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,10 @@ environment variable therefore it must be removed.
148148
LOGA_FORMAT=simple rackup
149149
```
150150

151+
### Sidekiq
152+
153+
Loga `2.3` provides an out-of-the-box support for `Sidekiq ~> 5.0`.
154+
151155
## Output Example
152156

153157
### GELF Format

gemfiles/sidekiq51.gemfile

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# This file was generated by Appraisal
2+
3+
source "https://rubygems.org"
4+
5+
gem "sidekiq", "~> 5.1.0"
6+
7+
group :test do
8+
gem "simplecov"
9+
end
10+
11+
gemspec path: "../"

lib/loga.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
require 'loga/rack/request'
99
require 'loga/rack/request_id'
1010
require 'loga/railtie' if defined?(Rails)
11+
require 'loga/sidekiq'
1112

12-
# rubocop:disable Naming/MemoizedInstanceVariableName
1313
module Loga
1414
ConfigurationError = Class.new(StandardError)
1515

@@ -26,6 +26,8 @@ def self.configure(options, framework_options = {})
2626
raise ConfigurationError, 'Loga has already been configured' if @configuration
2727

2828
@configuration ||= Configuration.new(options, framework_options)
29+
30+
Loga::Sidekiq.configure_logging
2931
end
3032

3133
def self.logger
@@ -36,4 +38,3 @@ def self.reset
3638
@configuration = nil
3739
end
3840
end
39-
# rubocop:enable Naming/MemoizedInstanceVariableName

lib/loga/sidekiq.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
require 'loga/sidekiq/job_logger'
2+
3+
module Loga
4+
module Sidekiq
5+
def self.configure_logging
6+
return unless defined?(::Sidekiq)
7+
return if Gem::Version.new(::Sidekiq::VERSION) < Gem::Version.new('5.0')
8+
9+
::Sidekiq.configure_server do |config|
10+
config.options[:job_logger] = Loga::Sidekiq::JobLogger
11+
end
12+
13+
::Sidekiq::Logging.logger = Loga.configuration.logger
14+
end
15+
end
16+
end

lib/loga/sidekiq/job_logger.rb

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
module Loga
2+
module Sidekiq
3+
# The approach of using a custom job logger in sidekiq was introduced
4+
# in v5.0: https://github.com/mperham/sidekiq/pull/3235
5+
# This job logger does not support logging for Sidekiq versions
6+
# that are before 5.0
7+
class JobLogger
8+
include Loga::Utilities
9+
10+
EVENT_TYPE = 'sidekiq'.freeze
11+
12+
attr_reader :started_at, :data
13+
14+
def initialize
15+
@started_at = Time.now
16+
@data = {}
17+
end
18+
19+
def call(item, _queue)
20+
yield
21+
rescue Exception => ex # rubocop:disable Lint/RescueException
22+
data['exception'] = ex
23+
24+
raise
25+
ensure
26+
assign_data(item)
27+
send_message
28+
end
29+
30+
private
31+
32+
def assign_data(item)
33+
data['created_at'] = item['created_at']
34+
data['enqueued_at'] = item['enqueued_at']
35+
data['jid'] = item['jid']
36+
data['queue'] = item['queue']
37+
data['retry'] = item['retry']
38+
data['params'] = item['args']
39+
data['class'] = item['class']
40+
data['duration'] = duration_in_ms(started_at)
41+
end
42+
43+
def short_message
44+
"#{data['class']} with jid: '#{data['jid']}' executed in #{data['duration']}ms"
45+
end
46+
47+
def send_message
48+
event = Event.new(data: data, message: short_message, type: EVENT_TYPE)
49+
50+
logger.public_send(compute_level, event)
51+
end
52+
53+
def compute_level
54+
data.key?('exception') ? :warn : :info
55+
end
56+
57+
def logger
58+
Loga.logger
59+
end
60+
end
61+
end
62+
end

0 commit comments

Comments
 (0)