Skip to content

Commit 34e5837

Browse files
committed
Initial Sidekiq 7 support
1 parent fcbb095 commit 34e5837

File tree

1 file changed

+56
-17
lines changed
  • lib/rails_semantic_logger/extensions/sidekiq

1 file changed

+56
-17
lines changed

lib/rails_semantic_logger/extensions/sidekiq/sidekiq.rb

Lines changed: 56 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,30 @@
1515
require "sidekiq/job_logger"
1616
require "sidekiq/logging"
1717
require "sidekiq/worker"
18-
elsif Sidekiq::VERSION.to_i > 5
18+
elsif Sidekiq::VERSION.to_i == 6
1919
require "sidekiq/exception_handler"
2020
require "sidekiq/job_logger"
2121
require "sidekiq/worker"
22+
else
23+
require "sidekiq/config"
24+
require "sidekiq/job_logger"
25+
require "sidekiq/worker"
2226
end
2327

2428
module Sidekiq
2529
if Sidekiq::VERSION.to_i > 4
2630
# Let Semantic Logger handle duration logging
2731
class JobLogger
2832
def call(item, queue)
29-
klass = item["wrapped"] || item["class"]
33+
klass = item["wrapped"] || item["class"]
3034
metric = "Sidekiq/#{klass}/perform" if klass
3135
logger = klass ? SemanticLogger[klass] : Sidekiq.logger
3236
logger.info("Start #perform")
3337
logger.measure_info(
3438
"Completed #perform",
3539
on_exception_level: :error,
36-
log_exception: :full,
37-
metric: metric
40+
log_exception: :full,
41+
metric: metric
3842
) do
3943
yield
4044
end
@@ -50,6 +54,16 @@ def prepare(job_hash, &block)
5054
SemanticLogger.tagged(job_hash_context(job_hash), &block)
5155
end
5256
end
57+
58+
def job_hash_context(job_hash)
59+
h = {
60+
class: job_hash["display_class"] || job_hash["wrapped"] || job_hash["class"],
61+
jid: job_hash["jid"]
62+
}
63+
h[:bid] = job_hash["bid"] if job_hash["bid"]
64+
h[:tags] = job_hash["tags"] if job_hash["tags"]
65+
h
66+
end
5367
end
5468
end
5569

@@ -67,20 +81,31 @@ def self.job_hash_context(job_hash)
6781
event
6882
end
6983
end
70-
end
7184

72-
# Exception is already logged by Semantic Logger during the perform call
73-
module ExceptionHandler
74-
class Logger
75-
def call(ex, ctx)
76-
Sidekiq.logger.warn(ctx) if !ctx.empty?
85+
# Exception is already logged by Semantic Logger during the perform call
86+
module ExceptionHandler
87+
class Logger
88+
def call(ex, ctx)
89+
Sidekiq.logger.warn(ctx) if !ctx.empty?
90+
end
7791
end
7892
end
7993
end
8094

81-
module Worker
82-
# Logging within each worker should use its own logger
83-
if Sidekiq::VERSION.to_i == 4
95+
if Sidekiq::VERSION.to_i >= 7
96+
module Config
97+
remove_const :ERROR_HANDLER
98+
99+
# Exception is already logged by Semantic Logger during the perform call
100+
ERROR_HANDLER = ->(ex, ctx, cfg = Sidekiq.default_configuration) {
101+
cfg.logger.warn(ctx) unless ctx.empty?
102+
}
103+
end
104+
end
105+
106+
# Logging within each worker should use its own logger
107+
if Sidekiq::VERSION.to_i == 4
108+
module Worker
84109
def self.included(base)
85110
raise ArgumentError, "You cannot include Sidekiq::Worker in an ActiveJob: #{base.name}" if base.ancestors.any? { |c| c.name == "ActiveJob::Base" }
86111

@@ -90,7 +115,9 @@ def self.included(base)
90115
base.class_attribute :sidekiq_retry_in_block
91116
base.class_attribute :sidekiq_retries_exhausted_block
92117
end
93-
elsif Sidekiq::VERSION.to_i == 5
118+
end
119+
elsif Sidekiq::VERSION.to_i == 5
120+
module Worker
94121
def self.included(base)
95122
raise ArgumentError, "You cannot include Sidekiq::Worker in an ActiveJob: #{base.name}" if base.ancestors.any? { |c| c.name == "ActiveJob::Base" }
96123

@@ -100,10 +127,22 @@ def self.included(base)
100127
base.sidekiq_class_attribute :sidekiq_retry_in_block
101128
base.sidekiq_class_attribute :sidekiq_retries_exhausted_block
102129
end
103-
elsif Sidekiq::VERSION.to_i > 5
130+
end
131+
elsif Sidekiq::VERSION.to_i == 6
132+
module Worker
104133
def self.included(base)
105134
raise ArgumentError, "Sidekiq::Worker cannot be included in an ActiveJob: #{base.name}" if base.ancestors.any? { |c| c.name == "ActiveJob::Base" }
106135

136+
base.include(Options)
137+
base.extend(ClassMethods)
138+
base.include(SemanticLogger::Loggable)
139+
end
140+
end
141+
else
142+
module Job
143+
def self.included(base)
144+
raise ArgumentError, "Sidekiq::Job cannot be included in an ActiveJob: #{base.name}" if base.ancestors.any? { |c| c.name == "ActiveJob::Base" }
145+
107146
base.include(Options)
108147
base.extend(ClassMethods)
109148
base.include(SemanticLogger::Loggable)
@@ -130,8 +169,8 @@ def call(worker, item, queue)
130169
worker.logger.measure_info(
131170
"Completed #perform",
132171
on_exception_level: :error,
133-
log_exception: :full,
134-
metric: "Sidekiq/#{worker.class.name}/perform"
172+
log_exception: :full,
173+
metric: "Sidekiq/#{worker.class.name}/perform"
135174
) do
136175
yield
137176
end

0 commit comments

Comments
 (0)