class Sidekiq::LogJsonFormatter

Public Instance Methods

call(severity, time, program_name, message) click to toggle source
# File lib/sidekiq/log_json_formatter.rb, line 5
def call(severity, time, program_name, message)
  hash = {
    '@timestamp' => time.utc.iso8601,
    '@fields' => {
      pid: ::Process.pid,
      tid: "TID-#{tid}",
      context: ctx,
      program_name: program_name,
      worker: worker,
    },
    '@type' => 'sidekiq',
    '@status' => nil,
    '@severity' => severity,
    '@run_time' => nil,
  }.merge(process_message(message))

  Sidekiq.dump_json(hash) << "\n"
end

Private Instance Methods

process_message(message) click to toggle source
# File lib/sidekiq/log_json_formatter.rb, line 32
def process_message(message)
  case message
  when Exception
    {
      '@status' => 'exception',
      '@message' => message.message
    }
  when Hash
    if message['retry']
      {
        '@status' => 'retry',
        '@message' => "#{message['class']} failed, retrying with args #{message['args']}."
      }
    else
      {
        '@status' => 'dead',
        '@message' => "#{message['class']} failed with args #{message['args']}, not retrying."
      }
    end
  else
    {
      '@status' => status(message),
      '@run_time' => ctx[:elapsed] && ctx[:elapsed].to_f, # run time in seconds
      '@message' => message
    }
  end
end
status(message) click to toggle source
# File lib/sidekiq/log_json_formatter.rb, line 60
def status(message)
  if %w[start done fail].any?(message)
    message
  else
    nil
  end
end
worker() click to toggle source
# File lib/sidekiq/log_json_formatter.rb, line 26
def worker
  return nil if ctx == {}

  ctx[:class]
end