Sha256: dc62560857626bf0596d188fbb9a6ec76bd041ccbdac80b06fb57a1074a68a76

Contents?: true

Size: 1.41 KB

Versions: 1

Compression:

Stored size: 1.41 KB

Contents

require 'appsignal'

module Pallets
  module Middleware
    class AppsignalInstrumenter
      extend Appsignal::Hooks::Helpers

      def self.call(worker, job, context)
        job_status = nil
        transaction = Appsignal::Transaction.create(
          SecureRandom.uuid,
          Appsignal::Transaction::BACKGROUND_JOB,
          Appsignal::Transaction::GenericRequest.new(queue_start: job['created_at'])
        )

        Appsignal.instrument('perform_job.pallets') do
          begin
            yield
          rescue Exception => ex
            job_status = :failed
            transaction.set_error(ex)
            raise
          ensure
            transaction.set_action_if_nil("#{job['task_class']}#run (#{job['workflow_class']})")
            transaction.params = filtered_context(context)
            formatted_metadata(job).each { |kv| transaction.set_metadata(*kv) }
            transaction.set_http_or_background_queue_start
            Appsignal::Transaction.complete_current!
            Appsignal.increment_counter('pallets_job_count', 1, status: job_status || :successful)
          end
        end
      end

      def self.filtered_context(context)
        Appsignal::Utils::HashSanitizer.sanitize(
          context,
          Appsignal.config[:filter_parameters]
        )
      end

      def self.formatted_metadata(job)
        job.map { |k, v| [k, truncate(string_or_inspect(v))] }
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
pallets-0.5.0 lib/pallets/middleware/appsignal_instrumenter.rb