Sha256: e8a1fb109864b103e3d5e72386ea0cd6705a166f6eff226e1e65b658305c8f21

Contents?: true

Size: 1.94 KB

Versions: 39

Compression:

Stored size: 1.94 KB

Contents

module Sentry
  module Sidekiq
    class ContextFilter
      ACTIVEJOB_RESERVED_PREFIX_REGEX = /^_aj_/.freeze
      SIDEKIQ_NAME = "Sidekiq".freeze

      attr_reader :context

      def initialize(context)
        @context = context
        @has_global_id = defined?(GlobalID)
      end

      # Once an ActiveJob is queued, ActiveRecord references get serialized into
      # some internal reserved keys, such as _aj_globalid.
      #
      # The problem is, if this job in turn gets queued back into ActiveJob with
      # these magic reserved keys, ActiveJob will throw up and error. We want to
      # capture these and mutate the keys so we can sanely report it.
      def filtered
        filtered_context = filter_context(context)

        if job_entry = filtered_context.delete(:job)
          job_entry.each do |k, v|
            filtered_context[k] = v
          end
        end

        filtered_context
      end

      def transaction_name
        class_name = (context["wrapped"] || context["class"] ||
                      (context[:job] && (context[:job]["wrapped"] || context[:job]["class"]))
                    )

        if class_name
          "#{SIDEKIQ_NAME}/#{class_name}"
        elsif context[:event]
          "#{SIDEKIQ_NAME}/#{context[:event]}"
        else
          SIDEKIQ_NAME
        end
      end

      private

      def filter_context(hash)
        case hash
        when Array
          hash.map { |arg| filter_context(arg) }
        when Hash
          Hash[hash.map { |key, value| filter_context_hash(key, value) }]
        else
          if has_global_id? && hash.is_a?(GlobalID)
            hash.to_s
          else
            hash
          end
        end
      end

      def filter_context_hash(key, value)
        key = key.to_s.sub(ACTIVEJOB_RESERVED_PREFIX_REGEX, "") if key.match(ACTIVEJOB_RESERVED_PREFIX_REGEX)
        [key, filter_context(value)]
      end

      def has_global_id?
        @has_global_id
      end
    end
  end
end

Version data entries

39 entries across 39 versions & 1 rubygems

Version Path
sentry-sidekiq-5.7.0 lib/sentry/sidekiq/context_filter.rb
sentry-sidekiq-5.6.0 lib/sentry/sidekiq/context_filter.rb
sentry-sidekiq-5.5.0 lib/sentry/sidekiq/context_filter.rb
sentry-sidekiq-5.4.2 lib/sentry/sidekiq/context_filter.rb
sentry-sidekiq-5.4.1 lib/sentry/sidekiq/context_filter.rb
sentry-sidekiq-5.4.0 lib/sentry/sidekiq/context_filter.rb
sentry-sidekiq-5.3.1 lib/sentry/sidekiq/context_filter.rb
sentry-sidekiq-5.3.0 lib/sentry/sidekiq/context_filter.rb
sentry-sidekiq-5.2.1 lib/sentry/sidekiq/context_filter.rb
sentry-sidekiq-5.2.0 lib/sentry/sidekiq/context_filter.rb
sentry-sidekiq-5.1.1 lib/sentry/sidekiq/context_filter.rb
sentry-sidekiq-5.1.0 lib/sentry/sidekiq/context_filter.rb
sentry-sidekiq-5.0.2 lib/sentry/sidekiq/context_filter.rb
sentry-sidekiq-5.0.1 lib/sentry/sidekiq/context_filter.rb
sentry-sidekiq-5.0.0 lib/sentry/sidekiq/context_filter.rb
sentry-sidekiq-4.9.2 lib/sentry/sidekiq/context_filter.rb
sentry-sidekiq-4.9.1 lib/sentry/sidekiq/context_filter.rb
sentry-sidekiq-4.9.0 lib/sentry/sidekiq/context_filter.rb
sentry-sidekiq-4.8.3 lib/sentry/sidekiq/context_filter.rb
sentry-sidekiq-4.8.2 lib/sentry/sidekiq/context_filter.rb