Sha256: 016aca2feaa40d6c098b172839e1053840ecd6e00d96d9b41e24d3c6616100e5

Contents?: true

Size: 1.41 KB

Versions: 7

Compression:

Stored size: 1.41 KB

Contents

module Skylight
  module Sidekiq
    def self.add_middleware
      unless defined?(::Sidekiq)
        Skylight.warn "Skylight for Sidekiq is active, but Sidekiq is not defined."
        return
      end

      ::Sidekiq.configure_server do |sidekiq_config|
        Skylight.debug "Adding Sidekiq Middleware"

        sidekiq_config.server_middleware do |chain|
          # Put it at the front
          chain.prepend ServerMiddleware
        end
      end
    end

    class ServerMiddleware
      include Util::Logging

      def call(worker, job, queue)
        t { "Sidekiq middleware beginning trace" }
        title = job["wrapped"] || job["class"]

        # TODO: Using hints here would be ideal but requires further refactoring
        meta =
          if (source_location = worker.method(:perform).source_location)
            { source_file: source_location[0], source_line: source_location[1] }
          end

        Skylight.trace(title, "app.sidekiq.worker", title, meta: meta, segment: queue, component: :worker) do |trace|
          yield
        rescue Exception # includes Sidekiq::Shutdown
          trace.segment = "error" if trace
          raise
        end
      end
    end

    ActiveSupport::Notifications.subscribe("started_instrumenter.skylight") \
        do |_name, _started, _finished, _unique_id, payload|
      if payload[:instrumenter].config.enable_sidekiq?
        add_middleware
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
skylight-5.1.0.beta lib/skylight/sidekiq.rb
skylight-5.0.1 lib/skylight/sidekiq.rb
skylight-5.0.0 lib/skylight/sidekiq.rb
skylight-5.0.0.beta5 lib/skylight/sidekiq.rb
skylight-5.0.0.beta4 lib/skylight/sidekiq.rb
skylight-5.0.0.beta3 lib/skylight/sidekiq.rb
skylight-5.0.0.beta2 lib/skylight/sidekiq.rb