Skip to content

Commit

Permalink
Integrate with Sidekiq if installed (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
PetrHeinz authored Jul 14, 2023
1 parent af2559b commit bd29d34
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 2 deletions.
1 change: 1 addition & 0 deletions lib/logtail-rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
11 changes: 10 additions & 1 deletion lib/logtail-rails/logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
38 changes: 38 additions & 0 deletions lib/logtail-rails/sidekiq.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion lib/logtail-rails/version.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Logtail
module Integrations
module Rails
VERSION = "0.2.2"
VERSION = "0.2.3"
end
end
end

0 comments on commit bd29d34

Please sign in to comment.