Sha256: 0e8b40115a1c3003207698f878e39ab13ab761d296267e954359d7a0a248b4ac

Contents?: true

Size: 1.57 KB

Versions: 1

Compression:

Stored size: 1.57 KB

Contents

if defined?(::Sidekiq)
  Appsignal.logger.info('Loading Sidekiq integration')

  module Appsignal
    module Integrations
      class SidekiqPlugin
        def job_keys
          @job_keys ||= Set.new(%w(
            class args retried_at failed_at
            error_message error_class backtrace
            error_backtrace enqueued_at retry
          ))
        end

        def call(worker, item, queue)
          Appsignal.monitor_transaction(
            'perform_job.sidekiq',
            :class       => item['class'],
            :method      => 'perform',
            :metadata    => formatted_metadata(item),
            :params      => format_args(item['args']),
            :queue_start => item['enqueued_at']
          ) do
            yield
          end
        end

        def formatted_metadata(item)
          {}.tap do |hsh|
            item.each do |key, val|
              hsh[key] = truncate(string_or_inspect(val)) unless job_keys.include?(key)
            end
          end
        end

        def string_or_inspect(string_or_other)
          if string_or_other.is_a?(String)
            string_or_other
          else
            string_or_other.inspect
          end
        end

        def format_args(args)
          args.map do |arg|
            truncate(string_or_inspect(arg))
          end
        end

        def truncate(text)
          text.size > 200 ? "#{text[0...197]}..." : text
        end
      end
    end
  end

  Sidekiq.configure_server do |config|
    config.server_middleware do |chain|
      chain.add Appsignal::Integrations::SidekiqPlugin
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
appsignal-0.11.13 lib/appsignal/integrations/sidekiq.rb