Class: Appydays::Loggable::SidekiqJobLogger

Inherits:
Sidekiq::JobLogger
  • Object
show all
Includes:
Configurable, Appydays::Loggable, Sidekiq::Component
Defined in:
lib/appydays/loggable/sidekiq_job_logger.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Appydays::Loggable

[], configure_12factor, default_level=, ensure_stderr_appender, included, set_default_level, with_log_tags

Methods included from Configurable

included

Instance Attribute Details

#log_level_job_doneObject

Level to log ‘job_done’ messages at. Defaults to :info.



18
19
20
# File 'lib/appydays/loggable/sidekiq_job_logger.rb', line 18

def log_level_job_done
  @log_level_job_done
end

#log_level_slow_jobObject

Level to log slow jobs at. Defaults to :warn.



22
23
24
# File 'lib/appydays/loggable/sidekiq_job_logger.rb', line 22

def log_level_slow_job
  @log_level_slow_job
end

Class Method Details

.death_handler(job, ex) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/appydays/loggable/sidekiq_job_logger.rb', line 116

def self.death_handler(job, ex)
  self.logger.error(
    "job_retries_exhausted",
    {
      job_class: job["class"],
      job_args: job["args"],
      job_retry: job["retry"],
      job_queue: job["queue"],
      job_dead: job["dead"],
      job_id: job["jid"],
      job_created_at: job["created_at"],
      job_enqueued_at: job["enqueued_at"],
      job_error_message: job["error_message"],
      job_error_class: job["error_class"],
      job_failed_at: job["failed_at"],
      job_retry_count: job["retry_count"],
    },
    ex,
  )
end

.error_handler(ex, ctx, _config) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/appydays/loggable/sidekiq_job_logger.rb', line 76

def self.error_handler(ex, ctx, _config)
  # ctx looks like:
  # {
  # :context=>"Job raised exception",
  # :job=>
  #  {"class"=>"App::Async::FailingJobTester",
  #   "args"=>
  #    [{"id"=>"e8e03571-9851-4daa-a801-a0b43282f317",
  #      "name"=>"app.test_failing_job",
  #      "payload"=>[true]}],
  #   "retry"=>true,
  #   "queue"=>"default",
  #   "jid"=>"cb00c4fe9b2f16b72797d35c",
  #   "created_at"=>1567811837.798969,
  #   "enqueued_at"=>1567811837.79901},
  # :jobstr=>
  #  "{\"class\":\"App::Async::FailingJobTester\", <etc>"
  # }
  job = ctx[:job]
  # If there was a connection error, you may end up with no job context.
  # It's very difficult to test this usefully, so it's not tested.
  unless job
    self.logger.error("job_error_no_job", {}, ex)
    return
  end
  self.logger.error(
    "job_error",
    {
      job_class: job["class"],
      job_args: job["args"],
      job_retry: job["retry"],
      job_queue: job["queue"],
      job_id: job["jid"],
      job_created_at: job["created_at"],
      job_enqueued_at: job["enqueued_at"],
    },
    ex,
  )
end

.job_tagsObject



74
# File 'lib/appydays/loggable/sidekiq_job_logger.rb', line 74

def self.job_tags = Thread.current[:appydays_sidekiq_job_logger_job_tags] || {}

.set_job_tags(tags) ⇒ Object

Set job tags that get logged out in the “job_done” and “job_fail” messages. See README for more info. We do NOT merge the job_tags in with critical errors (death and job_error), since those will log the job args, and they aren’t properly tested right now. We may add support in the future.



69
70
71
72
# File 'lib/appydays/loggable/sidekiq_job_logger.rb', line 69

def self.set_job_tags(tags)
  Thread.current[:appydays_sidekiq_job_logger_job_tags] ||= {}
  Thread.current[:appydays_sidekiq_job_logger_job_tags].merge!(tags)
end

Instance Method Details

#call(item, _queue) ⇒ Object



24
25
26
27
28
29
# File 'lib/appydays/loggable/sidekiq_job_logger.rb', line 24

def call(item, _queue, &)
  start = self.now
  self.with_log_tags(job_id: item["jid"]) do
    self.call_inner(item, start, &)
  end
end