
TuneUp is a development performance monitoring tool from FiveRuns. It can help you catch poorly performing actions and queries early and is a great tool to add to your performance monitoring toolbelt. TuneUp also makes it very simple to add custom instrumentation to your Rails app, which came in handy when we needed to report on web service queries made by our Endeca client.
A brief chat with Bruce Williams helped point the way to TuneUp’s FiveRuns::TuneUp.step, the jumping-off point for its instrumentation of your model, view and controller activity. We were able to write a simple plugin that adds instrumentation to calls made by the Primedia Endeca gem (a gem used to consume Endeca’s RESTful JSON bridge API).
The plugin takes advantage of alias_method_chain to add instrumentation to the Endeca query methods. This solution is very clean and is idiomatic to Rails itself as Rails uses alias_method_chain to “embellish” many parts of the request response cycle for things like benchmarking and caching, as David mentions in a blog post from his Rails Myths series
If you want to look at the code, you can find the plugin at its github repo. All the code is in the init.rb but I’ll reprint it here for convenience. Note that we could have refactored these similar method definitions using metaprogramming techniques but chose not to do so for reasons of simplicity and clarity.
if defined? FiveRuns && defined? Endeca && defined? Endeca::Document # Tuneup instrumentation class << Endeca::Document def all_with_tuneup(*args) Fiveruns::Tuneup.step "#{name}.all", :model do all_without_tuneup(*args) end end alias_method_chain :all, :tuneup def first_with_tuneup(*args) Fiveruns::Tuneup.step "#{name}.first", :model do first_without_tuneup(*args) end end alias_method_chain :first, :tuneup def by_id_with_tuneup(*args) Fiveruns::Tuneup.step "#{name}.by_id", :model do by_id_without_tuneup(*args) end end alias_method_chain :by_id, :tuneup end end