diff --git a/lib/logtail-rails.rb b/lib/logtail-rails.rb index 2f01fcf..40048e3 100755 --- a/lib/logtail-rails.rb +++ b/lib/logtail-rails.rb @@ -21,6 +21,7 @@ require "logtail-rails/action_dispatch" require "logtail-rails/action_view" require "logtail-rails/active_record" +require "logtail-rails/sidekiq" require "logtail-rails/log_entry" require "logtail-rails/logger" diff --git a/lib/logtail-rails/logger.rb b/lib/logtail-rails/logger.rb index 24ee0dc..32df5e9 100755 --- a/lib/logtail-rails/logger.rb +++ b/lib/logtail-rails/logger.rb @@ -32,7 +32,16 @@ def self.create_default_logger(source_token) io_device = STDOUT end - self.create_logger(io_device) + logger = self.create_logger(io_device) + + if defined?(Sidekiq) + Sidekiq.configure_server do |config| + logger = self.create_logger(io_device, config.logger) if config.logger.class == Sidekiq::Logger + config.logger = logger + end + end + + logger end end end diff --git a/lib/logtail-rails/sidekiq.rb b/lib/logtail-rails/sidekiq.rb new file mode 100755 index 0000000..865acc6 --- /dev/null +++ b/lib/logtail-rails/sidekiq.rb @@ -0,0 +1,38 @@ +# integrates Logtail::CurrentContext with Sidekiq::Context if installed + +begin + require 'sidekiq/job_logger' + + class Sidekiq::JobLogger + def call(_item, _queue) + start = Process.clock_gettime(Process::CLOCK_MONOTONIC) + + Logtail::CurrentContext.with({ sidekiq: Sidekiq::Context.current.to_h }) do + @logger.info("start") + + yield + end + + Sidekiq::Context.add(:elapsed, elapsed(start)) + Logtail::CurrentContext.with({ sidekiq: Sidekiq::Context.current.to_h }) do + @logger.info('done') + end + rescue Exception + Sidekiq::Context.add(:elapsed, elapsed(start)) + Logtail::CurrentContext.with({ sidekiq: Sidekiq::Context.current.to_h }) do + @logger.info('fail') + end + + raise + end + + private + + def elapsed(start) + (Process.clock_gettime(::Process::CLOCK_MONOTONIC) - start).round(3) + end + end + +rescue LoadError + # sidekiq is not present +end diff --git a/lib/logtail-rails/version.rb b/lib/logtail-rails/version.rb index b5e9897..6e2e0e7 100755 --- a/lib/logtail-rails/version.rb +++ b/lib/logtail-rails/version.rb @@ -1,7 +1,7 @@ module Logtail module Integrations module Rails - VERSION = "0.2.2" + VERSION = "0.2.3" end end end