Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

logging controls on a per-worker basis #1521

Closed
wants to merge 1 commit into from

3 participants

@scootklein

Refactored as per our previous discussions. Is there anything you'd like me to add to to the wiki/readme for how to use this functionality?

Also, would monkeypatching be the best way to inject into Sidekiq::Extensions::DelayedClass?

module Sidekiq
  module Extensions
    class DelayedClass
      sidekiq_logger do |logger|
        logger.level = Logger::WARN
        logger.execution_threshold = 5
      end
    end
  end
end
@mperham
Owner

It's been months since we chatted. Could you summarize the changes here and what you're looking to accomplish? I'm old and my memory is fading...

@scootklein

We're running > 5k background jobs per minute, most of which are ephemeral and we don't need logging for. Paying for a hosted rsyslog service can get costly when most of the chatter from Sidekiq isn't necessary. The PR lets you customize the logger per-worker, to raise the log level (WARN only shows failures), and to optionally set an execution threshold to help debug jobs or queues that are taking a long time to complete.

@jonhyman
Collaborator

What we did was remove the middleware that adds the "started" and "finished" logging and kept the log level at info for all workers. We do about 50-100k jobs per minute during normal daily peak load and had the same motivation regarding cloud logging. IMO I don't know if this is needed, instead just do

config.server_middleware do |chain|
  chain.remove(Sidekiq::Middleware::Server::Logging)
end
@scootklein

I'd rather not remove all of the logging, seems that could introduce unintended consequences and will give us less visibility, especially for jobs or queues that are causing congestion among the worker pool.

@jonhyman
Collaborator
@jonhyman
Collaborator
@mperham
Owner

I've gotta side with Jon here. This feels overly complicated for Sidekiq core.

@mperham mperham closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 1, 2014
  1. logging controls on a per-worker basis

    Scott Klein authored
This page is out of date. Refresh to see the latest.
View
5 lib/sidekiq/logging.rb
@@ -1,6 +1,11 @@
require 'time'
require 'logger'
+# new attribute for Logger, don't log if not over execution_threshold
+class Logger
+ attr_accessor :execution_threshold
+end
+
module Sidekiq
module Logging
View
18 lib/sidekiq/middleware/server/logging.rb
@@ -7,11 +7,19 @@ def call(worker, item, queue)
Sidekiq::Logging.with_context("#{worker.class.to_s} JID-#{item['jid']}") do
begin
start = Time.now
- logger.info { "start" }
+ worker.logger.info { "start" }
yield
- logger.info { "done: #{elapsed(start)} sec" }
+
+ elapsed = elapsed(start)
+
+ # if execution_threshold is set, let's respect it with WARN on too long of job
+ if worker.logger.execution_threshold && worker.logger.execution_threshold.to_f < elapsed
+ worker.logger.warn { "done: #{elapsed} sec" }
+ else
+ worker.logger.info { "done: #{elapsed} sec" }
+ end
rescue Exception
- logger.info { "fail: #{elapsed(start)} sec" }
+ worker.logger.warn { "fail: #{elapsed(start)} sec" }
raise
end
end
@@ -20,10 +28,6 @@ def call(worker, item, queue)
def elapsed(start)
(Time.now - start).to_f.round(3)
end
-
- def logger
- Sidekiq.logger
- end
end
end
end
View
12 lib/sidekiq/worker.rb
@@ -28,10 +28,16 @@ def self.included(base)
base.class_attribute :sidekiq_options_hash
base.class_attribute :sidekiq_retry_in_block
base.class_attribute :sidekiq_retries_exhausted_block
+ base.class_attribute :sidekiq_logger_block
end
def logger
- Sidekiq.logger
+ unless @logger
+ @logger = Sidekiq.logger.dup
+ self.class.sidekiq_logger_block.try(:call, @logger)
+ end
+
+ @logger
end
module ClassMethods
@@ -75,6 +81,10 @@ def sidekiq_retries_exhausted(&block)
self.sidekiq_retries_exhausted_block = block
end
+ def sidekiq_logger(&block)
+ self.sidekiq_logger_block = block
+ end
+
def get_sidekiq_options # :nodoc:
self.sidekiq_options_hash ||= Sidekiq.default_worker_options
end
Something went wrong with that request. Please try again.