Sha256: 65bec574317cff831a0bd560b08a3e9bc9aeb16a7b977066c04055b52af7c469

Contents?: true

Size: 1.72 KB

Versions: 18

Compression:

Stored size: 1.72 KB

Contents

begin
  require "active_support"
rescue LoadError
end

if defined?(ActiveSupport)
  module Frenchy
    module Instrumentation
      class LogSubscriber < ActiveSupport::LogSubscriber
        def start_processing(event)
          Thread.current[:frenchy_runtime] = 0.0
        end

        def request(event)
          Thread.current[:frenchy_runtime] ||= 0.0
          Thread.current[:frenchy_runtime] += event.duration
          if logger.debug?
            name = "%s (%.2fms)" % [event.payload[:service].capitalize, event.duration]
            output = "  #{color(name, YELLOW, true)} #{event.payload[:method].to_s.upcase} #{event.payload[:path]}"
            if event.payload[:params].any?
              output += "?"
              output += event.payload[:params].map {|k,v| "#{k}=#{v}" }.join("&")
            end
            debug output
          end
        end

        def self.runtime
          Thread.current[:frenchy_runtime] || 0.0
        end
      end

      module ControllerRuntime
        extend ActiveSupport::Concern

        protected

        def append_info_to_payload(payload)
          super
          payload[:frenchy_runtime] = Frenchy::Instrumentation::LogSubscriber.runtime
        end

        module ClassMethods
          def log_process_action(payload)
            messages = super
            if runtime = payload[:frenchy_runtime]
              messages << "Frenchy: %.1fms" % runtime.to_f
            end
            messages
          end
        end
      end
    end
  end

  Frenchy::Instrumentation::LogSubscriber.attach_to(:action_controller)
  Frenchy::Instrumentation::LogSubscriber.attach_to(:frenchy)

  ActiveSupport.on_load(:action_controller) do
    include Frenchy::Instrumentation::ControllerRuntime
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
frenchy-0.6.3 lib/frenchy/instrumentation.rb
frenchy-0.6.2 lib/frenchy/instrumentation.rb
frenchy-0.6.1 lib/frenchy/instrumentation.rb
frenchy-0.6.0 lib/frenchy/instrumentation.rb
frenchy-0.5.2 lib/frenchy/instrumentation.rb
frenchy-0.5.1 lib/frenchy/instrumentation.rb
frenchy-0.5.0 lib/frenchy/instrumentation.rb
frenchy-0.4.0 lib/frenchy/instrumentation.rb
frenchy-0.3.0 lib/frenchy/instrumentation.rb
frenchy-0.2.9 lib/frenchy/instrumentation.rb
frenchy-0.2.8 lib/frenchy/instrumentation.rb
frenchy-0.2.7 lib/frenchy/instrumentation.rb
frenchy-0.2.6 lib/frenchy/instrumentation.rb
frenchy-0.2.5 lib/frenchy/instrumentation.rb
frenchy-0.2.4 lib/frenchy/instrumentation.rb
frenchy-0.2.3 lib/frenchy/instrumentation.rb
frenchy-0.2.2 lib/frenchy/instrumentation.rb
frenchy-0.2.1 lib/frenchy/instrumentation.rb