Sha256: 018defebf76e093fe38c8eb29d904290bfb0d57fb2785ca5aea3319a6ab8cb6c

Contents?: true

Size: 1.92 KB

Versions: 1

Compression:

Stored size: 1.92 KB

Contents

# Sidekiq patches
if Sidekiq::VERSION.to_i == 4
  require "sidekiq/logging"
  require "sidekiq/middleware/server/logging"
  require "sidekiq/processor"
elsif Sidekiq::VERSION.to_i == 5
  require "sidekiq/logging"
end

module Sidekiq
  # Sidekiq v4 & v5
  if defined?(::Sidekiq::Logging)
    # Replace Sidekiq logging context
    module Logging
      def self.with_context(msg, &block)
        SemanticLogger.tagged(msg, &block)
      end

      def self.job_hash_context(job_hash)
        h         = {jid: job_hash["jid"]}
        h[:bid]   = job_hash["bid"] if job_hash["bid"]
        h[:queue] = job_hash["queue"] if job_hash["queue"]
        h
      end
    end
  end

  # Sidekiq v4
  if defined?(::Sidekiq::Middleware::Server::Logging)
    # Convert string to machine readable format
    class Processor
      def log_context(job_hash)
        h         = {jid: job_hash["jid"]}
        h[:bid]   = job_hash["bid"] if job_hash["bid"]
        h[:queue] = job_hash["queue"] if job_hash["queue"]
        h
      end
    end

    # Let Semantic Logger handle duration logging
    module Middleware
      module Server
        class Logging
          # rubocop:disable Style/ExplicitBlockArgument
          def call(worker, item, queue)
            SemanticLogger.tagged(queue: queue) do
              worker.logger.info(
                "Start #perform",
                metric:        "sidekiq.queue.latency",
                metric_amount: job_latency_ms(item)
              )
              worker.logger.measure_info(
                "Completed #perform",
                on_exception_level: :error,
                log_exception:      :full,
                metric:             "sidekiq.job.perform"
              ) { yield }
            end
          end

          def job_latency_ms(job)
            return unless job && job["enqueued_at"]

            (Time.now.to_f - job["enqueued_at"].to_f) * 1000
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rails_semantic_logger-4.17.0 lib/rails_semantic_logger/extensions/sidekiq/sidekiq.rb