Sha256: 7516c9be0fa78520956f0b36de11d5b641554ba4bd6f36626aec1e473191c8eb

Contents?: true

Size: 1.49 KB

Versions: 3

Compression:

Stored size: 1.49 KB

Contents

module AppMonit
  module Rails
    class Subscriber

      class << self
        def register
          ActiveSupport::Notifications.subscribe /process_action/, self.new
        end
      end

      def call(*args)
        event = ActiveSupport::Notifications::Event.new(*args)

        payload  = event.payload
        endpoint = "#{payload[:controller]}##{payload[:action]}"

        return if AppMonit::Rails::Config.skipped_endpoints.include?(endpoint)

        minute   = event.time.to_i - (event.time.to_i % 60)

        if payload[:exception]
          trigger_error(minute, endpoint, event.duration.to_i)
          trigger_error(minute, 'total', event.duration.to_i)
        else
          durations = { total: event.duration.to_i, view: payload[:view_runtime].to_i,
                        db:    payload[:db_runtime].to_i, ext: payload[:ext_runtime].to_i }
          trigger_event(minute, endpoint, durations)
          trigger_event(minute, 'total', durations)
        end
      end

      # @return [AppMonit::Worker]
      def worker
        AppMonit::Rails::Worker.instance
      end

      def trigger_error(minute, endpoint, duration)
        event = AppMonit::Rails::Error.new(minute, endpoint, duration)
        AppMonit::Rails.logger.debug event
        worker.push(event)
      end

      def trigger_event(minute, endpoint, durations)
        event = AppMonit::Rails::Event.new(minute, endpoint, durations)
        AppMonit::Rails.logger.debug event
        worker.push(event)
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
app_monit_rails-0.1.0 lib/app_monit/rails/subscriber.rb
app_monit_rails-0.0.6 lib/app_monit/rails/subscriber.rb
app_monit_rails-0.0.5 lib/app_monit/rails/subscriber.rb