1
0
Fork 0
mirror of https://github.com/mperham/sidekiq.git synced 2022-11-09 13:52:34 -05:00

Tweak job logger to reduce overhead, 3-4% speedup

This commit is contained in:
Mike Perham 2022-01-26 16:57:43 -08:00
parent 255b492725
commit 01686c5e25
3 changed files with 20 additions and 28 deletions

View file

@ -86,7 +86,7 @@ def Process.rss
`ps -o rss= -p #{Process.pid}`.chomp.to_i `ps -o rss= -p #{Process.pid}`.chomp.to_i
end end
iter = 10 iter = 50
count = 10_000 count = 10_000
iter.times do iter.times do

View file

@ -12,46 +12,34 @@ module Sidekiq
yield yield
with_elapsed_time_context(start) do Sidekiq::Context.add(:elapsed, elapsed(start))
@logger.info("done") @logger.info("done")
end
rescue Exception rescue Exception
with_elapsed_time_context(start) do Sidekiq::Context.add(:elapsed, elapsed(start))
@logger.info("fail") @logger.info("fail")
end
raise raise
end end
def prepare(job_hash, &block) def prepare(job_hash, &block)
level = job_hash["log_level"]
if level
@logger.log_at(level) do
Sidekiq::Context.with(job_hash_context(job_hash), &block)
end
else
Sidekiq::Context.with(job_hash_context(job_hash), &block)
end
end
def job_hash_context(job_hash)
# If we're using a wrapper class, like ActiveJob, use the "wrapped" # If we're using a wrapper class, like ActiveJob, use the "wrapped"
# attribute to expose the underlying thing. # attribute to expose the underlying thing.
h = { h = {
class: job_hash["display_class"] || job_hash["wrapped"] || job_hash["class"], class: job_hash["display_class"] || job_hash["wrapped"] || job_hash["class"],
jid: job_hash["jid"] jid: job_hash["jid"]
} }
h[:bid] = job_hash["bid"] if job_hash["bid"] h[:bid] = job_hash["bid"] if job_hash.has_key?("bid")
h[:tags] = job_hash["tags"] if job_hash["tags"] h[:tags] = job_hash["tags"] if job_hash.has_key?("tags")
h
end
def with_elapsed_time_context(start, &block) Thread.current[:sidekiq_context] = h
Sidekiq::Context.with(elapsed_time_context(start), &block) level = job_hash["log_level"]
end if level
@logger.log_at(level, &block)
def elapsed_time_context(start) else
{elapsed: elapsed(start).to_s} yield
end
ensure
Thread.current[:sidekiq_context] = nil
end end
private private

View file

@ -16,6 +16,10 @@ module Sidekiq
def self.current def self.current
Thread.current[:sidekiq_context] ||= {} Thread.current[:sidekiq_context] ||= {}
end end
def self.add(k, v)
Thread.current[:sidekiq_context][k] = v
end
end end
module LoggingUtils module LoggingUtils