Sha256: 28ede5a9f6cef602e99efdf3f641fd4f325e3a0d111d39710d3fa1ddb82ee455

Contents?: true

Size: 932 Bytes

Versions: 4

Compression:

Stored size: 932 Bytes

Contents

# 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

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
logtail-rails-0.2.6 lib/logtail-rails/sidekiq.rb
logtail-rails-0.2.5 lib/logtail-rails/sidekiq.rb
logtail-rails-0.2.4 lib/logtail-rails/sidekiq.rb
logtail-rails-0.2.3 lib/logtail-rails/sidekiq.rb