Permalink
Browse files

Messy rails logger hooks to log per-request redis timings.

  • Loading branch information...
1 parent 61fd491 commit e96c33e4ea5cd85a63ffe76f788a25f71ab7a67f @slact committed Apr 26, 2012
Showing with 67 additions and 0 deletions.
  1. +21 −0 lib/queris.rb
  2. +22 −0 lib/rails/log_subscriber.rb
  3. +24 −0 lib/rails/request_timing.rb
View
@@ -42,6 +42,22 @@ def self.add_redis(redis, *roles)
@redis_by_role[role]||=[]
@redis_by_role[role] << redis
end
+ if Object.const_defined? 'ActiveSupport'
+ require "rails/log_subscriber"
+
+ #the following is one ugly monkey(patch).
+ # we assume that, since we're in Railsworld, the Redis logger
+ # is up for grabs. It would be cleaner to wrap the redis client in a class,
+ # but I'm coding dirty for brevity.
+ # THIS MUST BE ADDRESSED IN THE FUTURE
+
+ class << redis.client
+ protected
+ def logging(commands)
+ ActiveSupport::Notifications.instrument("command.queris") { yield }
+ end
+ end
+ end
end
@@ -113,3 +129,8 @@ def self.redis_prefix(app_name=nil)
end
end
end
+
+#ugly rails hooks
+if Object.const_defined? 'Rails'
+ require "rails/request_timing"
+end
@@ -0,0 +1,22 @@
+module Queris
+ class LogSubscriber < ActiveSupport::LogSubscriber
+ def self.runtime=(value)
+ Thread.current["queris_redis_runtime"] = value
+ end
+
+ def self.runtime
+ Thread.current["queris_redis_runtime"] ||= 0
+ end
+
+ def self.reset_runtime
+ rt, self.runtime = runtime, 0
+ rt
+ end
+
+ def command(event)
+ self.class.runtime += event.duration
+ end
+ end
+end
+
+Queris::LogSubscriber.attach_to :queris
@@ -0,0 +1,24 @@
+module Queris
+ module ControllerRuntime
+ extend ActiveSupport::Concern
+
+ protected
+
+ def append_info_to_payload(payload)
+ super
+ payload[:redis_queris_runtime] = Queris::LogSubscriber.runtime
+ end
+
+ module ClassMethods
+ def log_process_action(payload)
+ messages, queris_runtime = super, payload[:redis_queris_runtime]
+ messages << ("Queris: %.1fms" % queris_runtime.to_f) if queris_runtime
+ messages
+ end
+ end
+ end
+end
+
+ActiveSupport.on_load(:action_controller) do
+ include Queris::ControllerRuntime
+end

0 comments on commit e96c33e

Please sign in to comment.