Skip to content

Instantly share code, notes, and snippets.

@garrensmith
Created June 26, 2012 16:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save garrensmith/2996973 to your computer and use it in GitHub Desktop.
Save garrensmith/2996973 to your computer and use it in GitHub Desktop.
CouchRest Instrumentation for Rails 3
# add to config/initializers/instrumentation.rb
module CouchRest
class Database
alias_method :old_get, :get
alias_method :old_view, :view
alias_method :old_update_doc, :update_doc
alias_method :old_delete_doc, :delete_doc
alias_method :old_save_doc, :save_doc
def get(id, params = {})
ActiveSupport::Notifications.instrument("query.couchdb", :params => {:id => id, :params => params}) do
old_get id, params
end
end
def view(name, params = {}, payload = {}, &block)
ActiveSupport::Notifications.instrument("view.couchdb", :params => {:name => name, :params => params, :payload => payload}) do
old_view name, params, payload, &block
end
end
def save_doc(doc, bulk = false, batch = false)
ActiveSupport::Notifications.instrument("save.couchdb", :params => {:doc => doc, :bulk => bulk, :batch => batch}) do
old_save_doc doc, bulk, batch
end
end
def update_doc(doc_id, params = {}, update_limit = 10)
ActiveSupport::Notifications.instrument("update.couchdb", :params => {:doc_id => doc_id, :params => params, :update_limit => update_limit}) do
old_update_doc doc_id, params, update_limit
end
end
def delete_doc(doc, bulk = false)
ActiveSupport::Notifications.instrument("delete.couchdb", :params => {:doc => doc}) do
old_delete_doc doc, bulk
end
end
end
end
module CouchInstrumentation
class LogSubscriber < ActiveSupport::LogSubscriber
def initialize
super
@odd_or_even = false
end
def query(event)
logit("Get",event)
end
def view(event)
logit("View", event)
end
def save(event)
logit("Save", event)
end
def update(event)
logit("Update", event)
end
def delete(event)
logit("Delete", event)
end
private
def logit(type, event)
#return unless logger.debug?
name = '%s (%.1fms)' % ["COUCHDB #{type}", event.duration]
query = event.payload[:params].map{ |k, v| "#{k}: #{color(v, BOLD, true)}" }.join(', ')
if odd?
debug " #{color(name, YELLOW, true)} [ #{query} ]"
else
debug " #{color(name, BLUE, true)} [ #{query} ]"
end
end
def odd?
@odd_or_even = !@odd_or_even
end
end
end
CouchInstrumentation::LogSubscriber.attach_to :couchdb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment