Sha256: 9caef20665a7ffdce7d8b38c626cc1a1c94ecdc7416f65487a565acebbef8ada

Contents?: true

Size: 1.09 KB

Versions: 1

Compression:

Stored size: 1.09 KB

Contents

require 'ears/middleware'

module Ears
  module Middlewares
    # A middleware that automatically wraps {Ears::Consumer#work} in an Appsignal transaction.
    class Appsignal < Middleware
      # @param [Hash] opts The options for the middleware.
      # @option opts [String] :class_name The name of the class you want to monitor.
      # @option opts [String] :namespace ('background') The namespace in which the action should appear.
      def initialize(opts)
        super()
        @class_name = opts.fetch(:class_name)
        @namespace = opts.fetch(:namespace, 'background')
      end

      def call(delivery_info, metadata, payload, app)
        start_transaction do
          begin
            app.call(delivery_info, metadata, payload)
          rescue => e
            ::Appsignal.set_error(e)
            raise
          end
        end
      end

      private

      attr_reader :namespace, :class_name

      def start_transaction(&block)
        ::Appsignal.monitor(
          namespace: namespace,
          action: "#{class_name}#work",
          &block
        )
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ears-0.14.0 lib/ears/middlewares/appsignal.rb