What is the best way to print out a custom attribute added on a middleware after Sidekiq 6.4.1 #5343
-
Hi, I would appreciate if anyone can guide me how to print out a custom attribute added on a middleware. module SidekiqMiddleware
module Client
class Hello
def call(_worker_class, job, _queue, _redis_pool)
job["hello"] = "world"
yield
end
end
end
....
end I registered it together with a custom logger. Sidekiq.configure_server do |config|
config.client_middleware do |chain|
chain.add SidekiqMiddleware::Client::Hello
end
config.server_middleware do |chain|
chain.add SidekiqMiddleware::Server::Hello
end
config.log_formatter = Sidekiq::Logger::Formatters::JSON.new
config.options[:job_logger] = SidekiqJobLoggerWithHello
SidekiqUniqueJobs::Server.configure(config)
end And the custom logger looks like this. require "sidekiq/job_logger"
class SidekiqJobLoggerWithHello < Sidekiq::JobLogger
def job_hash_context(job_hash)
original_job_hash = super
original_job_hash.merge("hello" => job_hash["hello"])
end
end This code works before, but after upgrading to 6.4.2, it didn't work. I saw that there is this change https://github.com/mperham/sidekiq/blob/00ae6a74234a9b626a593ae6c5620416349f8eaf/lib/sidekiq/job_logger.rb#L24 introduced in 6.4.1 which removed |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
You don't need a custom job logger at all. In your server middleware, you'd just add: Sidekiq::Context.with("hello" => job["hello"], &block) |
Beta Was this translation helpful? Give feedback.
-
Hi @mperham thanks for the suggestion. Really appreciated. module SidekiqMiddleware
module Client
class Hello
def call(_worker_class, job, _queue, _redis_pool)
job["hello"] = "world"
yield
end
end
end
module Server
class Hello
def call(worker, job, _queue)
Sidekiq::Context.with("hello" => "world") do
Sidekiq.logger.info
end
yield
end
end
end
end Then, when i trigger the job, I can see the log as follows.
I can see that the |
Beta Was this translation helpful? Give feedback.
You don't need a custom job logger at all. In your server middleware, you'd just add: