1
1
# Sidekiq patches
2
- #
3
- # To re-enable stdout logging for sidekiq server processes, add the following snippet to config/initializers/sidekiq.rb:
4
- # Sidekiq.configure_server do |config|
5
- # SemanticLogger.add_appender(io: $stdout, level: :debug, formatter: :color)
6
- # end
7
2
if Sidekiq ::VERSION . to_i == 4
8
- require "sidekiq/exception_handler"
9
3
require "sidekiq/logging"
10
4
require "sidekiq/middleware/server/logging"
11
5
require "sidekiq/processor"
12
- require "sidekiq/worker"
13
6
elsif Sidekiq ::VERSION . to_i == 5
14
- require "sidekiq/exception_handler"
15
- require "sidekiq/job_logger"
16
7
require "sidekiq/logging"
17
- require "sidekiq/worker"
18
- elsif Sidekiq ::VERSION . to_i == 6 && Sidekiq ::VERSION . to_f < 6.5
19
- require "sidekiq/exception_handler"
20
- require "sidekiq/job_logger"
21
- require "sidekiq/worker"
22
- elsif Sidekiq ::VERSION . to_i == 6
23
- require "sidekiq/job_logger"
24
- require "sidekiq/worker"
25
- else
26
- require "sidekiq/config"
27
- require "sidekiq/job_logger"
28
- require "sidekiq/job"
29
8
end
30
9
31
10
module Sidekiq
32
- # Sidekiq > v4
33
- if defined? ( ::Sidekiq ::JobLogger )
34
- # Let Semantic Logger handle duration logging
35
- class JobLogger
36
- def call ( item , queue , &block )
37
- klass = item [ "wrapped" ] || item [ "class" ]
38
- logger = klass ? SemanticLogger [ klass ] : Sidekiq . logger
39
-
40
- SemanticLogger . tagged ( queue : queue ) do
41
- # Latency is the time between when the job was enqueued and when it started executing.
42
- logger . info (
43
- "Start #perform" ,
44
- metric : "sidekiq.queue.latency" ,
45
- metric_amount : job_latency_ms ( item )
46
- )
47
-
48
- # Measure the duration of running the job
49
- logger . measure_info (
50
- "Completed #perform" ,
51
- on_exception_level : :error ,
52
- log_exception : :full ,
53
- metric : "sidekiq.job.perform" ,
54
- &block
55
- )
56
- end
57
- end
58
-
59
- def prepare ( job_hash , &block )
60
- level = job_hash [ "log_level" ]
61
- if level
62
- SemanticLogger . silence ( level ) do
63
- SemanticLogger . tagged ( job_hash_context ( job_hash ) , &block )
64
- end
65
- else
66
- SemanticLogger . tagged ( job_hash_context ( job_hash ) , &block )
67
- end
68
- end
69
-
70
- def job_hash_context ( job_hash )
71
- h = { jid : job_hash [ "jid" ] }
72
- h [ :bid ] = job_hash [ "bid" ] if job_hash [ "bid" ]
73
- h [ :tags ] = job_hash [ "tags" ] if job_hash [ "tags" ]
74
- h [ :queue ] = job_hash [ "queue" ] if job_hash [ "queue" ]
75
- h
76
- end
77
-
78
- def job_latency_ms ( job )
79
- return unless job && job [ "enqueued_at" ]
80
-
81
- ( Time . now . to_f - job [ "enqueued_at" ] . to_f ) * 1000
82
- end
83
- end
84
- end
85
-
86
- # Sidekiq <= v6
11
+ # Sidekiq v4 & v5
87
12
if defined? ( ::Sidekiq ::Logging )
88
13
# Replace Sidekiq logging context
89
14
module Logging
@@ -100,106 +25,8 @@ def self.job_hash_context(job_hash)
100
25
end
101
26
end
102
27
103
- # Exception is already logged by Semantic Logger during the perform call
104
- if defined? ( ::Sidekiq ::ExceptionHandler )
105
- # Sidekiq <= v6.5
106
- module ExceptionHandler
107
- class Logger
108
- def call ( _exception , ctx )
109
- return if ctx . empty?
110
-
111
- job_hash = ctx [ :job ] || { }
112
- klass = job_hash [ "display_class" ] || job_hash [ "wrapped" ] || job_hash [ "class" ]
113
- logger = klass ? SemanticLogger [ klass ] : Sidekiq . logger
114
- ctx [ :context ] ? logger . warn ( ctx [ :context ] , ctx ) : logger . warn ( ctx )
115
- end
116
- end
117
- end
118
- elsif defined? ( ::Sidekiq ::Config )
119
- # Sidekiq >= v7
120
- class Config
121
- remove_const :ERROR_HANDLER
122
-
123
- ERROR_HANDLER = -> ( ex , ctx , cfg = Sidekiq . default_configuration ) do
124
- unless ctx . empty?
125
- job_hash = ctx [ :job ] || { }
126
- klass = job_hash [ "display_class" ] || job_hash [ "wrapped" ] || job_hash [ "class" ]
127
- logger = klass ? SemanticLogger [ klass ] : Sidekiq . logger
128
- ctx [ :context ] ? logger . warn ( ctx [ :context ] , ctx ) : logger . warn ( ctx )
129
- end
130
- end
131
- end
132
- elsif Sidekiq . error_handlers . delete ( Sidekiq ::DEFAULT_ERROR_HANDLER )
133
- # Sidekiq >= 6.5
134
- # Replace default error handler if present
135
- Sidekiq . error_handlers << -> ( ex , ctx ) do
136
- unless ctx . empty?
137
- job_hash = ctx [ :job ] || { }
138
- klass = job_hash [ "display_class" ] || job_hash [ "wrapped" ] || job_hash [ "class" ]
139
- logger = klass ? SemanticLogger [ klass ] : Sidekiq . logger
140
- ctx [ :context ] ? logger . warn ( ctx [ :context ] , ctx ) : logger . warn ( ctx )
141
- end
142
- end
143
- end
144
-
145
- # Logging within each worker should use its own logger
146
- case Sidekiq ::VERSION . to_i
147
- when 4
148
- module Worker
149
- def self . included ( base )
150
- if base . ancestors . any? { |c | c . name == "ActiveJob::Base" }
151
- raise ArgumentError , "You cannot include Sidekiq::Worker in an ActiveJob: #{ base . name } "
152
- end
153
-
154
- base . extend ( ClassMethods )
155
- base . include ( SemanticLogger ::Loggable )
156
- base . class_attribute :sidekiq_options_hash
157
- base . class_attribute :sidekiq_retry_in_block
158
- base . class_attribute :sidekiq_retries_exhausted_block
159
- end
160
- end
161
- when 5
162
- module Worker
163
- def self . included ( base )
164
- if base . ancestors . any? { |c | c . name == "ActiveJob::Base" }
165
- raise ArgumentError , "You cannot include Sidekiq::Worker in an ActiveJob: #{ base . name } "
166
- end
167
-
168
- base . extend ( ClassMethods )
169
- base . include ( SemanticLogger ::Loggable )
170
- base . sidekiq_class_attribute :sidekiq_options_hash
171
- base . sidekiq_class_attribute :sidekiq_retry_in_block
172
- base . sidekiq_class_attribute :sidekiq_retries_exhausted_block
173
- end
174
- end
175
- when 6
176
- module Worker
177
- def self . included ( base )
178
- if base . ancestors . any? { |c | c . name == "ActiveJob::Base" }
179
- raise ArgumentError , "Sidekiq::Worker cannot be included in an ActiveJob: #{ base . name } "
180
- end
181
-
182
- base . include ( Options )
183
- base . extend ( ClassMethods )
184
- base . include ( SemanticLogger ::Loggable )
185
- end
186
- end
187
- else
188
- module Job
189
- def self . included ( base )
190
- if base . ancestors . any? { |c | c . name == "ActiveJob::Base" }
191
- raise ArgumentError , "Sidekiq::Job cannot be included in an ActiveJob: #{ base . name } "
192
- end
193
-
194
- base . include ( Options )
195
- base . extend ( ClassMethods )
196
- base . include ( SemanticLogger ::Loggable )
197
- end
198
- end
199
- end
200
-
28
+ # Sidekiq v4
201
29
if defined? ( ::Sidekiq ::Middleware ::Server ::Logging )
202
- # Sidekiq v4
203
30
# Convert string to machine readable format
204
31
class Processor
205
32
def log_context ( job_hash )
@@ -214,6 +41,7 @@ def log_context(job_hash)
214
41
module Middleware
215
42
module Server
216
43
class Logging
44
+ # rubocop:disable Style/ExplicitBlockArgument
217
45
def call ( worker , item , queue )
218
46
SemanticLogger . tagged ( queue : queue ) do
219
47
worker . logger . info (
0 commit comments