Sha256: 049c29b8f51ebd2480f350d342131e94722571d0e3de6de03b0ab6ce29e44956

Contents?: true

Size: 1.61 KB

Versions: 29

Compression:

Stored size: 1.61 KB

Contents

module ActiveRestClient
  class Instrumentation < ActiveSupport::LogSubscriber
    def request_call(event)
      self.class.time_spent += event.duration
      self.class.calls_made += 1
      name = '%s (%.1fms)' % [ActiveRestClient::NAME, event.duration]
      ActiveRestClient::Logger.debug "  \033[1;4;32m#{name}\033[0m #{event.payload[:name]}"
    end

    def self.time_spent=(value)
      @@time_spent = value
    end

    def self.time_spent
      @@time_spent ||= 0
    end

    def self.calls_made=(value)
      @@calls_made = value
    end

    def self.calls_made
      @@calls_made ||= 0
    end

    def self.reset
      @@time_spent = 0
      @@calls_made = 0
    end

    def logger
      ActiveRestClient::Logger
    end
  end

  module ControllerInstrumentation
    extend ActiveSupport::Concern

    protected

    def append_info_to_payload(payload)
      super
      payload[:active_rest_client_time_spent] = ActiveRestClient::Instrumentation.time_spent
      payload[:active_rest_client_calls_made] = ActiveRestClient::Instrumentation.calls_made
    end

    module ClassMethods
      def log_process_action(payload)
        messages, time_spent, calls_made = super, payload[:active_rest_client_time_spent], payload[:active_rest_client_calls_made]
        messages << ("#{ActiveRestClient::NAME}: %.1fms for %d calls" % [time_spent.to_f, calls_made]) if calls_made
        ActiveRestClient::Instrumentation.reset
        messages
      end
    end
  end
end

ActiveRestClient::Instrumentation.attach_to :active_rest_client

ActiveSupport.on_load(:action_controller) do
  include ActiveRestClient::ControllerInstrumentation
end

Version data entries

29 entries across 29 versions & 1 rubygems

Version Path
active_rest_client-1.2.0 lib/active_rest_client/instrumentation.rb
active_rest_client-1.1.12 lib/active_rest_client/instrumentation.rb
active_rest_client-1.1.11 lib/active_rest_client/instrumentation.rb
active_rest_client-1.1.10 lib/active_rest_client/instrumentation.rb
active_rest_client-1.1.9 lib/active_rest_client/instrumentation.rb
active_rest_client-1.1.0 lib/active_rest_client/instrumentation.rb
active_rest_client-1.0.9 lib/active_rest_client/instrumentation.rb
active_rest_client-1.0.8 lib/active_rest_client/instrumentation.rb
active_rest_client-1.0.7 lib/active_rest_client/instrumentation.rb
active_rest_client-1.0.6 lib/active_rest_client/instrumentation.rb
active_rest_client-1.0.5 lib/active_rest_client/instrumentation.rb
active_rest_client-1.0.4 lib/active_rest_client/instrumentation.rb
active_rest_client-1.0.3 lib/active_rest_client/instrumentation.rb
active_rest_client-1.0.2 lib/active_rest_client/instrumentation.rb
active_rest_client-1.0.1 lib/active_rest_client/instrumentation.rb
active_rest_client-1.0.0 lib/active_rest_client/instrumentation.rb
active_rest_client-0.9.75 lib/active_rest_client/instrumentation.rb
active_rest_client-0.9.73 lib/active_rest_client/instrumentation.rb
active_rest_client-0.9.72 lib/active_rest_client/instrumentation.rb
active_rest_client-0.9.71 lib/active_rest_client/instrumentation.rb